def encode(self, value): """ Identity encoding (str->str) with signature """ if self._signkey: from Crypto.Hash import HMAC value = _sharedance.escape( HMAC.new(self._signkey, value).digest()[:8] ) + value return value
def decode(self, value): """ Identity decoding (str->str) with integrity checking """ if self._signkey: sign, value = value[:11], value[11:] # 11 = 8 * 4/3 - padding from Crypto.Hash import HMAC if _sharedance.escape( HMAC.new(self._signkey, value).digest()[:8]) != sign: raise ValueError() return value
def _gensid(self): """ Generate a random session ID """ numbytes, rest = divmod(self._SIDLEN * 3, 4) numbytes += int(bool(rest)) return _sharedance.escape(_os.urandom(numbytes))[:self._SIDLEN]