Crate chacha20stream[−][src]
Expand description
chacha20_poly1305 stream wrapper
Contains a writable stream that wraps another, applying the chacha20_poly1305 cipher to the input before writing for either encryption or decryption.
Examples
Encrypt a message to an in-memory buffer.
// Generate random key and IV for the operations.
let (key, iv) = chacha20stream::keygen();
let input = "Hello world!";
let mut sink = Sink::encrypt(Vec::new(), key, iv).expect("Failed to create encryptor");
sink.write_all(input.as_bytes()).unwrap();
sink.flush().unwrap(); // `flush` also clears the in-memory buffer if there is left over data in it.
let output_encrypted = sink.into_inner();
Decrypting a message:
fn decrypt_message_to<W: Write + ?Sized>(output: &mut W, encrypted: &[u8], key: Key, iv: IV) -> io::Result<()>
{
let mut sink = Sink::decrypt(output, key, iv)?;
sink.write_all(&encrypted[..])?;
sink.flush().unwrap(); // `flush` also clears the in-memory buffer if there is left over data in it.
Ok(())
}
Features
- smallvec - Use
smallvec
crate to store the in-memory buffer on the stack if it’s smalle enough (default) - async - Enable
AsyncSink
with tokio 0.2AsyncWrite
- explicit_clear - Explicitly clear in-memory buffer after operations.
- serde - Enable
Key
andIV
to be de/serialised with Serde.