Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: error in ECDH example #4296

Closed
jasnell opened this issue Dec 15, 2015 · 4 comments
Closed

crypto: error in ECDH example #4296

jasnell opened this issue Dec 15, 2015 · 4 comments
Assignees
Labels
crypto Issues and PRs related to the crypto subsystem. doc Issues and PRs related to the documentations.

Comments

@jasnell
Copy link
Member

jasnell commented Dec 15, 2015

@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);
@jasnell jasnell added crypto Issues and PRs related to the crypto subsystem. doc Issues and PRs related to the documentations. labels Dec 15, 2015
@jasnell jasnell self-assigned this Dec 15, 2015
@shigeki
Copy link
Contributor

shigeki commented Dec 16, 2015

@jasnell Are you running the example on Node-v4? #3511 included ECDH.genrateKeys() in ECDH.computeSecret() so that the example running on Node-v5 would have no error.

@jasnell
Copy link
Member Author

jasnell commented 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 https://github.com/jasnell Are you running the example on
Node-v4? #3511 #3511 included
ECDH.genrateKeys() in ECDH.computeSecret() so that the example running on
Node-v5 would have no error.


Reply to this email directly or view it on GitHub
#4296 (comment).

@jasnell
Copy link
Member Author

jasnell commented Dec 16, 2015

hmm.. wouldn't that also mean we could drop bob.generateKeys(); from the example also? sigh.. nevermind... I need to read the example better. sigh lol

@shigeki
Copy link
Contributor

shigeki commented Dec 16, 2015

It can be dropped. The example just shows that ECDH.generateKeys() can also be effective before invoking ECDH.comuputeSecret() and it's not harmful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem. doc Issues and PRs related to the documentations.
Projects
None yet
Development

No branches or pull requests

2 participants