encryptman
AES-256-GCM encryption for application settings with HKDF key derivation.
Features
- AES-256-GCM authenticated encryption — guarantees confidentiality + integrity
- HKDF-SHA256 key derivation — derive purpose-specific keys safely from one master key
- Random nonces — encrypting the same plaintext twice yields distinct ciphertexts
- Zeroize on drop — master key memory is zeroed out automatically on drop
- Context isolation — prevents cross-domain ciphertext substitution attacks
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
use ;
let master_key = generate_master_key;
let encrypted = encrypt?;
let decrypted = decrypt?;
assert_eq!;
Usage
Basic encrypt/decrypt
use ;
let key = generate;
let ct = encrypt?;
let pt = decrypt?;
Multiple contexts with one key
use ;
let key = generate;
// Database passwords
let db_ct = encrypt_with_context?;
// API keys
let api_ct = encrypt_with_context?;
// Same plaintext, different contexts → different ciphertext
assert_ne!;
// Decrypt with the matching context
let db_pt = decrypt_with_context?;
URL-safe encoding (JWTs, cookies, URLs)
use ;
let key = generate_master_key;
let encrypted = encrypt_with_encoding?;
let decrypted = decrypt_with_encoding?;
assert_eq!;
Storing and restoring the master key
use MasterKey;
// Generate a new key
let key = generate;
// Export raw bytes (e.g., to store in OS keychain / Vault)
let bytes = *key.as_bytes;
// Reconstruct key later
let restored = from_bytes;
How It Works
master_key ──► HKDF-SHA256(context) ──► AES-256 key
│
plaintext + random 12-byte nonce ─────────────┴──► AES-256-GCM ──► version || nonce || ciphertext ──► Base64
- Key Derivation: HKDF-SHA256 generates domain-separated subkeys from a single master key.
- Authenticated Encryption: AES-256-GCM detects any payload tampering during decryption.
- Fresh Nonce: 12 random bytes generated per encryption call to maintain security guarantees.
- Version Byte: Reserved for future algorithm migrations.
Security Considerations
- Secure Storage: Always store the master key in a key management service (KMS), OS keychain, or encrypted secret store.
- Context Separation: Use distinct context strings for different application domains (e.g.,
"db","oauth") to prevent cross-context substitution. - Integrity Validation: Decryption will fail with an error if the payload has been tampered with or corrupted.
- Memory Security:
MasterKeyimplementsZeroizeto scrub key bytes from RAM when dropped.
When NOT to use this crate
This crate is designed for encrypting small strings (passwords, API keys, tokens). It is not suitable for:
- Password hashing — use argon2 or bcrypt instead.
- File encryption — use a streaming AEAD like ChaCha20Poly1305 with proper chunking.
- Database-at-rest encryption — use your database's built-in encryption (e.g., PostgreSQL
pgcrypto, MySQLAES_ENCRYPT). - Large data — this crate allocates the entire plaintext/ciphertext in memory.
Minimum Supported Rust Version
MSRV: 1.85 (edition 2024)
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.