Repository navigation
Closed
Description
@nodejs/crypto ... In crypto.markdown, the example for ECDH.setPublicKey(public_key[, encoding]) gives an error. Documenting this as a todo to fix (note, the examples use the updated syntax being worked on in #4282)
const crypto = require('crypto');
const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');
// Note: This is a shortcut way to specify one of Alice's previous private
// keys. It would be unwise to use such a predictable private key in a real
// application.
alice.setPrivateKey(
crypto.createHash('sha256').update('alice', 'utf8').digest()
);
// Bob uses a newly generated cryptographically strong pseudorandom key pair
bob.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);
crypto.js:526
var key = this._handle.getPublicKey(f);
^
Error: You should generate ECDH keys first
at Error (native)
at ECDH.getPublicKey (crypto.js:526:26)
at Object.<anonymous> (/Users/james/tmp/test.js:17:48)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:134:18)
at node.js:961:3
Adding alice.generateKeys() allows the example to work.
const crypto = require('crypto');
const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');
// Note: This is a shortcut way to specify one of Alice's previous private
// keys. It would be unwise to use such a predictable private key in a real
// application.
alice.setPrivateKey(
crypto.createHash('sha256').update('alice', 'utf8').digest()
);
// Bob and Alice use a newly generated cryptographically strong pseudorandom key pair
bob.generateKeys();
alice.generateKeys();
const alice_secret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
const bob_secret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
// alice_secret and bob_secret should be the same shared secret value
console.log(alice_secret === bob_secret);
Activity
shigeki commentedon Dec 16, 2015
@jasnell Are you running the example on Node-v4? #3511 included
ECDH.genrateKeys()inECDH.computeSecret()so that the example running on Node-v5 would have no error.jasnell commentedon Dec 16, 2015
Aha! That's right. Forgot about that change!
On Dec 15, 2015 6:42 PM, "Shigeki Ohtsu" notifications@github.com wrote:
jasnell commentedon Dec 16, 2015
hmm.. wouldn't that also mean we could dropsigh.. nevermind... I need to read the example better. sigh lolbob.generateKeys();from the example also?shigeki commentedon Dec 16, 2015
It can be dropped. The example just shows that
ECDH.generateKeys()can also be effective before invokingECDH.comuputeSecret()and it's not harmful