Skip to content

Commit 62aba2e

Browse files
committed
Merge pull request realpython#465 from amiralis/master
add crypto
2 parents ac44d9a + a2f44b8 commit 62aba2e

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

docs/contents.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ different scenarios.
5959
scenarios/scientific
6060
scenarios/imaging
6161
scenarios/xml
62+
scenarios/crypto
6263

6364

6465
Shipping Great Code

docs/scenarios/crypto.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Cryptography
2+
============
3+
4+
Cryptography
5+
------------
6+
7+
`Cryptography <https://cryptography.io/en/latest/>`_ is an actively developed
8+
library that provides cryptographic recipes and primitives. It supports
9+
Python 2.6-2.7, Python 3.2+ and PyPy.
10+
11+
12+
Cryptography is divided into two layers of recipes and hazardous materials (hazmat).
13+
The recipes layer provides simple API for proper symmetric encryption and the
14+
hazmat layer provides low-level cryptographic primitives.
15+
16+
17+
18+
Installation
19+
~~~~~~~~~~~~
20+
21+
.. code-block:: console
22+
23+
$ pip install cryptography
24+
25+
Example
26+
~~~~~~~
27+
28+
Example code using high level symmetric encryption recipe:
29+
30+
.. code-block:: python
31+
32+
from cryptography.fernet import Fernet
33+
key = Fernet.generate_key()
34+
cipher_suite = Fernet(key)
35+
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
36+
plain_text = cipher_suite.decrypt(cipher_text)
37+
38+
39+
40+
41+
PyCrypto
42+
--------
43+
44+
`PyCrypto <https://www.dlitz.net/software/pycrypto/>`_ is another library,
45+
which provides secure hash functions and various encryption algorithms. It
46+
supports Python version 2.1 through 3.3.
47+
48+
Installation
49+
~~~~~~~~~~~~
50+
51+
.. code-block:: console
52+
53+
$ pip install pycrypto
54+
55+
Example
56+
~~~~~~~
57+
58+
.. code-block:: python
59+
60+
from Crypto.Cipher import AES
61+
# Encryption
62+
encryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
63+
cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")
64+
65+
# Decryption
66+
decryption_suite = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
67+
plain_text = decryption_suite.decrypt(cipher_text)

0 commit comments

Comments
 (0)