aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2015-09-05 09:22:40 -0500
committerPaul Kehrer <paul.l.kehrer@gmail.com>2015-09-05 09:22:40 -0500
commita60b69ffdfc2b06c1379a0de116df873084fb17b (patch)
tree94228e21c6c658582dcef886841e9f2f98617953
parent8359190f6e509fa2d12c0d86c9dfd1a8b35bf9cb (diff)
parentab75a307b242b00a94c207f65a539d1ed0682468 (diff)
downloadcryptography-a60b69ffdfc2b06c1379a0de116df873084fb17b.tar.gz
cryptography-a60b69ffdfc2b06c1379a0de116df873084fb17b.tar.bz2
cryptography-a60b69ffdfc2b06c1379a0de116df873084fb17b.zip
Merge branch 'master' into static-linking-osx
-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.