Function x25519_dalek::x25519

source ·
pub fn x25519(k: [u8; 32], u: [u8; 32]) -> [u8; 32]
Expand description

The bare, byte-oriented x25519 function, exactly as specified in RFC7748.

This can be used with X25519_BASEPOINT_BYTES for people who cannot use the better, safer, and faster ephemeral DH API.

§Example

use rand_core::OsRng;
use rand_core::RngCore;

use x25519_dalek::x25519;
use x25519_dalek::StaticSecret;
use x25519_dalek::PublicKey;

// Generate Alice's key pair.
let alice_secret = StaticSecret::random_from_rng(&mut OsRng);
let alice_public = PublicKey::from(&alice_secret);

// Generate Bob's key pair.
let bob_secret = StaticSecret::random_from_rng(&mut OsRng);
let bob_public = PublicKey::from(&bob_secret);

// Alice and Bob should now exchange their public keys.

// Once they've done so, they may generate a shared secret.
let alice_shared = x25519(alice_secret.to_bytes(), bob_public.to_bytes());
let bob_shared = x25519(bob_secret.to_bytes(), alice_public.to_bytes());

assert_eq!(alice_shared, bob_shared);