blob: 02b99705162130419abd2cac8ed97cc9caef2982 [file] [log] [blame]
Alex Gaynor333fb102013-10-31 10:27:35 -07001Fernet
2======
3
4.. currentmodule:: cryptography.fernet
5
6.. testsetup::
7
8 import binascii
Alex Gaynor36e2df02013-10-31 10:40:17 -07009 key = binascii.unhexlify(b"0" * 64)
Alex Gaynor333fb102013-10-31 10:27:35 -070010
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 >>> ciphertext
26 '...'
Alex Gaynor333fb102013-10-31 10:27:35 -070027 >>> f.decrypt(ciphertext)
28 'my deep dark secret'
29
30 :param bytes key: A 32-byte key. This **must** be kept secret. Anyone with
31 this key is able to create and read messages.
32
33
34 .. method:: encrypt(plaintext)
35
36 :param bytes plaintext: The message you would like to encrypt.
37 :returns bytes: A secure message which cannot be read or altered
Alex Gaynorde475eb2013-10-31 10:35:19 -070038 without the key. It is URL safe base64-encoded.
Alex Gaynor333fb102013-10-31 10:27:35 -070039
40 .. method:: decrypt(ciphertext, ttl=None)
41
42 :param bytes ciphertext: An encrypted message.
43 :param int ttl: Optionally, the number of seconds old a message may be
44 for it to be valid. If the message is older than
45 ``ttl`` seconds (from the time it was originally
Alex Gaynor13e0d542013-10-31 10:38:04 -070046 created) an exception will be raised. If ``ttl`` is not
47 provided (or is ``None``), the age of the message is
48 not considered.
Alex Gaynor333fb102013-10-31 10:27:35 -070049 :returns bytes: The original plaintext.
50
51
52.. _`Fernet`: https://github.com/fernet/spec/