def test_encrypt_decrypt(): key_ = "1234523451234545" # 16 byte key # Iff padded the message doesn't have to be multiple of 16 in length msg_ = "ToBeOrNotTobe W.S." iv_ = os.urandom(16) encrypted_msg = encrypt(key_, msg_, iv_) txt = decrypt(key_, encrypted_msg, iv_) assert txt == msg_ encrypted_msg = encrypt(key_, msg_, 0) txt = decrypt(key_, encrypted_msg, 0) assert txt == msg_
def create_cookie(self, value, typ, cookie_name=None, ttl=-1, kill=False): if kill: ttl = -1 elif ttl < 0: ttl = self.cookie_ttl if cookie_name is None: cookie_name = self.srv.cookie_name timestamp = str(int(time.time())) _msg = "::".join([value, timestamp, typ]) if self.srv.symkey: # Pad the message to be multiples of 16 bytes in length lm = len(_msg) _msg = _msg.ljust(lm + 16 - lm % 16, self.pad_chr) info = encrypt(self.srv.symkey, _msg, self.srv.iv).decode("utf-8") else: info = _msg cookie = make_cookie(cookie_name, info, self.srv.seed, expire=ttl, domain="", path="", timestamp=timestamp) if PY2: return str(cookie[0]), str(cookie[1]) else: return cookie
def create_cookie(self, value, typ, cookie_name=None, ttl=-1, kill=False): if kill: ttl = -1 elif ttl < 0: ttl = self.cookie_ttl if cookie_name is None: cookie_name = self.srv.cookie_name timestamp = str(int(time.mktime(time.gmtime()))) _msg = "::".join([value, timestamp, typ]) if self.srv.symkey: # Pad the message to be multiples of 16 bytes in length lm = len(_msg) _msg = _msg.ljust(lm + 16 - lm % 16, self.pad_chr) info = encrypt(self.srv.symkey, _msg, self.srv.iv) else: info = _msg cookie = make_cookie(cookie_name, info, self.srv.seed, expire=ttl, domain="", path="") return cookie