pub trait SegmentCipher: Send + Sync {
// Required methods
fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, CipherError>;
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, CipherError>;
}Expand description
Encrypts and decrypts segment file payloads.
Implementations must be Send + Sync because the buffer is shared
across threads via Arc<SegmentBuffer>.
The ciphertext format is implementation-defined but must be self-describing:
decrypt must be able to recover the plaintext from the
exact bytes returned by encrypt without external state.
§Naming
The trait is called SegmentCipher, not SegmentAead, even though the
shipped implementation (the AesGcmCipher behind the encryption feature)
is an AEAD. This is deliberate: the trait contract is “any stateless
self-describing encrypt/decrypt pair”, which admits AEADs (recommended),
HMAC-wrapped symmetric ciphers, or even custom schemes that combine
encryption with a separate authenticator. Renaming to SegmentAead would
narrow the contract to AEADs only — a constraint the trait does not actually
enforce. Use an AEAD in practice; the trait stays general on purpose.
§Example
use segment_buffer::{CipherError, SegmentCipher};
struct Rot13;
impl SegmentCipher for Rot13 {
fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, CipherError> {
Ok(plaintext.iter().map(|b| b.wrapping_add(13)).collect())
}
fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, CipherError> {
Ok(ciphertext.iter().map(|b| b.wrapping_sub(13)).collect())
}
}Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".