use { crypto_secretbox::{aead::Aead, KeyInit, XSalsa20Poly1305}, ed25519_dalek::SigningKey, edx::{DeriveFactor, EdXchangeable}, }; const NONCE_SIZE: usize = XSalsa20Poly1305::NONCE_SIZE; const MESSAGES: &[&str] = &["hello, world", "你好,世界", "Bonjour, monde", "Hallo Welt", "مرحبا بالعالم"]; fn main() { let message = MESSAGES[rand::random::() % MESSAGES.len()]; println!("message : \"{}\"", message); let alice_sign = SigningKey::from_bytes(&rand::random()); let alice_verify = alice_sign.verifying_key(); println!("alice signing key : {}", hex::encode(alice_sign.as_bytes())); println!("alice verifying key : {}", hex::encode(alice_verify.as_bytes())); let bob_sign = SigningKey::from_bytes(&rand::random()); let bob_verify = bob_sign.verifying_key(); println!("bob signing key : {}", hex::encode(bob_sign.as_bytes())); println!("bob verifying key : {}", hex::encode(bob_verify.as_bytes())); let d_factor_bytes: [u8; 16] = rand::random(); let d_factor = DeriveFactor::hashed(d_factor_bytes); println!("derive factor : {}", hex::encode(d_factor_bytes)); let alice_kex_secret = alice_sign.edxchange(&bob_verify, &d_factor); let alice_cipher = XSalsa20Poly1305::new(alice_kex_secret.as_bytes().into()); let ciphertext = alice_cipher.encrypt(d_factor.0[..NONCE_SIZE].into(), message.as_bytes()).unwrap(); println!("alice kex secret : {}", hex::encode(alice_kex_secret.as_bytes())); println!("ciphertext (hex) : {}", hex::encode(&ciphertext)); println!("ciphertext (base32) : \"{}\"", base32::encode(base32::Alphabet::Crockford, &ciphertext).to_ascii_lowercase()); let bob_kex_secret = bob_sign.edxchange(&alice_verify, &d_factor); let bob_cipher = XSalsa20Poly1305::new(bob_kex_secret.as_bytes().into()); let plaintext = bob_cipher.decrypt(d_factor.0[..NONCE_SIZE].into(), ciphertext.as_ref()).unwrap(); println!("bob kex secret : {}", hex::encode(bob_kex_secret.as_bytes())); println!("plaintext : \"{}\"", String::from_utf8_lossy(&plaintext)); assert_eq!(plaintext, message.as_bytes()); }