Get your own Node server
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');

              
Alice: Creating ECDH instance...
Bob: Creating ECDH instance...
Exchanging public keys...
Alice: Computing shared secret...
Bob: Computing shared secret...

Key Exchange Results:
==================================================
Alice's public key (first 20 bytes): 04a1b2c3d4e5f67890a1b2c3d4e5f67890a1b2c3d4...
Bob's public key (first 20 bytes):   04b2a1c3d4e5f67890a1b2c3d4e5f67890a1b2c3d4...

Alice's secret (first 20 bytes): 1a2b3c4d5e6f7890abcdeffedcba9876543210...
Bob's secret (first 20 bytes):   1a2b3c4d5e6f7890abcdeffedcba9876543210...

Do the secrets match? true
Secret length: 32 bytes