qSHA256 now provides cryptographically secure primitives built on pyca/cryptography and hashlib.
A Python library providing secure cryptographic primitives including SHA-256 hashing, HMAC authentication, HKDF key derivation, AES-GCM authenticated encryption, and Ed25519 digital signatures. All implementations use vetted cryptographic libraries and include strict input validation for production use.
- SHA-256 Hashing: Secure hash function using Python's
hashlib - HMAC Authentication: Message authentication with constant-time comparison
- HKDF Key Derivation: Extract and expand key material securely
- AES-GCM Encryption: Authenticated encryption with associated data
- Ed25519 Signatures: Fast, secure digital signatures
- Input Validation: Strict type checking and size limits
- Production Ready: Built on cryptographically secure foundations
pip install qsha256import qsha256
# Hash some data
data = b"Hello, World!"
hash_result = qsha256.secure_sha256(data)
print(f"SHA-256: {hash_result.hex()}")import qsha256
# Generate a key
key = qsha256.generate_key(32)
# Create HMAC
message = b"Authenticate this message"
hmac_tag = qsha256.secure_hmac(key, message)
# Verify HMAC
is_valid = qsha256.secure_hmac_verify(key, message, hmac_tag)
print(f"HMAC valid: {is_valid}")import qsha256
# Generate encryption key
key = qsha256.generate_key(32) # 256-bit key
# Encrypt data
plaintext = b"Sensitive information"
nonce, ciphertext = qsha256.aes_gcm_encrypt(key, plaintext)
# Decrypt data
decrypted = qsha256.aes_gcm_decrypt(key, nonce, ciphertext)
print(f"Decrypted: {decrypted}")import qsha256
# Generate key pair
private_key, public_key = qsha256.ed25519_generate_keypair()
# Sign a message
message = b"Sign this document"
signature = qsha256.ed25519_sign(private_key, message)
# Verify signature
is_valid = qsha256.ed25519_verify(public_key, message, signature)
print(f"Signature valid: {is_valid}")import qsha256
# Derive keys from input key material
salt = b"random_salt"
info = b"context_info"
ikm = b"input_key_material"
derived_key = qsha256.hkdf_extract_expand(salt, info, ikm, 32)
print(f"Derived key: {derived_key.hex()}")Compute SHA-256 hash of input data.
- data: Input bytes to hash
- Returns: 32-byte SHA-256 hash
Compute HMAC-SHA256 authentication tag.
- key: HMAC key (minimum 16 bytes)
- data: Data to authenticate
- Returns: 32-byte HMAC tag
Verify HMAC tag using constant-time comparison.
- key: HMAC key
- data: Original data
- tag: HMAC tag to verify
- Returns: True if valid, False otherwise
Generate cryptographically secure random key.
- length: Key length in bytes (default: 32)
- Returns: Random key bytes
Extract and expand key material using HKDF-SHA256.
- salt: Salt for key extraction
- info: Context information
- ikm: Input key material
- length: Output key length in bytes
- Returns: Derived key material
Encrypt data using AES-GCM authenticated encryption.
- key: AES key (16, 24, or 32 bytes)
- plaintext: Data to encrypt
- aad: Additional authenticated data (optional)
- Returns: (nonce, ciphertext_and_tag)
Decrypt data using AES-GCM authenticated encryption.
- key: AES key
- nonce: Nonce used for encryption
- ct: Ciphertext and tag
- aad: Additional authenticated data (optional)
- Returns: Decrypted plaintext
Generate Ed25519 key pair.
- Returns: (private_key, public_key) both 32 bytes
Sign a message using Ed25519.
- private_key: 32-byte private key
- message: Message to sign
- Returns: 64-byte signature
Verify Ed25519 signature.
- public_key: 32-byte public key
- message: Original message
- signature: 64-byte signature
- Returns: True if valid, False otherwise
- Input Validation: All functions validate input types and enforce size limits (default: 1 MB)
- Constant-Time Operations: HMAC verification uses constant-time comparison
- Secure Random: Key generation uses
secrets.token_bytes() - Vetted Libraries: Built on
hashlib,cryptography, andhmacmodules - Error Handling: Clear error messages for invalid inputs
# Install development dependencies
pip install -r requirements.txt
# Run tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=qsha256 --cov-report=html# Lint with ruff
ruff check qsha256/ tests/
# Security check with bandit
bandit -r qsha256/demos/ folder contains experimental quantum SHA-256 implementations for educational purposes only. These are NOT SECURE and should never be used in production.
To run experimental demos:
cd demos/
python demo.py- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
For security-related issues, please see SECURITY.md for our vulnerability disclosure policy.
This project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for version history and changes.