use aead::AeadCore;
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, aead::OsRng};
use colloid::cipher::{
detached,
non_detached::{in_place, non_in_place},
};
fn main() -> std::result::Result<(), chacha20poly1305::Error> {
let generated_key = ChaCha20Poly1305::generate_key(&mut OsRng);
let key: &[u8; 32] = generated_key.as_slice().try_into().unwrap();
let generated_nonce = ChaCha20Poly1305::generate_nonce(&mut OsRng);
let nonce: &[u8; 12] = generated_nonce.as_slice().try_into().unwrap();
let mut ciphertext = aead::bytes::BytesMut::from("We are cryptographic");
println!("Option: in-place en/de.");
in_place::encrypt(&key, &nonce, b"Hello", &mut ciphertext)?;
println!("{:?}", str::from_utf8(&ciphertext));
in_place::decrypt(&key, &nonce, b"Hello", &mut ciphertext)?;
println!("{:?}", ciphertext);
println!("Decrypted: {:?}", str::from_utf8(&ciphertext));
println!("Option: non-in-place en/de.");
let encrypted = non_in_place::encrypt(&key, &nonce, &mut ciphertext)?;
println!("Encryped: {:?}", str::from_utf8(&encrypted));
let decryped = non_in_place::decrypt(&key, &nonce, &encrypted)?;
println!("Decrypted: {:?}", str::from_utf8(&decryped));
println!("Option: in-place-detatached en/de.");
let tag = detached::encrypt(&key, &nonce, b"Crypto", &mut ciphertext)?;
println!("Tag: {:?}", tag);
println!("Encrypted: {:?}", ciphertext);
detached::decrypt(&key, &nonce, b"Crypto", &mut ciphertext, &tag)?;
println!("Decrypted: {:?}", ciphertext);
Ok(())
}