pub struct StreamEncryptor { /* private fields */ }Available on crate feature
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.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StreamEncryptor
impl RefUnwindSafe for StreamEncryptor
impl Send for StreamEncryptor
impl Sync for StreamEncryptor
impl Unpin for StreamEncryptor
impl UnsafeUnpin for StreamEncryptor
impl UnwindSafe for StreamEncryptor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more