pub struct AesWriter<E: BlockEncryptor, W: Write> { /* private fields */ }Expand description
Wraps a Write implementation with CBC
based on given BlockEncryptor
§Examples
Write encrypted to a file.
let key: [u8; 16] = OsRng::new()?.gen();
let file = File::create("...")?;
let encryptor = AesSafe128Encryptor::new(&key);
let mut writer = AesWriter::new(file, encryptor)?;
writer.write_all("Hello World!".as_bytes())?;Encrypt in-memory.
let key: [u8; 16] = OsRng::new()?.gen();
let encryptor = AesSafe128Encryptor::new(&key);
let mut encrypted = Vec::new();
{
let mut writer = AesWriter::new(&mut encrypted, encryptor)?;
writer.write_all("Hello World!".as_bytes())?;
}Implementations§
Source§impl<E: BlockEncryptor, W: Write> AesWriter<E, W>
impl<E: BlockEncryptor, W: Write> AesWriter<E, W>
Sourcepub fn new(writer: W, enc: E) -> Result<AesWriter<E, W>>
pub fn new(writer: W, enc: E) -> Result<AesWriter<E, W>>
Creates a new AesWriter with a random IV.
The IV will be written as first block of the file.
§Parameters
- writer: Writer to write encrypted data into
- enc:
BlockEncryptorto use for encyrption
§Examples
let key: [u8; 16] = OsRng::new()?.gen();
let encryptor = AesSafe128Encryptor::new(&key);
let file = File::create("...")?;
let mut writer = AesWriter::new(file, encryptor)?;Trait Implementations§
Source§impl<E: BlockEncryptor, W: Write> Write for AesWriter<E, W>
impl<E: BlockEncryptor, W: Write> Write for AesWriter<E, W>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Encrypts the passed buffer and writes the result to the underlying writer.
Due to the blocksize of CBC not all data will be written instantaneously. For example if 17 bytes are passed, the first 16 will be encrypted as one block and written the underlying writer, but the last byte won’t be encrypted and written yet.
If flush has been called, this method will always return an error.
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Warning: When this method is called, the encryption will finish and insert final padding.
After calling flush, this writer cannot be written to anymore and will always return an
error.
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)