blob: ac610eb8cc9352e0657c925d2a6e4256eeea752f [file] [log] [blame]
Alex Gaynor333fb102013-10-31 10:27:35 -07001Fernet
2======
3
4.. currentmodule:: cryptography.fernet
5
6.. testsetup::
7
8 import binascii
9 key = binascii.unhexlify(b"0" * 32)
10
11
12`Fernet`_ is an implementation of symmetric (also known as "secret key")
13authenticated cryptography. Fernet provides guarntees that a message encrypted
14using it cannot be manipulated or read without the key.
15
16.. class:: Fernet(key)
17
18 This class provides both encryption and decryption facilities.
19
20 .. doctest::
21
22 >>> from cryptography.fernet import Fernet
23 >>> f = Fernet(key)
24 >>> ciphertext = f.encrypt(b"my deep dark secret")
Alex Gaynorde475eb2013-10-31 10:35:19 -070025 # Secret bytes.
26 >>> ciphertext
27 '...'
Alex Gaynor333fb102013-10-31 10:27:35 -070028 >>> f.decrypt(ciphertext)
29 'my deep dark secret'
30
31 :param bytes key: A 32-byte key. This **must** be kept secret. Anyone with
32 this key is able to create and read messages.
33
34
35 .. method:: encrypt(plaintext)
36
37 :param bytes plaintext: The message you would like to encrypt.
38 :returns bytes: A secure message which cannot be read or altered
Alex Gaynorde475eb2013-10-31 10:35:19 -070039 without the key. It is URL safe base64-encoded.
Alex Gaynor333fb102013-10-31 10:27:35 -070040
41 .. method:: decrypt(ciphertext, ttl=None)
42
43 :param bytes ciphertext: An encrypted message.
44 :param int ttl: Optionally, the number of seconds old a message may be
45 for it to be valid. If the message is older than
46 ``ttl`` seconds (from the time it was originally
47 created) an exception will be raised.
48 :returns bytes: The original plaintext.
49
50
51.. _`Fernet`: https://github.com/fernet/spec/