Skip to main content

EncryptionStream

Struct EncryptionStream 

Source
pub struct EncryptionStream<I> { /* private fields */ }
Expand description

Streaming encryption processor that handles data and produces encrypted chunks.

This processes incoming data bytes, splits them into chunks according to self-encryption rules, encrypts them, and makes them available through iteration. At the end, it applies shrinking and provides access to the final DataMap.

§Memory Efficiency

This implementation maintains minimal memory usage by:

  • Only buffering incomplete chunks (typically < 1MB)
  • Processing data sequentially as it arrives
  • Applying shrinking at the end

Implementations§

Source§

impl<I> EncryptionStream<I>

Source

pub fn chunks(&mut self) -> ChunkStream<'_, I>

Returns an iterator that yields only encrypted chunks.

This provides a clean interface for processing chunks without having to pattern match on the ChunkOrDataMap enum. After the returned iterator is consumed, the DataMap will be available via datamap().

§Examples
use self_encryption::stream_encrypt;
use bytes::Bytes;

let file_data = b"Hello, world!".repeat(1000);
let data_iter = file_data.chunks(1024).map(|chunk| Bytes::from(chunk.to_vec()));

let mut stream = stream_encrypt(file_data.len(), data_iter)?;

// Clean iteration over chunks only!
for chunk_result in stream.chunks() {
    let (_hash, _content) = chunk_result?;
    // println!("Got chunk {} with {} bytes", hex::encode(hash), content.len());
    // Store chunk directly to your backend
    // store(hash, content)?;
}

// Get the final DataMap
let _datamap = stream.datamap().expect("Should have DataMap after chunks iteration");
Source

pub fn datamap(&self) -> Option<&DataMap>

Returns the final DataMap after chunk iteration is complete.

This method should be called after the chunks() iterator has been fully consumed. Returns None if encryption is not yet complete.

§Examples
use self_encryption::stream_encrypt;
use bytes::Bytes;

let file_data = b"Hello, world!".repeat(1000);
let data_iter = file_data.chunks(1024).map(|chunk| Bytes::from(chunk.to_vec()));

let mut stream = stream_encrypt(file_data.len(), data_iter)?;

// Process all chunks
for chunk_result in stream.chunks() {
    let (_hash, _content) = chunk_result?;
    // Store chunk directly
    // store(hash, content)?;
}

// Get the DataMap
let _datamap = stream.datamap().expect("Should have DataMap");
Source

pub fn into_datamap(self) -> Option<DataMap>

Returns the final DataMap after chunk iteration is complete, consuming the stream.

This method should be called after the chunks() iterator has been fully consumed. Returns None if encryption is not yet complete.

§Examples
use self_encryption::stream_encrypt;
use bytes::Bytes;

let file_data = b"Hello, world!".repeat(1000);
let data_iter = file_data.chunks(1024).map(|chunk| Bytes::from(chunk.to_vec()));

let mut stream = stream_encrypt(file_data.len(), data_iter)?;

// Process all chunks
for chunk_result in stream.chunks() {
    let (_hash, _content) = chunk_result?;
    // Store chunk directly
    // store(hash, content)?;
}

// Get the DataMap (consuming the stream)
let _datamap = stream.into_datamap();

Returns the final DataMap, or None if encryption is not yet complete (i.e., chunks() was not fully consumed).

Auto Trait Implementations§

§

impl<I> !Freeze for EncryptionStream<I>

§

impl<I> RefUnwindSafe for EncryptionStream<I>
where I: RefUnwindSafe,

§

impl<I> Send for EncryptionStream<I>
where I: Send,

§

impl<I> Sync for EncryptionStream<I>
where I: Sync,

§

impl<I> Unpin for EncryptionStream<I>
where I: Unpin,

§

impl<I> UnsafeUnpin for EncryptionStream<I>
where I: UnsafeUnpin,

§

impl<I> UnwindSafe for EncryptionStream<I>
where I: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V