Skip to content

Instantly share code, notes, and snippets.

@moshest
Created June 25, 2015 16:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moshest/7d27848b2bbc45c40d67 to your computer and use it in GitHub Desktop.
Save moshest/7d27848b2bbc45c40d67 to your computer and use it in GitHub Desktop.
Node.js v0.12 ECDH Example
var ecdh = crypto.createECDH('secp256k1');
ecdh.generateKeys();
var publicKey = ecdh.getPublicKey(null, 'compressed');
var privateKey = ecdh.getPrivateKey(null, 'compressed');
console.log('Private1:', privateKey.length, privateKey.toString('hex'));
console.log('Public1: ', publicKey.length, publicKey.toString('hex'));
var ecdh2 = crypto.createECDH('secp256k1');
ecdh2.generateKeys();
var publicKey2 = ecdh2.getPublicKey(null, 'compressed');
var privateKey2 = ecdh2.getPrivateKey(null, 'compressed');
console.log('Private2:', privateKey2.length, privateKey2.toString('hex'));
console.log('Public2: ', publicKey2.length, publicKey2.toString('hex'));
var secret = ecdh.computeSecret(publicKey2);
console.log('Secret1: ', secret.length, secret.toString('hex'));
var secret2 = ecdh2.computeSecret(publicKey);
console.log('Secret2: ', secret2.length, secret2.toString('hex'));
// Private1: 32 82e02aa2caf42bbbe4668ca85bbd8f02203131724f2966ebb69386630249fbf1
// Public1: 33 02e81c206dda201e5afe677d87a033645e637ea1b7ba4291020c173ec4a7f99e91
// Private2: 32 bd9f7377ffdcac7d34f6edbd80eb7e7900c5bd079caefafd78148c5e04a96592
// Public2: 33 02a247b3753e0bcc34cbe56aee931e832f7376d12dffa8772e604c98b0bb6a77eb
// Secret1: 32 b97581cf2d9cf3ef0485385725cf0957ad4f8343c8c6d45dba1c239f581e735c
// Secret2: 32 b97581cf2d9cf3ef0485385725cf0957ad4f8343c8c6d45dba1c239f581e735c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment