cryptainer
Small, password-based authenticated encryption for Rust.
Encrypt byte slices or Serde-compatible structs with Argon2id and
AES-256-GCM, then decrypt them with a simple, typed API. cryptainer is
no_std-friendly and keeps file I/O optional.
Features
- Password-based encryption with Argon2id
- AES-256-GCM authenticated encryption
no_stdsupport withalloc- Optional derived-key zeroization
- Optional file helpers through
std - Optional bincode-backed struct encryption through
derive
Quick start
Add the crate with its standard-library defaults:
[]
= "2.1"
Encrypt bytes
use ;
let mut rng = rng;
let encrypted = encrypt_data?;
let plaintext = decrypt_data?;
encrypt_data returns a CryptData value containing the salt, nonce, and
ciphertext. Keep it together until you are ready to persist it or decrypt it.
Encrypt structs
Enable the derive feature:
[]
= { = "2.1", = ["derive"] }
= { = "1", = ["derive"] }
Then derive Cryptainer alongside Serde:
use Cryptainer;
use ;
let mut rng = rng;
let secret = Secret ;
let encrypted = secret.encrypt?;
let decrypted = decrypt?;
The derive generates encrypt and decrypt methods. Serialization uses
bincode 2; encrypted data is still authenticated by AES-GCM.
Save and load files
use ;
let mut rng = rng;
let encrypted = encrypt_data?;
save_file?;
let plaintext = decrypt_file?;
Format and security
Encrypted output is stored as:
salt (16 bytes) || nonce (12 bytes) || ciphertext
The ciphertext includes the AES-GCM authentication tag. Passwords are processed
with Argon2id to derive a 256-bit encryption key. A fresh salt and nonce are
generated for every call to encrypt_data; callers must provide a
cryptographically secure RNG.
When enabled, the zeroize feature clears derived keys and temporary
serialization buffers when they are dropped. It cannot clear passwords,
caller-owned plaintext, decrypted return values, or encrypted data retained
for persistence.
cryptainer does not provide password storage, password strength checking, key
rotation, or secure password transport. Applications should handle those
concerns separately.
API at a glance
encrypt_data(password, plaintext, rng) -> Result<CryptData, CryptError>decrypt_data(data, password) -> Result<Vec<u8>, CryptError>decrypt_bytes(data, password) -> Result<Vec<u8>, CryptError>encrypt_value(value, password, rng) -> Result<CryptData, CryptError>(withderive)decrypt_value(data, password) -> Result<T, CryptError>(withderive)save_file(file_path, data) -> Result<(), CryptError>(std only)decrypt_file(file_path, password) -> Result<Vec<u8>, CryptError>(std only)
Features
std(default): enables file I/O and standard library integrationzeroize(default): zeroizes derived keys and temporary serialized plaintext buffers where supportedderive: enables bincode-backed struct encryption and theCryptainerderive
License
MIT