const crypto = require('crypto');
// Alice creates an ECDH instance and generates keys
console.log('Alice: Creating ECDH instance...');
const alice = crypto.createECDH('prime256v1');
alice.generateKeys();
// Bob creates an ECDH instance and generates keys
console.log('Bob: Creating ECDH instance...');
const bob = crypto.createECDH('prime256v1');
bob.generateKeys();
// Exchange public keys (over an insecure channel)
console.log('Exchanging public keys...');
const alicePublicKey = alice.getPublicKey();
const bobPublicKey = bob.getPublicKey();
// Alice computes the shared secret using Bob's public key
console.log('Alice: Computing shared secret...');
const aliceSecret = alice.computeSecret(bobPublicKey);
// Bob computes the shared secret using Alice's public key
console.log('Bob: Computing shared secret...');
const bobSecret = bob.computeSecret(alicePublicKey);
// Both secrets should be the same
console.log('\nKey Exchange Results:');
console.log('='.repeat(50));
console.log("Alice's public key (first 20 bytes):", alicePublicKey.toString('hex').substring(0, 40) + '...');
console.log("Bob's public key (first 20 bytes): ", bobPublicKey.toString('hex').substring(0, 40) + '...');
console.log("\nAlice's secret (first 20 bytes):", aliceSecret.toString('hex').substring(0, 40) + '...');
console.log("Bob's secret (first 20 bytes): ", bobSecret.toString('hex').substring(0, 40) + '...');
console.log('\nDo the secrets match?', aliceSecret.equals(bobSecret));
console.log('Secret length:', aliceSecret.length, 'bytes');