use ever_crypto::{
XChaChaCrypto, XChaChaKey, XChaChaNonce,
KyberCrypto,
Result,
};
fn main() -> Result<()> {
println!("=== Ever Crypto Library Example ===\n");
println!("1. XChaCha20Poly1305 Encryption");
xchacha_example()?;
println!("\n2. Kyber1024 Key Encapsulation");
kyber_example()?;
println!("\n3. Hybrid Encryption (Kyber1024 + XChaCha20Poly1305)");
hybrid_example()?;
println!("\n=== All examples completed successfully! ===");
Ok(())
}
fn xchacha_example() -> Result<()> {
let key = XChaChaKey::generate();
let nonce = XChaChaNonce::generate();
let plaintext = b"Hello, World! This is a secret message.";
let aad = b"Additional authenticated data";
println!(" Original message: {:?}", std::str::from_utf8(plaintext).unwrap());
println!(" Key size: {} bytes", key.as_bytes().len());
println!(" Nonce size: {} bytes", nonce.as_bytes().len());
let ciphertext = XChaChaCrypto::encrypt(&key, &nonce, plaintext, Some(aad))?;
println!(" Ciphertext size: {} bytes", ciphertext.len());
let decrypted = XChaChaCrypto::decrypt(&key, &nonce, &ciphertext, Some(aad))?;
println!(" Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
assert_eq!(plaintext, &decrypted[..]);
println!(" ✓ Encryption/Decryption successful!");
Ok(())
}
fn kyber_example() -> Result<()> {
let alice_keypair = KyberCrypto::generate_keypair();
println!(" Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
println!(" Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
println!(" Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
println!(" Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
println!(" Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
println!(" ✓ Key encapsulation successful!");
let sizes = KyberCrypto::sizes();
println!(" Component sizes:");
println!(" - Public key: {} bytes", sizes.public_key);
println!(" - Secret key: {} bytes", sizes.secret_key);
println!(" - Ciphertext: {} bytes", sizes.ciphertext);
println!(" - Shared secret: {} bytes", sizes.shared_secret);
Ok(())
}
fn hybrid_example() -> Result<()> {
println!(" Setting up hybrid encryption...");
let alice_keypair = KyberCrypto::generate_keypair();
let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
println!(" Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
&xchacha_key,
plaintext,
None,
)?;
println!(" Hybrid encryption complete!");
println!(" - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
println!(" - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
println!(" - XChaCha ciphertext size: {} bytes", ciphertext.len());
println!(" Decrypting...");
let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
println!(" Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
assert_eq!(plaintext, &decrypted[..]);
println!(" ✓ Hybrid encryption/decryption successful!");
Ok(())
}