or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cryptographic-hashing.mdcryptographic-protocols.mddigital-signatures.mdindex.mdinput-output-operations.mdmathematical-primitives.mdpublic-key-cryptography.mdsymmetric-encryption.mdutility-functions.md
tile.json

tessl/pypi-pycryptodome

PyCryptodome is a self-contained Python package of low-level cryptographic primitives

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pycryptodome@3.23.x

To install, run

npx @tessl/cli install tessl/pypi-pycryptodome@3.23.0

index.mddocs/

PyCryptodome

PyCryptodome 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.

Package Information

  • Package Name: pycryptodome
  • Language: Python
  • Installation: pip install pycryptodome

Core Imports

import Crypto

Common 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, unpad

Basic Usage

from 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()

Architecture

PyCryptodome is organized into functional modules that provide distinct cryptographic capabilities:

  • Cipher Module: Symmetric and asymmetric encryption algorithms with multiple modes of operation
  • Hash Module: Cryptographic hash functions, message authentication codes, and extendable output functions
  • PublicKey Module: Public key cryptography including RSA, DSA, ECC, and ElGamal key management
  • Signature Module: Digital signature schemes for RSA, DSA, and elliptic curve algorithms
  • Protocol Module: High-level cryptographic protocols including key derivation and secret sharing
  • Util Module: Utility functions for padding, encoding, number theory, and data manipulation
  • IO Module: Input/output utilities for PEM and PKCS#8 key formats
  • Math Module: Mathematical primitives for big integers and primality testing
  • Random Module: Cryptographically secure random number generation

This modular design enables developers to import only the required functionality while maintaining compatibility with the broader Python cryptographic ecosystem.

Capabilities

Symmetric Encryption

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_OCB

Symmetric Encryption

Cryptographic Hashing

Complete 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): ...

Cryptographic Hashing

Public Key Cryptography

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): ...

Public Key Cryptography

Digital Signatures

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): ...

Digital Signatures

Cryptographic Protocols

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): ...

Cryptographic Protocols

Utility Functions

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): ...

Utility Functions

Input/Output Operations

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): ...

Input/Output Operations

Mathematical Primitives

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): ...

Mathematical Primitives

Types

# 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: ...