PyCryptodome¶
PyCryptodome is a self-contained Python package of low-level
cryptographic primitives, for Python 3.9 and newer.
It needs no system libraries: install it with pip install pycryptodome.
What do you want to do?¶
I want to… |
Recommended |
Guide |
|---|---|---|
Encrypt data with a secret key |
AES-GCM |
|
Encrypt data with a password |
scrypt + AES-GCM |
|
Store and check users’ passwords |
bcrypt |
|
Encrypt data for a recipient’s public key |
HPKE |
|
Sign data, or verify a signature |
Ed25519 |
|
Compute a hash of data or of a file |
SHA-256 |
|
Authenticate a message with a secret key (MAC) |
HMAC-SHA256 |
|
Generate, save, and load keys |
PEM files (PKCS#8) |
|
Agree on a secret key with another party |
ECDH (P-256) + HKDF |
For instance, this is how to encrypt and authenticate a message with AES in GCM mode, and then decrypt it (see Encrypt data with a secret key for the details):
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
key = get_random_bytes(32)
nonce = get_random_bytes(12)
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
ciphertext, tag = cipher.encrypt_and_digest(b"Attack at dawn")
# The receiver needs the key, the nonce, the ciphertext and the tag
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
Where to go next¶
Guides, for the common tasks, step by step.
API documentation, for all the algorithms and their parameters, grouped by package.
Installation, for the details of the installation.
Frequently Asked Questions, for the most frequent questions.