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 secret key

Encrypt data with a password

scrypt + AES-GCM

Work with passwords

Store and check users’ passwords

bcrypt

Work with passwords

Encrypt data for a recipient’s public key

HPKE

Encrypt data for a recipient’s public key

Sign data, or verify a signature

Ed25519

Sign data and verify signatures

Compute a hash of data or of a file

SHA-256

Hash data and authenticate messages

Authenticate a message with a secret key (MAC)

HMAC-SHA256

Hash data and authenticate messages

Generate, save, and load keys

PEM files (PKCS#8)

Generate, save, and load keys

Agree on a secret key with another party

ECDH (P-256) + HKDF

Generate, save, and load keys

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