Expand description
§cryptainer
Small, password-based authenticated encryption for Rust.
cryptainer derives a 256-bit key from a password with Argon2id and
encrypts data with AES-256-GCM. It supports no_std with alloc, while
file I/O and typed struct encryption remain opt-in features.
§Feature flags
std(default) enables file helpers and standard-library integration.deriveenables bincode-backed Serde struct encryption and the [Cryptainer] derive macro.zeroize(default) enables zeroization of derived keys and temporary serialized plaintext buffers where supported.
§Examples
§Encrypt bytes
let mut rng = rand::rng();
let data = encrypt_data("password", b"hello world", &mut rng)?;§Encrypt a struct
With the derive feature enabled, derive [Cryptainer] alongside Serde’s
Serialize and Deserialize:
use cryptainer::Cryptainer;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Cryptainer)]
struct Secret {
message: String,
}
let mut rng = rand::rng();
let secret = Secret { message: "hello".into() };
let encrypted = secret.encrypt("password", &mut rng)?;
let decrypted = Secret::decrypt(&encrypted, "password")?;§Security notes
Every encryption generates a fresh salt and nonce. The serialized format is
salt (16 bytes) || nonce (12 bytes) || ciphertext, where the ciphertext
includes the AES-GCM authentication tag. Password strength and password
transport are the responsibility of the calling application.
When zeroize is enabled, derived keys and temporary serialized plaintext
buffers are cleared when dropped. Returned plaintext and caller-owned
buffers remain the caller’s responsibility.
§Decrypt bytes
let mut rng = rand::rng();
let encrypted = encrypt_data("password", b"hello world", &mut rng)?;
let plaintext = decrypt_data(&encrypted, "password")?;§Save and load files
let mut rng = rand::rng();
let data = encrypt_data("password", b"secret", &mut rng)?;
save_file("secret.stuff", &data)?;
let plaintext = decrypt_file("secret.stuff", "password")?;With the derive feature, load and decrypt a persisted struct directly:
use cryptainer::{load_value, save_file};
save_file("secret.stuff", &encrypted)?;
let secret: Secret = load_value("secret.stuff", "password")?;Structs§
- Crypt
Data - Encrypted data returned by
encrypt_dataorencrypt_with_key.
Enums§
- Crypt
Error - Errors returned by encryption, decryption, and file helpers.
Functions§
- decrypt_
bytes - Decrypts serialized data encoded as
salt || nonce || ciphertext. - decrypt_
bytes_ with_ key - Parses and decrypts serialized encrypted data with a pre-derived key.
- decrypt_
data - Decrypts data produced by
encrypt_data. - decrypt_
file - Reads an encrypted file from disk and decrypts it with
password. - decrypt_
file_ with_ key - Reads an encrypted file and decrypts it with a pre-derived 256-bit key.
- decrypt_
with_ key - Decrypts
CryptDatawith a pre-derived 256-bitkey. - derive_
key - Derives a 256-bit Argon2id key from
passwordand a 16-bytesalt. - encrypt_
data - Encrypts
plaintextwith a key derived frompassword. - encrypt_
with_ key - Encrypts
plaintextwith a pre-derived 256-bitkeyand caller-provided 16-bytesalt. - load_
file - Loads serialized encrypted data from disk.
- save_
file - Writes encrypted data to disk in
salt || nonce || ciphertextorder.
Type Aliases§
- KeyBuffer
- A 32-byte derived key that is zeroized when dropped.