Ejemplo n.º 1
0
 def test_verify(self, secret, now, src, ttl_sec, token, backend,
                 monkeypatch):
     print("testing ....1")
     f = Fernet2(secret.encode("ascii"), backend=backend)
     current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
     monkeypatch.setattr(time, "time", lambda: current_time)
     payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
     assert payload == src.encode("ascii")
Ejemplo n.º 2
0
 def test_timestamp_ignored_no_ttl(self, monkeypatch, backend):
     f = Fernet2(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
     pt = b"encrypt me"
     token = f.encrypt(pt)
     ts = "1985-10-26T01:20:01-07:00"
     current_time = calendar.timegm(iso8601.parse_date(ts).utctimetuple())
     monkeypatch.setattr(time, "time", lambda: current_time)
     assert f.decrypt(token, ttl=None) == pt
Ejemplo n.º 3
0
 def test_adata(self, backend):
     adata = "Hello"
     ptxt = "spring break is coming"
     secret = "cx_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4="
     f = Fernet2(secret.encode("ascii"), backend=backend)
     ctxt = f.encrypt(ptxt, adata)
     adata_diff = "Hello World"
     with pytest.raises(InvalidToken):
         f.decrypt(token = ctxt, adata = adata_diff)
Ejemplo n.º 4
0
def _decrypt(fields):
    fn = Fernet2(SECRET_KEY)
    result = {}
    for k, v in fields.iteritems():
        if k in ENCRYPTED_COLS:
            result[k] = fn.decrypt(str(v).encode('utf-8'), ASSOCIATED_DATA)
        else:
            result[k] = str(v).encode('utf-8')
    return result
Ejemplo n.º 5
0
 def test_encrypt(self, adata, secret, ptxt, ctxt, iv, backend,
                 monkeypatch):
     print("testing ....encrypt")
     f = Fernet2(secret.encode("ascii"), backend=backend)
     # is this correct or should I use this? base64.urlsafe_b64encode(os.urandom(32))
     current_rand =  os.urandom(16)
     monkeypatch.setattr(os, "urandom", lambda: current_rand)
     # would this even work to check decryption with another key?
     payload = f.decrypt(ctxt.encode("ascii"), adata = adata)
     assert payload == ptxt.encode("ascii")
Ejemplo n.º 6
0
 def test_invalid_start_byte(self, backend):
     f = Fernet2(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
     with pytest.raises(InvalidToken):
         f.decrypt(base64.urlsafe_b64encode(b"\x83"))
Ejemplo n.º 7
0
 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
     f = Fernet2(secret.encode("ascii"), backend=backend)
     current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
     monkeypatch.setattr(time, "time", lambda: current_time)
     with pytest.raises(InvalidToken):
         f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Ejemplo n.º 8
0
def test_default_backend():
    f = Fernet2(Fernet2.generate_key())
    assert f._backend is default_backend()
Ejemplo n.º 9
0
 def test_decrypt(self, adata, secret, ptxt, ctxt, iv, backend,
                 monkeypatch):
     print("testing ....decrpyt")
     f = Fernet2(secret.encode("ascii"), backend=backend)
     payload = f.decrypt(ctxt.encode("ascii"), adata = adata)
     assert payload == ptxt.encode("ascii")
Ejemplo n.º 10
0
 def test_bad_key(self, backend):
     with pytest.raises(ValueError):
         Fernet2(base64.urlsafe_b64encode(b"abc"), backend=backend)
Ejemplo n.º 11
0
 def test_roundtrips(self, message, backend):
     f = Fernet2(Fernet2.generate_key(), backend=backend)
     assert f.decrypt(f.encrypt(message)) == message
Ejemplo n.º 12
0
 def test_unicode(self, backend):
     f = Fernet2(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
     with pytest.raises(TypeError):
         f.encrypt(u"")
     with pytest.raises(TypeError):
         f.decrypt(u"")
Ejemplo n.º 13
0
 def test_non_base64_token(self, backend):
     f = Fernet2(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
     with pytest.raises(InvalidToken):
         f.decrypt(b"\x00")
Ejemplo n.º 14
0
 def test_timestamp_too_short(self, backend):
     f = Fernet2(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
     with pytest.raises(InvalidToken):
         f.decrypt(base64.urlsafe_b64encode(b"\x80abc"))