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>
impl<I> EncryptionStream<I>
Sourcepub fn chunks(&mut self) -> ChunkStream<'_, I> ⓘ
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");Sourcepub fn datamap(&self) -> Option<&DataMap>
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");Sourcepub fn into_datamap(self) -> Option<DataMap>
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> 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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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