aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2015-09-04 17:58:27 -0400
committerAlex Gaynor <alex.gaynor@gmail.com>2015-09-04 17:58:27 -0400
commitab75a307b242b00a94c207f65a539d1ed0682468 (patch)
treece79e9af74261f785c535238bc369e2324708e57
parentcad58d7bfa1a01ade66828498dcb2894993838b9 (diff)
parent209e6300a5e53e1024e548664174419cbb55b942 (diff)
downloadcryptography-ab75a307b242b00a94c207f65a539d1ed0682468.tar.gz
cryptography-ab75a307b242b00a94c207f65a539d1ed0682468.tar.bz2
cryptography-ab75a307b242b00a94c207f65a539d1ed0682468.zip
Merge pull request #2322 from reaperhulk/docs-fix
fix a docs typo and convert it to a doctest to prevent future problems
-rw-r--r--docs/fernet.rst46
1 files changed, 24 insertions, 22 deletions
diff --git a/docs/fernet.rst b/docs/fernet.rst
index a066ae63..a2bab32a 100644
--- a/docs/fernet.rst
+++ b/docs/fernet.rst
@@ -115,28 +115,30 @@ password through a key derivation function such as
:class:`~cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC`, bcrypt or
scrypt.
-.. code-block:: python
-
- import base64
- import os
-
- from cryptography.fernet import Fernet
- from cryptography.hazmat.backends import default_backend
- from cryptography.hazmat.primitives import hashes
- from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
-
- password = b"password"
- salt = os.urandom(16)
-
- kdf = PBKDF2HMAC(
- algorithm=hashes.SHA256(),
- length=32,
- salt=salt,
- iterations=100000,
- backend=default_backend
- )
- key = base64.urlsafe_b64encode(kdf.derive(password))
- f = Fernet(key)
+.. doctest::
+
+ >>> import base64
+ >>> import os
+ >>> from cryptography.fernet import Fernet
+ >>> from cryptography.hazmat.backends import default_backend
+ >>> from cryptography.hazmat.primitives import hashes
+ >>> from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
+ >>> password = b"password"
+ >>> salt = os.urandom(16)
+ >>> kdf = PBKDF2HMAC(
+ ... algorithm=hashes.SHA256(),
+ ... length=32,
+ ... salt=salt,
+ ... iterations=100000,
+ ... backend=default_backend()
+ ... )
+ >>> key = base64.urlsafe_b64encode(kdf.derive(password))
+ >>> f = Fernet(key)
+ >>> token = f.encrypt(b"Secret message!")
+ >>> token
+ '...'
+ >>> f.decrypt(token)
+ 'Secret message!'
In this scheme, the salt has to be stored in a retrievable location in order
to derive the same key from the password in the future.