0

I was unable to create a signature for private key ownership using Node's crypto module. How can I do so with the following public and private keys in hex:

const crypto = require("crypto");

const ecdh = crypto.createECDH("secp256k1");
ecdh.generateKeys();
const privateKey = ecdh.getPrivateKey("hex");
const publicKey = ecdh.getPublicKey("hex");
chandler
  • 71
  • 3
  • 9

2 Answers2

2

ECDH - is an algorithm for generating secrets for cryptographic algorithms.

You should compute secret which depends on another public key:

const secret = ecdh.computeSecret(anotherPublicKeyInHex, 'hex');

And use it in cipher/decipher:

crypto.createCipheriv('some-algorithm', secret, initialVector, options);
crypto.createDecipheriv('some-algorithm', secret, initialVector, options);

If you don't know, what you can do with that, don't use it. For first, you need to read more information about cryptographic algorithm, algorithms of generating secrets, etc.

https://en.wikipedia.org/wiki/Public-key_cryptography

https://en.wikipedia.org/wiki/Key_generation

https://en.wikipedia.org/wiki/Elliptic-curve_cryptography

1

ECDH is key agreement, not signature generation. DH means Diffie-Hellman after all.

Try another example such as the one found here for ECDSA (EC Digitial Signature Algorithm). Yes, the word DSA is missing from the example, but that's just crappy API design (sorry, no other word for it). Of course you'd need to replace the given curve name with "secp256k1"; I just tested that and it works.

Note that, in principle, the public and private keys are identical in form to the ones you'd have created, so again, in principle, you could use ECDH key pair generation and then use ECDSA as linked. It makes no sense as source code (and with the sparse documentation I cannot get it to work), but mathematically it should be possible.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263