blob: 7bdfa3fa620b9fa53ceed176f437034f9659119f [file] [log] [blame]
Alex Gaynor2b21b122013-10-31 09:39:25 -07001import base64
2
Alex Gaynorde36e902013-10-31 10:10:44 -07003import six
4
Alex Gaynor2b21b122013-10-31 09:39:25 -07005from cryptography.fernet import Fernet
6
7
8class TestFernet(object):
9 def test_generate(self):
10 f = Fernet(base64.urlsafe_b64decode(
11 b"cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4="
12 ))
13 token = f._encrypt_from_parts(
14 b"hello",
15 499162800,
Alex Gaynorde36e902013-10-31 10:10:44 -070016 b"".join(map(six.int2byte, range(16))),
Alex Gaynor2b21b122013-10-31 09:39:25 -070017 )
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070018 assert token == (b"gAAAAAAdwJ6wAAECAwQFBgcICQoLDA0ODy021cpGVWKZ_eEwCGM"
Alex Gaynor413bd8b2013-10-31 11:22:11 -070019 b"4BLLF_5CV9dOPmrhuVUPgJobwOz7JcbmrR64jVmpU4IwqDA==")
Alex Gaynor2b21b122013-10-31 09:39:25 -070020
21 def test_verify(self):
22 f = Fernet(base64.urlsafe_b64decode(
23 b"cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4="
24 ))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070025 payload = f.decrypt(
26 (b"gAAAAAAdwJ6wAAECAwQFBgcICQoLDA0ODy021cpGVWKZ_eEwCGM4BLLF_5CV9dO"
Alex Gaynor413bd8b2013-10-31 11:22:11 -070027 b"PmrhuVUPgJobwOz7JcbmrR64jVmpU4IwqDA=="),
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070028 ttl=60,
29 current_time=499162801
30 )
Alex Gaynor2b21b122013-10-31 09:39:25 -070031 assert payload == b"hello"