Crate chacha20stream[][src]

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.2 AsyncWrite
  • explicit_clear - Explicitly clear in-memory buffer after operations.

Re-exports

pub use key::Key;
pub use key::IV;

Modules

key

Key and IV structures for the cipher

Structs

Sink

ChaCha Sink

Functions

keygen

Generate a random key and IV for the chacha20_poly1305 cipher