Skip to main content

basic/
basic.rs

1use ascon::{Aead128, Cxof128, Hash256};
2
3fn main() -> Result<(), ascon::Error> {
4    // Demonstration values only. Provision a secret key and a fresh nonce in use.
5    let cipher = Aead128::new(&[0x42; 16]);
6    let nonce = [0x01; 16];
7    let mut message = *b"Hello from Rust";
8    let tag = cipher.encrypt_in_place(&nonce, b"example", &mut message);
9    println!("Ciphertext: {message:02x?}\nTag: {tag:02x?}");
10    cipher.decrypt_in_place(&nonce, b"example", &mut message, &tag)?;
11    assert_eq!(&message, b"Hello from Rust");
12
13    let mut hash = Hash256::new();
14    hash.update(b"Hello ");
15    hash.update(b"from Rust");
16    println!("Hash: {:02x?}", hash.finalize());
17
18    let mut cxof = Cxof128::new(b"example protocol v1")?;
19    cxof.update(&message);
20    let mut reader = cxof.finalize();
21    let mut output = [0; 48];
22    reader.read(&mut output[..17]);
23    reader.read(&mut output[17..]);
24    println!("CXOF: {output:02x?}");
25    Ok(())
26}