Dalton Cézane
Dalton Cézane

Reputation: 3782

How to generate an ECDSA sign without need of PEM key format in NodeJS?

I have to generate an ECDSA sign and decode it after.

I tried with two different forms: one gave me a sign of size 135, while the other gave me a sign with size 70. The first one was generated with a pem key (with asn1js and bn modules as the answer in this link) and the second was generated with 'raw' key (eccrypto module), as we can seen below:

const ecdsa = require('eccrypto');
shaMsg = crypto.createHash('sha256').update(myData).digest();
ecdsa.sign(privateKey, shaMsg).then(function(sig) {
       console.log("Signature in DER format:", sig, '-------size: ', sig.length);
       ecdsa.verify(publicKey(), msg, sig).then(function() {
           console.log("Signature is OK");
       }).catch(function() {
           console.log("Signature is BAD");
       });
    });

The output for this code is:

Signature in DER format: < Buffer 30 44 02 20 56 5c 61 76 a7 17 3c 67 8d 04 54 dd d5 9a 81 3a e9 1f af a6 7e 2d 34 3e 78 78 47 fb 5e 8e 9c 79 02 20 78 a4 bb f6 20 41 7c 8c 59 1b 93 43 ... > -------size: 70

I need to decode my sign to make other operations. Can anyone help me with this?

Upvotes: 2

Views: 2276

Answers (1)

Dalton C&#233;zane
Dalton C&#233;zane

Reputation: 3782

As nobody helped, I did what I wanted with another module (elliptic):

var EC = require("elliptic").ec;
var ec = new EC("secp256k1");

var shaMsg = crypto.createHash("sha256").update(myData.toString()).digest();
var mySign = ec.sign(shaMsg, privateKey, {canonical: true});

I hope this helps someone else.

Upvotes: 3

Related Questions