pub struct Secret(/* private fields */);Expand description
A 256-bit symmetric encryption key for content encryption
Each Secret is used to encrypt a single item (node or data blob) using ChaCha20-Poly1305 AEAD.
The encrypted format is: nonce (12 bytes) || ciphertext (variable) || tag (16 bytes).
§Examples
// Generate a new random secret
let secret = Secret::generate();
// Encrypt data
let plaintext = b"sensitive data";
let ciphertext = secret.encrypt(plaintext)?;
// Decrypt data
let recovered = secret.decrypt(&ciphertext)?;
assert_eq!(plaintext, &recovered[..]);Implementations§
Source§impl Secret
impl Secret
Sourcepub fn from_slice(data: &[u8]) -> Result<Self, SecretError>
pub fn from_slice(data: &[u8]) -> Result<Self, SecretError>
Create a secret from a byte slice
§Errors
Returns an error if the slice length is not exactly SECRET_SIZE bytes.
Sourcepub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, SecretError>
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, SecretError>
Encrypt data using ChaCha20-Poly1305 AEAD
The output format is: nonce (12 bytes) || ciphertext || auth_tag (16 bytes).
A random nonce is generated for each encryption operation.
§Errors
Returns an error if encryption fails (should be rare, only on system RNG failure).
Sourcepub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, SecretError>
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, SecretError>
Decrypt data using ChaCha20-Poly1305 AEAD
Expects input in the format: nonce (12 bytes) || ciphertext || auth_tag (16 bytes).
§Errors
Returns an error if:
- Data is too short to contain a nonce
- Authentication tag verification fails (data was tampered with or wrong key)
Sourcepub fn encrypt_reader<R>(&self, reader: R) -> Result<impl Read, SecretError>where
R: Read,
pub fn encrypt_reader<R>(&self, reader: R) -> Result<impl Read, SecretError>where
R: Read,
Create an encrypted reader from a plaintext reader
This buffers all data in memory, encrypts it, and returns a reader over the encrypted data. Future optimization: implement true streaming encryption.
Sourcepub fn decrypt_reader<R>(&self, reader: R) -> Result<impl Read, SecretError>where
R: Read,
pub fn decrypt_reader<R>(&self, reader: R) -> Result<impl Read, SecretError>where
R: Read,
Create a decrypted reader from an encrypted reader
This buffers all encrypted data in memory, decrypts it, and returns a reader over the plaintext. Future optimization: implement true streaming decryption.