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 get_cookie_value(self, cookie=None, cookie_name=None): """ Return information stored in the Cookie :param cookie: :param cookie_name: The name of the cookie I'm looking for :return: tuple (value, timestamp, type) """ if cookie is None or cookie_name is None: return None else: try: info, timestamp = parse_cookie(cookie_name, self.srv.seed, cookie) except (TypeError, AssertionError): return None else: if self.srv.symkey: txt = decrypt(self.srv.symkey, info, self.srv.iv) # strip spaces at the end txt = txt.rstrip(self.pad_chr) else: txt = info value, _ts, typ = txt.split("::") if timestamp == _ts: return value, _ts, typ return None
def authenticated_as(self, cookie=None, authorization="", **kwargs): """ :param cookie: A HTTP Cookie :param authorization: The HTTP Authorization header :param args: extra args :param kwargs: extra key word arguments :return: """ (encmsg, iv) = base64.b64decode(authorization).split(":") try: user = aes.decrypt(self.symkey, encmsg, iv) except (AssertionError, KeyError): raise FailedAuthentication("Decryption failed") return {"uid": user}, time.time()
def authenticated_as(self, cookie=None, authorization="", **kwargs): """ :param cookie: A HTTP Cookie :param authorization: The HTTP Authorization header :param args: extra args :param kwargs: extra key word arguments :return: """ (encmsg, iv) = base64.b64decode(authorization).split(":") try: user = aes.decrypt(self.symkey, encmsg, iv) except (AssertionError, KeyError): raise FailedAuthentication() return {"uid": user}