feat: ecdh with openpgp card x25519
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
use ed25519_dalek::{Keypair, Signer, Verifier};
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use ed25519_dalek::{ Keypair, Signer, Verifier };
|
use x25519_dalek::{EphemeralSecret, PublicKey};
|
||||||
use x25519_dalek::{ EphemeralSecret, PublicKey };
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut csprng = OsRng{};
|
let mut csprng = OsRng {};
|
||||||
let keypair = Keypair::generate(&mut csprng);
|
let keypair = Keypair::generate(&mut csprng);
|
||||||
println!("Key pair: {:#?}", keypair);
|
println!("Key pair: {:#?}", keypair);
|
||||||
|
|
||||||
@@ -15,14 +17,29 @@ fn main() {
|
|||||||
println!("Verify result: {:?}", public_key.verify(message, &signature));
|
println!("Verify result: {:?}", public_key.verify(message, &signature));
|
||||||
|
|
||||||
// key exchange
|
// key exchange
|
||||||
|
println!("{}", "-".repeat(88));
|
||||||
let alice_secret = EphemeralSecret::new(OsRng);
|
let alice_secret = EphemeralSecret::new(OsRng);
|
||||||
let alice_public = PublicKey::from(&alice_secret);
|
let alice_public = PublicKey::from(&alice_secret);
|
||||||
let bob_secret = EphemeralSecret::new(OsRng);
|
let bob_secret = EphemeralSecret::new(OsRng);
|
||||||
let bob_public = PublicKey::from(&bob_secret);
|
let bob_public = PublicKey::from(&bob_secret);
|
||||||
|
|
||||||
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
|
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
|
||||||
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
|
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
|
||||||
|
|
||||||
|
println!("Alice public key : {}", hex::encode(alice_public.as_bytes()));
|
||||||
|
println!("Bob public key : {}", hex::encode(bob_public.as_bytes()));
|
||||||
|
|
||||||
println!("Alice shared secret: {}", hex::encode(alice_shared_secret.as_bytes()));
|
println!("Alice shared secret: {}", hex::encode(alice_shared_secret.as_bytes()));
|
||||||
println!("Bob shared secret: {}", hex::encode(bob_shared_secret.as_bytes()));
|
println!("Bob shared secret: {}", hex::encode(bob_shared_secret.as_bytes()));
|
||||||
|
|
||||||
|
println!("{}", "-".repeat(88));
|
||||||
|
let public_key_bytes = hex::decode("7FEBAAB0D80CED24730B613F3D86924560EBCF13A838DEBC065F63C69C24C61E").unwrap();
|
||||||
|
let public_key_bytes: [u8; 32] = public_key_bytes.try_into().unwrap();
|
||||||
|
let public_key_card = PublicKey::from(public_key_bytes);
|
||||||
|
println!("Public key card: {}", hex::encode(public_key_card.as_bytes()));
|
||||||
|
let new_secret = EphemeralSecret::new(OsRng);
|
||||||
|
let new_public = PublicKey::from(&new_secret);
|
||||||
|
println!("New public key: {}", hex::encode(new_public.as_bytes()));
|
||||||
|
let new_card_shared_secret = new_secret.diffie_hellman(&public_key_card);
|
||||||
|
println!("New&card shared secret: {}", hex::encode(new_card_shared_secret.as_bytes()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user