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) || encrypted(hash(32 bytes) || plaintext) || tag (16 bytes).
The BLAKE3 hash of the plaintext is prepended before encryption to enable content verification
without full decryption (useful for filesystem sync operations).
§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) || encrypted(hash(32) || plaintext) || auth_tag (16 bytes).
A BLAKE3 hash of the plaintext is computed and prepended to the data before encryption.
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) || encrypted(hash(32) || plaintext) || auth_tag (16 bytes).
Returns only the plaintext (hash is stripped but verified for integrity).
§Errors
Returns an error if:
- Data is too short to contain a nonce
- Authentication tag verification fails (data was tampered with or wrong key)
- Decrypted data is too short to contain the hash header
- Hash verification fails (data corruption)
Sourcepub fn extract_plaintext_hash(
&self,
data: &[u8],
) -> Result<[u8; 32], SecretError>
pub fn extract_plaintext_hash( &self, data: &[u8], ) -> Result<[u8; 32], SecretError>
Extract the BLAKE3 hash of the plaintext without decrypting the full content
This is useful for filesystem sync operations where you only need to compare content hashes without loading the entire file into memory.
§Errors
Returns an error if:
- Data is too short to contain a nonce
- Authentication tag verification fails (data was tampered with or wrong key)
- Decrypted data is too short to contain the hash header
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.