Skip to content

crypto: error in ECDH example #4296

Description

@jasnell

@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

  1. added
    cryptoIssues and PRs related to the crypto subsystem.
    docIssues and PRs related to Node.js documentation.
    on Dec 15, 2015
  2. self-assigned this
    on Dec 15, 2015
  3. shigeki commented on Dec 16, 2015

    @shigeki
    Contributor

    @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.

  4. jasnell commented on Dec 16, 2015

    @jasnell
    MemberAuthor

    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).

  5. jasnell commented on Dec 16, 2015

    @jasnell
    MemberAuthor

    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

  6. shigeki commented on Dec 16, 2015

    @shigeki
    Contributor

    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

Metadata

Metadata

Assignees

Labels

cryptoIssues and PRs related to the crypto subsystem.docIssues and PRs related to Node.js documentation.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions