use anyhow::{Context, Result};
use pwsec::{ChachaB64, CipherB64};
fn main() -> Result<()> {
const PBKDF2_ROUNDS: u32 = 172_234;
let secret = b"this is some serialized form of the secret data";
let auth_tag = b"arbitrary, nonconfidential text";
let password = "LOIUo98zkjhB";
let storage = {
let cipher_b64 = ChachaB64::with_pbkdf2_rounds(PBKDF2_ROUNDS)
.encrypt_auth(secret, auth_tag, password)?;
(cipher_b64.to_string(), auth_tag.to_vec())
};
{
let (cipher, auth_tag) = storage;
let cipher_b64 = CipherB64::parse(&cipher).context("bad decrypt")?;
let decrypted_secret = ChachaB64::with_pbkdf2_rounds(PBKDF2_ROUNDS)
.decrypt_auth(cipher_b64, &auth_tag, password)?;
assert_eq!(*secret, *decrypted_secret);
}
Ok(())
}