Search code examples
node.jscryptographydigital-signature

Create signature with NodeJS's ECDH


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");

Solution

  • 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