Alex Gaynor | e908329 | 2013-12-17 16:56:29 -0800 | [diff] [blame] | 1 | Fernet (Symmetric encryption) |
| 2 | ============================= |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 3 | |
| 4 | .. currentmodule:: cryptography.fernet |
| 5 | |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 6 | `Fernet`_ is an implementation of symmetric (also known as "secret key") |
Alex Gaynor | 43307c7 | 2013-11-21 21:50:14 -0800 | [diff] [blame] | 7 | authenticated cryptography. Fernet provides guarantees that a message encrypted |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 8 | using it cannot be manipulated or read without the key. |
| 9 | |
| 10 | .. class:: Fernet(key) |
| 11 | |
| 12 | This class provides both encryption and decryption facilities. |
| 13 | |
| 14 | .. doctest:: |
| 15 | |
| 16 | >>> from cryptography.fernet import Fernet |
Alex Gaynor | 36597b4 | 2013-11-22 10:25:13 -0800 | [diff] [blame] | 17 | >>> key = Fernet.generate_key() |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 18 | >>> f = Fernet(key) |
Alex Gaynor | 0d08963 | 2013-12-17 20:23:43 -0800 | [diff] [blame] | 19 | >>> token = f.encrypt(b"my deep dark secret") |
| 20 | >>> token |
Alex Gaynor | de475eb | 2013-10-31 10:35:19 -0700 | [diff] [blame] | 21 | '...' |
Alex Gaynor | 0d08963 | 2013-12-17 20:23:43 -0800 | [diff] [blame] | 22 | >>> f.decrypt(token) |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 23 | 'my deep dark secret' |
| 24 | |
Alex Gaynor | 7a121fc | 2013-11-22 10:18:30 -0800 | [diff] [blame] | 25 | :param bytes key: A URL-safe base64-encoded 32-byte key. This **must** be |
| 26 | kept secret. Anyone with this key is able to create and |
| 27 | read messages. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 28 | |
Alex Gaynor | 36597b4 | 2013-11-22 10:25:13 -0800 | [diff] [blame] | 29 | .. classmethod:: generate_key() |
| 30 | |
| 31 | Generates a fresh fernet key. Keep this some place safe! If you lose it |
| 32 | you'll no longer be able to decrypt messages; if anyone else gains |
Alex Gaynor | 6cf242b | 2013-12-16 11:17:07 -0800 | [diff] [blame] | 33 | access to it, they'll be able to decrypt all of your messages, and |
| 34 | they'll also be able forge arbitrary messages which will be |
| 35 | authenticated and decrypted. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 36 | |
| 37 | .. method:: encrypt(plaintext) |
| 38 | |
| 39 | :param bytes plaintext: The message you would like to encrypt. |
| 40 | :returns bytes: A secure message which cannot be read or altered |
Alex Gaynor | 0d08963 | 2013-12-17 20:23:43 -0800 | [diff] [blame] | 41 | without the key. It is URL-safe base64-encoded. This is |
Alex Gaynor | 32dc4e4 | 2013-12-20 13:26:12 -0800 | [diff] [blame] | 42 | refered to as a "Fernet token". |
| 43 | |
Alex Gaynor | 719eb6a | 2013-12-20 13:35:57 -0800 | [diff] [blame^] | 44 | .. note:: |
Alex Gaynor | 32dc4e4 | 2013-12-20 13:26:12 -0800 | [diff] [blame] | 45 | |
| 46 | The encrypted message contains the current time when it was |
| 47 | generated in *plaintext*, the time a message was created will |
| 48 | therefore be visible to a possible attacker. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 49 | |
Alex Gaynor | 0d08963 | 2013-12-17 20:23:43 -0800 | [diff] [blame] | 50 | .. method:: decrypt(token, ttl=None) |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 51 | |
Alex Gaynor | 0d08963 | 2013-12-17 20:23:43 -0800 | [diff] [blame] | 52 | :param bytes token: The Fernet token. This is the result of calling |
| 53 | :meth:`encrypt`. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 54 | :param int ttl: Optionally, the number of seconds old a message may be |
| 55 | for it to be valid. If the message is older than |
| 56 | ``ttl`` seconds (from the time it was originally |
Alex Gaynor | 13e0d54 | 2013-10-31 10:38:04 -0700 | [diff] [blame] | 57 | created) an exception will be raised. If ``ttl`` is not |
| 58 | provided (or is ``None``), the age of the message is |
| 59 | not considered. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 60 | :returns bytes: The original plaintext. |
Alex Gaynor | 719eb6a | 2013-12-20 13:35:57 -0800 | [diff] [blame^] | 61 | :raises cryptography.fernet.InvalidToken: If the ``token`` is in any |
| 62 | way invalid, this exception |
| 63 | is raised. A token may be |
| 64 | invalid for a number of |
| 65 | reasons: it is older than the |
| 66 | ``ttl``, it is malformed, or |
| 67 | it does not have a valid |
| 68 | signature. |
Alex Gaynor | 7a121fc | 2013-11-22 10:18:30 -0800 | [diff] [blame] | 69 | |
| 70 | |
| 71 | .. class:: InvalidToken |
| 72 | |
| 73 | See :meth:`Fernet.decrypt` for more information. |
Alex Gaynor | 333fb10 | 2013-10-31 10:27:35 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | .. _`Fernet`: https://github.com/fernet/spec/ |