Writer

Struct Writer 

Source
pub struct Writer<W, const CHUNK: usize> { /* private fields */ }
Available on crate features allow-non-fips and std only.
Expand description

A wrapper for any implementor of std::io::Write.

Writer implements std::io::Write and takes a child which also implements this trait. This type can wrap any writer, and ensure all data passed to said writer is encrypted.

Implementations§

Source§

impl<W, const CHUNK: usize> Writer<W, CHUNK>

Source

pub const fn new(chacha: ChaCha20<Streaming>, writer: W) -> Result<Self, W>

Creates a new Writer instance.

§Arguments
  • chacha - The ChaCha20 instance in streaming mode.
  • writer - The underlying writer.
§Returns

A new Writer instance.

§Errors

If the size of CHUNK is greater than u32::MAX

Source

pub fn finish(self) -> ChaCha20<NeedsIv>

Finishes the streaming encryption and returns to the NeedsIv state.

§Returns

A ChaCha20 instance in the NeedsIv state.

Methods from Deref<Target = ChaCha20<Streaming>>§

Source

pub fn decrypt_into( &mut self, cipher: &[u8], plain: &mut [u8], ) -> Result<(), Unspecified>

Decrypts the ciphertext into the output buffer.

§Arguments
  • cipher - The ciphertext to decrypt.
  • plain - The buffer to store the decrypted data.
§Errors
  • If the length of cipher is greater than u32::MAX.
  • If the length of cipher is greater than the length of plain.
Source

pub fn decrypt_in_place<'io>( &mut self, in_out: &'io mut [u8], ) -> Result<&'io [u8], Unspecified>

Decrypts the ciphertext in-place.

§Arguments
  • in_out - The plaintext to decrypt in place.
§Errors

If the length of in_out is greater than u32::MAX.

§Returns

The in_out argument, decrypted, for convenience. This can be ignored.

Source

pub fn decrypt_into_sized<const I: usize, const O: usize>( &mut self, cipher: &[u8; I], plain: &mut [u8; O], ) -> Result<(), Unspecified>

Decrypts the ciphertext into the output buffer with compile-time safety checks.

§Arguments
  • cipher - The ciphertext array to decrypt.
  • plain - The array to store the decrypted data.
§Errors
  • If the length of cipher is greater than u32::MAX.
  • If the length of cipher is greater than the length of plain.
Source

pub fn decrypt_in_place_sized<'io, const C: usize>( &mut self, in_out: &'io mut [u8; C], ) -> Result<&'io [u8; C], Unspecified>

Decrypts the ciphertext in-place with compile-time safety checks.

§Arguments
  • in_out - The ciphertext to decrypt in-place.
§Returns

ChaCha20 instance in the NeedsIv state.

§Errors
  • The length of in_out was greater than u32::MAX.
Source

pub fn decrypt_into_exact<const C: usize>( &mut self, cipher: &[u8; C], plain: &mut [u8; C], ) -> Result<(), Unspecified>

Decrypts the ciphertext into the output buffer with exact sizes.

§Arguments
  • cipher - The ciphertext array to decrypt.
  • plain - The array to store the decrypted data.
§Errors

If C (the length of cipher and plain) is greater than u32::MAX.

Source

pub fn decrypt(&mut self, cipher: &[u8]) -> Result<Vec<u8>, Unspecified>

Available on crate feature alloc only.

Decrypts the ciphertext and returns the plaintext as a vector.

§Arguments
  • cipher - The ciphertext to decrypt.
§Errors

If the length of cipher is greater than u32::MAX.

§Returns

A newly allocated buffer, the same length as cipher, containing the decrypted plaintext.

Source

pub fn decrypt_exact<const O: usize>( &mut self, cipher: &[u8; O], ) -> Result<[u8; O], Unspecified>

Decrypts the ciphertext array and returns the plaintext array.

§Arguments
  • cipher - The ciphertext array to decrypt.
§Errors

If O (the length of cipher) is greater than u32::MAX.

Source

pub fn encrypt_into( &mut self, plain: &[u8], cipher: &mut [u8], ) -> Result<(), Unspecified>

Encrypts the input into the output buffer in streaming mode.

§Arguments
  • plain - The input to encrypt.
  • cipher - The buffer to store the encrypted data.
§Errors
  • The length of cipher is less than the length of plain.
  • The length of plain is greater than u32::MAX.
Source

pub fn encrypt_into_sized<const I: usize, const O: usize>( &mut self, plain: &[u8; I], cipher: &mut [u8; O], ) -> Result<(), Unspecified>

Encrypts the input into the output buffer in streaming mode with compile-time size checking.

§Arguments
  • plain - The input array to encrypt.
  • cipher - The array to store the encrypted data.
§Errors
  • The length of plain was greater than u32::MAX.
  • The length of cipher was less than the length of plain.
Source

pub fn encrypt_into_exact<const C: usize>( &mut self, input: &[u8; C], output: &mut [u8; C], ) -> Result<(), Unspecified>

Encrypts the input into the output buffer in streaming mode with exact sizes.

§Type Parameters
  • C - The size of both the input and output arrays.
§Arguments
  • input - The input array to encrypt.
  • output - The array to store the encrypted data.
§Errors

C (the length of input and output) was greater than u32::MAX.

Trait Implementations§

Source§

impl<W, const CHUNK: usize> Deref for Writer<W, CHUNK>

Source§

type Target = ChaCha20<Streaming>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<W, const CHUNK: usize> DerefMut for Writer<W, CHUNK>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<W: Write, const CHUNK: usize> Write for Writer<W, CHUNK>

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Encrypts and writes the given buffer.

§Arguments
  • buf - The buffer to encrypt and write.
§Returns

The number of bytes written on success, or an io::Error on failure.

§Note

The maximum number of bytes this can write in a single invocation is capped by the CHUNK size. If this is not desirable, please consider using the write_all implementation.

Source§

fn write_all(&mut self, buf: &[u8]) -> Result<()>

Encrypts and writes the entire buffer.

§Arguments
  • buf - The buffer to encrypt and write.
§Returns

Ok(()) on success, or an io::Error on failure.

Source§

fn flush(&mut self) -> Result<()>

Flushes the underlying writer.

§Returns

Propagates the result of invoking flush for the underlying writer

1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<W, const CHUNK: usize> Freeze for Writer<W, CHUNK>
where W: Freeze,

§

impl<W, const CHUNK: usize> RefUnwindSafe for Writer<W, CHUNK>
where W: RefUnwindSafe,

§

impl<W, const CHUNK: usize> Send for Writer<W, CHUNK>
where W: Send,

§

impl<W, const CHUNK: usize> Sync for Writer<W, CHUNK>
where W: Sync,

§

impl<W, const CHUNK: usize> Unpin for Writer<W, CHUNK>
where W: Unpin,

§

impl<W, const CHUNK: usize> UnwindSafe for Writer<W, CHUNK>
where W: 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.