pub struct StreamEncryptor { /* private fields */ }stream only.Expand description
Streaming AEAD encryptor. Buffers caller-supplied plaintext into
fixed-size chunks, encrypts each chunk with a STREAM-construction
nonce, and emits ciphertext || tag per chunk.
Usage is symmetric with the super::StreamDecryptor:
- Construct with
StreamEncryptor::new. The constructor returns the encryptor and a 24-byte header — write this header to the output sink first. - Feed plaintext via
update. The method returns zero or more encrypted chunks (eachchunk_size + 16bytes) as buffer fills are reached. - Call
finalizeto emit any remaining buffered data as the final chunk. The final chunk is always emitted (even if zero plaintext bytes remain) and is always strictly smaller thanchunk_size + 16bytes, so the decryptor can detect it unambiguously by length.
§Example
use crypt_io::stream::{StreamDecryptor, StreamEncryptor};
use crypt_io::Algorithm;
let key = [0u8; 32];
let plaintext = b"the quick brown fox jumps over the lazy dog".repeat(1000);
// ---- Encrypt ----
let (mut enc, header) = StreamEncryptor::new(&key, Algorithm::ChaCha20Poly1305)?;
let mut wire = header.to_vec();
wire.extend(enc.update(&plaintext)?);
wire.extend(enc.finalize()?);
// ---- Decrypt ----
let mut dec = StreamDecryptor::new(&key, &wire[..24])?;
let mut recovered = dec.update(&wire[24..])?;
recovered.extend(dec.finalize()?);
assert_eq!(recovered, plaintext);Implementations§
Source§impl StreamEncryptor
impl StreamEncryptor
Sourcepub fn new(key: &[u8], algorithm: Algorithm) -> Result<(Self, [u8; 24])>
pub fn new(key: &[u8], algorithm: Algorithm) -> Result<(Self, [u8; 24])>
Construct a new stream encryptor with the default 64 KiB chunk size. Returns the encryptor plus the 24-byte header to be written to the output sink before any encrypted chunks.
§Errors
Error::InvalidKeyifkeyis not 32 bytes.Error::RandomFailureif the OS RNG cannot produce a nonce prefix.
Sourcepub fn new_with_chunk_size(
key: &[u8],
algorithm: Algorithm,
chunk_size_log2: u8,
) -> Result<(Self, [u8; 24])>
pub fn new_with_chunk_size( key: &[u8], algorithm: Algorithm, chunk_size_log2: u8, ) -> Result<(Self, [u8; 24])>
Construct with an explicit chunk size. chunk_size_log2 must
be in MIN_CHUNK_SIZE_LOG2..=MAX_CHUNK_SIZE_LOG2
(10..=24).
§Errors
See new, plus
Error::InvalidCiphertext
on out-of-range chunk size.
Sourcepub fn chunk_size(&self) -> usize
pub fn chunk_size(&self) -> usize
Chunk size in bytes used by this encryptor.
Sourcepub fn chunk_size_log2(&self) -> u8
pub fn chunk_size_log2(&self) -> u8
Log2 of the chunk size, as stored in the header.
Sourcepub fn update(&mut self, data: &[u8]) -> Result<Vec<u8>>
pub fn update(&mut self, data: &[u8]) -> Result<Vec<u8>>
Feed plaintext bytes. Returns zero or more complete encrypted
chunks (each chunk_size + 16 bytes) concatenated.
§Errors
Error::AuthenticationFailedon an upstream AEAD failure (unreachable in practice).
Sourcepub fn finalize_into(self, out: &mut Vec<u8>) -> Result<()>
pub fn finalize_into(self, out: &mut Vec<u8>) -> Result<()>
Zero-allocation finalize — appends the
final chunk to out instead of returning a new Vec. See
update_into.
§Errors
Same as finalize.