I've read the documentation example (below), but the keys there are being generated, rather than imported from raw bytes.
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);
Using curve secp256k1, I want to create the ECDH shared secret from one 33-byte compressed public key and one 32-byte private key (from two different key pairs).
ecdh.computeSecret(otherPublicKey, ...)
looks like the correct method.
- How do I turn my 32-byte private key Buffer into an ECDH object on which I can call the method?
- Are a Buffer of 33 bytes an acceptable value for the
otherPublicKey
argument?