1

As official doc

const crypto = require('crypto');
const sign = crypto.createSign('sha256');

sign.update('some data to sign');

const privateKey =
`-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49
AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2
pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
-----END EC PRIVATE KEY-----`;
console.log(sign.sign(privateKey).toString('hex'));

How can we generate the private key like the below format using crypto module?

`-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49
AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2
pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
-----END EC PRIVATE KEY-----`;

I trying to using createECDH getPrivateKey,but not the format I want.And using this format to sign will cause

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

  const crypto = require('crypto');
    const bob = crypto.createECDH('secp256k1');
    bob.generateKeys();

    const privateKey = bob.getPrivateKey()
    console.log(privateKey)

I was try to convert it using

const privateKey = bob.getPrivateKey().toString('base64');

and add comment to head and tail,make it look like this

`-----BEGIN EC PRIVATE KEY-----
pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==
-----END EC PRIVATE KEY-----`;

But this still not works

How can I generate an valid ECC pem in node.js using Crypto module?

  • Not sure if this helps or not. https://stackoverflow.com/questions/39499040/generating-ecdsa-signature-with-node-js-crypto – Samuel Toh Aug 08 '17 at 06:41
  • Yes,reading it before,but it using other library for generate key. – user7924038 Aug 08 '17 at 06:46
  • "I trying to using createECDH getPrivateKey,but not the format I want." That's a horrible error description. What format do you get and why doesn't it start with the PEM start line? – Maarten Bodewes Aug 08 '17 at 11:02
  • If you have try it,you know it will give you buffer type of the key and you can convert it to other type, but don't know how to create pem type key (-----BEGIN EC PRIVATE KEY----- .....) – user7924038 Aug 09 '17 at 01:54

0 Answers0