PyCryptodome is a self-contained Python package of low-level cryptographic primitives
npx @tessl/cli install tessl/pypi-pycryptodome@3.23.0PyCryptodome is a comprehensive self-contained Python library providing low-level cryptographic primitives. It offers a complete suite of symmetric ciphers, cryptographic hash functions, public key cryptography algorithms, digital signature schemes, key derivation functions, and secure random number generation.
pip install pycryptodomeimport CryptoCommon module imports:
from Crypto.Cipher import AES, ChaCha20, DES3
from Crypto.Hash import SHA256, HMAC
from Crypto.PublicKey import RSA, ECC
from Crypto.Signature import pkcs1_15, pss
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpadfrom Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
# Symmetric encryption with AES
key = get_random_bytes(16) # 128-bit key
cipher = AES.new(key, AES.MODE_CBC)
plaintext = b"Secret message that needs to be encrypted!"
padded_data = pad(plaintext, AES.block_size)
ciphertext = cipher.encrypt(padded_data)
# Decryption
decipher = AES.new(key, AES.MODE_CBC, cipher.iv)
decrypted_padded = decipher.decrypt(ciphertext)
decrypted = unpad(decrypted_padded, AES.block_size)
# Hash computation
hash_obj = SHA256.new()
hash_obj.update(b"Data to hash")
hash_digest = hash_obj.digest()
# RSA key generation and usage
key = RSA.generate(2048)
public_key = key.publickey()PyCryptodome is organized into functional modules that provide distinct cryptographic capabilities:
This modular design enables developers to import only the required functionality while maintaining compatibility with the broader Python cryptographic ecosystem.
Comprehensive collection of symmetric encryption algorithms including block ciphers (AES, DES, Blowfish) and stream ciphers (ChaCha20, Salsa20, ARC4) with support for multiple cipher modes.
# AES cipher factory
def AES.new(key, mode, *args, **kwargs): ...
# ChaCha20 cipher factory
def ChaCha20.new(**kwargs): ...
# Cipher mode constants
AES.MODE_ECB, AES.MODE_CBC, AES.MODE_CFB, AES.MODE_OFB, AES.MODE_CTR,
AES.MODE_GCM, AES.MODE_CCM, AES.MODE_EAX, AES.MODE_SIV, AES.MODE_OCBComplete suite of cryptographic hash functions including SHA family, SHA-3, MD family, BLAKE2, and specialized functions like SHAKE, cSHAKE, KMAC, and TurboSHAKE.
# Hash function factories
def SHA256.new(data=None): ...
def SHA3_256.new(data=None, update_after_digest=False): ...
def BLAKE2b.new(**kwargs): ...
# Message authentication codes
def HMAC.new(key, msg=b"", digestmod=None): ...
def CMAC.new(key, msg=None, ciphermod=None, **kwargs): ...Public key algorithms for key generation, encryption, and key exchange including RSA, DSA, ECC, and ElGamal with comprehensive key management capabilities.
# Key generation functions
def RSA.generate(bits, randfunc=None, e=65537): ...
def ECC.generate(**kwargs): ...
def DSA.generate(bits, randfunc=None, domain=None): ...
# Key import/export
def RSA.import_key(extern_key, passphrase=None): ...Digital signature schemes for RSA (PKCS#1 v1.5, PSS), DSA variants, and EdDSA with support for deterministic and probabilistic signing.
# RSA signature schemes
def pkcs1_15.new(rsa_key): ...
def pss.new(rsa_key, **kwargs): ...
# DSA signature scheme
def DSS.new(key, mode, encoding='binary', randfunc=None): ...High-level cryptographic protocols including key derivation functions (PBKDF2, scrypt, HKDF), secret sharing, and key exchange protocols.
# Key derivation functions
def scrypt(password, salt, key_len, N, r, p, num_keys=1): ...
def PBKDF2(password, salt, dkLen=16, count=1000, prf=None, **kwargs): ...
# Secret sharing
class Shamir:
@staticmethod
def split(k, n, secret, ssss=False): ...
@staticmethod
def combine(shares, ssss=False): ...Essential utilities for padding, encoding, number theory operations, and data manipulation that support the core cryptographic functions.
# Padding operations
def pad(data_to_pad, block_size, style='pkcs7'): ...
def unpad(padded_data, block_size, style='pkcs7'): ...
# Random number generation
def get_random_bytes(n): ...
# Number theory
def isPrime(N, false_positive_prob=1e-6, randfunc=None): ...
def getPrime(N, randfunc=None): ...Utilities for encoding and decoding cryptographic objects in standard formats including PEM and PKCS#8 for key serialization.
# PEM encoding/decoding
def PEM.encode(data, marker, passphrase=None, randfunc=None): ...
def PEM.decode(pem_data, passphrase=None): ...
# PKCS#8 key wrapping
def PKCS8.wrap(private_key, key_oid, passphrase=None, **kwargs): ...
def PKCS8.unwrap(p8_private_key, passphrase=None): ...Mathematical operations for cryptographic computations including big integer arithmetic and primality testing.
# Big integer class
class Integer:
def __init__(self, value): ...
# Arithmetic and modular operations available
# Primality testing
def miller_rabin_test(candidate, iterations, randfunc=None): ...
def test_probable_prime(candidate, randfunc=None): ...# Common cipher interface
class CipherObject:
def encrypt(self, plaintext: bytes) -> bytes: ...
def decrypt(self, ciphertext: bytes) -> bytes: ...
# Hash object interface
class HashObject:
def update(self, data: bytes) -> None: ...
def digest(self) -> bytes: ...
def hexdigest(self) -> str: ...
def copy(self): ...
# Key object interface
class KeyObject:
def has_private(self) -> bool: ...
def public_key(self): ...
def export_key(self, format: str = 'PEM', **kwargs) -> bytes: ...