Struct aesstream::AesWriter
[−]
[src]
pub struct AesWriter<E: BlockEncryptor, W: Write> { /* fields omitted */ }
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())?; }
Methods
impl<E: BlockEncryptor, W: Write> AesWriter<E, W>
[src]
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:
BlockEncryptor
to 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
impl<E: BlockEncryptor, W: Write> Write for AesWriter<E, W>
[src]
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.
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.
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0
Attempts to write an entire buffer into this write. Read more
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0
Writes a formatted string into this writer, returning any error encountered. Read more
fn by_ref(&mut self) -> &mut Self
1.0.0
Creates a "by reference" adaptor for this instance of Write
. Read more
impl<E: BlockEncryptor, W: Write> Drop for AesWriter<E, W>
[src]
fn drop(&mut self)
Drops this AesWriter trying to finish encryption and to write everything to the underlying writer.