#io #async #aead #ciphering

async_io_crypto

A rust library for handling AsyncRead with ciphering / deciphering

1 unstable release

Uses new Rust 2021

0.1.2 Oct 14, 2022
0.1.1 Oct 14, 2022
0.1.0 Oct 14, 2022

#778 in Cryptography

MIT license

38KB
528 lines

async_io_crypto

A rust library for handling AsyncRead with ciphering / deciphering. It combines aead crate with tokio IO traits

Build status Rust


lib.rs:

Provides a wrapper around a AsyncRead

# use async_io_crypto::{CipherRead, DecipherRead};
# use tokio::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
# use chacha20poly1305::{ChaCha20Poly1305, Key, KeyInit};
# use rand::SeedableRng;

#[tokio::main]
# async fn main() -> Result<(), Box<dyn std::error::Error>> {

let key = b"my very super super secret key!!";
let plaintext = b"hello world!";

let mut csprng = rand_chacha::ChaCha20Rng::from_entropy();
let cipher: ChaCha20Poly1305 = ChaCha20Poly1305::new(Key::from_slice(key));
let (mut ci_reader, nonce) = CipherRead::new(&plaintext[..], cipher.clone(), &mut csprng);

let mut ciphered = Vec::new();
ci_reader.read_to_end(&mut ciphered).await.unwrap();

let mut deci_reader = DecipherRead::new(&ciphered[..], cipher, nonce.as_slice());
let mut deciphered = Vec::new();
deci_reader.read_to_end(&mut deciphered).await.unwrap();
assert_eq!(deciphered, plaintext);
# Ok(())
# }

Dependencies

~4MB
~65K SLoC