Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 1 | import base64 |
| 2 | |
| 3 | from cryptography.fernet import Fernet |
| 4 | |
| 5 | |
| 6 | class TestFernet(object): |
| 7 | def test_generate(self): |
| 8 | f = Fernet(base64.urlsafe_b64decode( |
| 9 | b"cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=" |
| 10 | )) |
| 11 | token = f._encrypt_from_parts( |
| 12 | b"hello", |
| 13 | 499162800, |
| 14 | b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", |
| 15 | ) |
Alex Gaynor | 5e87dfd | 2013-10-31 09:46:03 -0700 | [diff] [blame] | 16 | assert token == (b"gAAAAAAdwJ6wAAECAwQFBgcICQoLDA0ODy021cpGVWKZ_eEwCGM" |
| 17 | "4BLLF_5CV9dOPmrhuVUPgJobwOz7JcbmrR64jVmpU4IwqDA==") |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 18 | |
| 19 | def test_verify(self): |
| 20 | f = Fernet(base64.urlsafe_b64decode( |
| 21 | b"cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=" |
| 22 | )) |
Alex Gaynor | 5e87dfd | 2013-10-31 09:46:03 -0700 | [diff] [blame] | 23 | payload = f.decrypt( |
| 24 | (b"gAAAAAAdwJ6wAAECAwQFBgcICQoLDA0ODy021cpGVWKZ_eEwCGM4BLLF_5CV9dO" |
| 25 | "PmrhuVUPgJobwOz7JcbmrR64jVmpU4IwqDA=="), |
| 26 | ttl=60, |
| 27 | current_time=499162801 |
| 28 | ) |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 29 | assert payload == b"hello" |