use chacha20poly1305::{ ChaCha20Poly1305, Key, Nonce, aead::{Aead, Generate, KeyInit}, }; type Result = std::result::Result>; mod helper { use super::{Generate, Key, Nonce}; pub fn gen_nonce() -> Nonce { Nonce::generate() } pub fn gen_key() -> Key { Key::generate() } } pub fn encrypt(key: &Key, plaintext: Vec) -> Result<(Nonce, Vec)> { let cipher = ChaCha20Poly1305::new(key); let nonce = helper::gen_nonce(); let ciphertext = cipher.encrypt(&nonce, plaintext.as_ref())?; Ok((nonce, ciphertext)) } pub fn decrypt(key: &Key, ciphertext: Vec, nonce: &Nonce) -> Result> { let cipher = ChaCha20Poly1305::new(key); let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())?; Ok(plaintext) } #[cfg(test)] mod tests { use super::*; use std::result::Result; #[test] fn encryption_decryption() -> Result<(), Box> { let key = helper::gen_key(); let plaintext = "I alone am the honored one.\n - Saturo Gojo."; let (nonce, ciphertext) = encrypt(&key, plaintext.into())?; let transformed = decrypt(&key, ciphertext, &nonce)?; assert_eq!(plaintext.as_bytes(), transformed); Ok(()) } }