Skip to main content

StreamEncryptor

Struct StreamEncryptor 

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

  1. Construct with StreamEncryptor::new. The constructor returns the encryptor and a 24-byte header — write this header to the output sink first.
  2. Feed plaintext via update. The method returns zero or more encrypted chunks (each chunk_size + 16 bytes) as buffer fills are reached.
  3. Call finalize to 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 than chunk_size + 16 bytes, 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

Source

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
Source

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.

Source

pub fn chunk_size(&self) -> usize

Chunk size in bytes used by this encryptor.

Source

pub fn chunk_size_log2(&self) -> u8

Log2 of the chunk size, as stored in the header.

Source

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
Source

pub fn finalize(self) -> Result<Vec<u8>>

Flush remaining buffered plaintext as the final chunk. Always emits at least 16 bytes (the AEAD tag), so the receiver sees an unambiguous “final” frame.

§Errors

Same as update.

Trait Implementations§

Source§

impl Debug for StreamEncryptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.