use crypt_io::{Algorithm, Crypt};
fn main() -> Result<(), crypt_io::Error> {
let key = [0x42u8; 32];
let chacha = Crypt::new();
let plaintext = b"attack at dawn";
let ciphertext = chacha.encrypt(&key, plaintext)?;
let recovered = chacha.decrypt(&key, &ciphertext)?;
println!(
"ChaCha20-Poly1305: {:?}",
core::str::from_utf8(&recovered).unwrap()
);
assert_eq!(&*recovered, plaintext);
let aes = Crypt::aes_256_gcm();
assert_eq!(aes.algorithm(), Algorithm::Aes256Gcm);
let ciphertext = aes.encrypt(&key, plaintext)?;
let recovered = aes.decrypt(&key, &ciphertext)?;
println!(
"AES-256-GCM: {:?}",
core::str::from_utf8(&recovered).unwrap()
);
let aad = b"context:session:42";
let ciphertext = chacha.encrypt_with_aad(&key, plaintext, aad)?;
let recovered = chacha.decrypt_with_aad(&key, &ciphertext, aad)?;
println!(
"AEAD with AAD: {:?}",
core::str::from_utf8(&recovered).unwrap()
);
Ok(())
}