Skip to main content

SegmentCipher

Trait SegmentCipher 

Source
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§

Source

fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>, CipherError>

Encrypt plaintext, returning self-describing ciphertext.

§Errors

Returns CipherError if the cipher fails to encrypt (e.g. RNG failure, internal AEAD error). Implementations must be deterministic in their failure modes — the same plaintext either always succeeds or always fails.

Source

fn decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>, CipherError>

Decrypt previously-produced ciphertext back to the original plaintext.

§Errors

Returns CipherError if the ciphertext is too short for the cipher’s nonce, the authentication tag does not verify (wrong key or tampering), or the underlying AEAD reports a decryption failure.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl SegmentCipher for AesGcmCipher

Available on crate feature encryption only.
Source§

impl SegmentCipher for XChaCha20Poly1305Cipher

Available on crate feature encryption only.