blob: 4e618f596f890ff7d5c30bccea05e7d5b1f16b00 [file] [log] [blame]
Alex Gaynore9083292013-12-17 16:56:29 -08001Fernet (Symmetric encryption)
2=============================
Alex Gaynor333fb102013-10-31 10:27:35 -07003
4.. currentmodule:: cryptography.fernet
5
Alex Gaynor333fb102013-10-31 10:27:35 -07006`Fernet`_ is an implementation of symmetric (also known as "secret key")
Alex Gaynor43307c72013-11-21 21:50:14 -08007authenticated cryptography. Fernet provides guarantees that a message encrypted
Alex Gaynor333fb102013-10-31 10:27:35 -07008using 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 Gaynor36597b42013-11-22 10:25:13 -080017 >>> key = Fernet.generate_key()
Alex Gaynor333fb102013-10-31 10:27:35 -070018 >>> f = Fernet(key)
Alex Gaynor0d089632013-12-17 20:23:43 -080019 >>> token = f.encrypt(b"my deep dark secret")
20 >>> token
Alex Gaynorde475eb2013-10-31 10:35:19 -070021 '...'
Alex Gaynor0d089632013-12-17 20:23:43 -080022 >>> f.decrypt(token)
Alex Gaynor333fb102013-10-31 10:27:35 -070023 'my deep dark secret'
24
Alex Gaynor7a121fc2013-11-22 10:18:30 -080025 :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 Gaynor333fb102013-10-31 10:27:35 -070028
Alex Gaynor36597b42013-11-22 10:25:13 -080029 .. 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 Gaynor6cf242b2013-12-16 11:17:07 -080033 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 Gaynor333fb102013-10-31 10:27:35 -070036
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 Gaynor0d089632013-12-17 20:23:43 -080041 without the key. It is URL-safe base64-encoded. This is
Alex Gaynor3ef458a2013-12-20 13:19:43 -080042 refered to as a "Fernet token". Note that this contains
43 the current time when it was generated in *plaintext*,
44 the time a message was created will therefore be
45 visible to a possible attacker.
Alex Gaynor333fb102013-10-31 10:27:35 -070046
Alex Gaynor0d089632013-12-17 20:23:43 -080047 .. method:: decrypt(token, ttl=None)
Alex Gaynor333fb102013-10-31 10:27:35 -070048
Alex Gaynor0d089632013-12-17 20:23:43 -080049 :param bytes token: The Fernet token. This is the result of calling
50 :meth:`encrypt`.
Alex Gaynor333fb102013-10-31 10:27:35 -070051 :param int ttl: Optionally, the number of seconds old a message may be
52 for it to be valid. If the message is older than
53 ``ttl`` seconds (from the time it was originally
Alex Gaynor13e0d542013-10-31 10:38:04 -070054 created) an exception will be raised. If ``ttl`` is not
55 provided (or is ``None``), the age of the message is
56 not considered.
Alex Gaynor333fb102013-10-31 10:27:35 -070057 :returns bytes: The original plaintext.
Alex Gaynor0d089632013-12-17 20:23:43 -080058 :raises InvalidToken: If the ``token`` is in any way invalid, this
59 exception is raised. A token may be invalid for a
60 number of reasons: it is older than the ``ttl``,
61 it is malformed, or it does not have a valid
62 signature.
Alex Gaynor7a121fc2013-11-22 10:18:30 -080063
64
65.. class:: InvalidToken
66
67 See :meth:`Fernet.decrypt` for more information.
Alex Gaynor333fb102013-10-31 10:27:35 -070068
69
70.. _`Fernet`: https://github.com/fernet/spec/