Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 1 | Fernet |
| 2 | ====== |
| 3 | |
| 4 | .. currentmodule:: cryptography.fernet |
| 5 | |
| 6 | .. testsetup:: |
| 7 | |
Alex Gaynor | 43307c7 | 2013-11-21 21:50:14 -0800 | [diff] [blame^] | 8 | import base64 |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 9 | import binascii |
Alex Gaynor | 43307c7 | 2013-11-21 21:50:14 -0800 | [diff] [blame^] | 10 | key = base64.urlsafe_b64encode(binascii.unhexlify(b"0" * 64)) |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 11 | |
| 12 | |
| 13 | `Fernet`_ is an implementation of symmetric (also known as "secret key") |
Alex Gaynor | 43307c7 | 2013-11-21 21:50:14 -0800 | [diff] [blame^] | 14 | authenticated cryptography. Fernet provides guarantees that a message encrypted |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 15 | using it cannot be manipulated or read without the key. |
| 16 | |
| 17 | .. class:: Fernet(key) |
| 18 | |
| 19 | This class provides both encryption and decryption facilities. |
| 20 | |
| 21 | .. doctest:: |
| 22 | |
| 23 | >>> from cryptography.fernet import Fernet |
| 24 | >>> f = Fernet(key) |
| 25 | >>> ciphertext = f.encrypt(b"my deep dark secret") |
Alex Gaynor | de475eb | 2013-10-31 10:35:19 -0700 | [diff] [blame] | 26 | >>> ciphertext |
| 27 | '...' |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 28 | >>> f.decrypt(ciphertext) |
| 29 | 'my deep dark secret' |
| 30 | |
Alex Gaynor | 43307c7 | 2013-11-21 21:50:14 -0800 | [diff] [blame^] | 31 | :param bytes key: A base64 encoded 32-byte key. This **must** be kept |
| 32 | secret. Anyone with this key is able to create and read |
| 33 | messages. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 34 | |
| 35 | |
| 36 | .. method:: encrypt(plaintext) |
| 37 | |
| 38 | :param bytes plaintext: The message you would like to encrypt. |
| 39 | :returns bytes: A secure message which cannot be read or altered |
Alex Gaynor | de475eb | 2013-10-31 10:35:19 -0700 | [diff] [blame] | 40 | without the key. It is URL safe base64-encoded. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 41 | |
| 42 | .. method:: decrypt(ciphertext, ttl=None) |
| 43 | |
| 44 | :param bytes ciphertext: An encrypted message. |
| 45 | :param int ttl: Optionally, the number of seconds old a message may be |
| 46 | for it to be valid. If the message is older than |
| 47 | ``ttl`` seconds (from the time it was originally |
Alex Gaynor | 13e0d54 | 2013-10-31 10:38:04 -0700 | [diff] [blame] | 48 | created) an exception will be raised. If ``ttl`` is not |
| 49 | provided (or is ``None``), the age of the message is |
| 50 | not considered. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 51 | :returns bytes: The original plaintext. |
| 52 | |
| 53 | |
| 54 | .. _`Fernet`: https://github.com/fernet/spec/ |