[ Log On ]
  • Home
  • Tst
  • Cha
  • Enc
  • Code
  • IP
  • Fun
  • Sub
  • DigF
  • Cis
  • Com
  • Db
  • About
  • Netsim

ECDSA with node.js

[Back] We can use node.js to implement ECDSA for signatures. In this case we use the secp256k1 curve, and which is used with Bitcoins.

Parameters

Message:


Theory

Signatures are at the core of our world of trust. For centuries we have used them as a legal way of proving our identity and our confirmation of something. The ECDSA method with secp256k1 is used in the Bitcoin network. A sample run is:

Message: Hello

Private key: Key priv: ea596088f7006acb2d8f664805481d607dfd56b8a932574261f05c70b16b36be pub: EC Point x: ec3b97d6f2640e549f17a7ca9cac0cc915d4800fa3aa54b9086534c8b8ea0eb1 y: d2cb026c1d1b6360ded1ec52e5dd83e26f082beae2ec0f050ba7de4fb616b2d8

Public key: EC Point x: ec3b97d6f2640e549f17a7ca9cac0cc915d4800fa3aa54b9086534c8b8ea0eb1 y: d2cb026c1d1b6360ded1ec52e5dd83e26f082beae2ec0f050ba7de4fb616b2d8

Signature: Signature {
  r: BN: 7ace4c7eabfabbcdc3c9a9ec9252e5ac69a9753ec21fc566717b8e5868b9f86f,
  s: BN: 595ceee6ab7cb352c4b1c2a979186240621a646674ca56f1ded6fbf5b57d24fa,
  recoveryParam: 1 }
Signature verified: true

In this case we have 256-bit private key (ea5 ... 6be), and produce a 512-bit public key (ec3b9...).

The following is the code [code]:

String.prototype.getBytes = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes;
};

var m="hello";

var EC = require('elliptic').ec;

var ec = new EC('secp256k1');

var key = ec.genKeyPair();
var publicKey = key.getPublic(false);

console.log("Message:",m);
console.log("\nPrivate key:",key);
console.log("\nPublic key:",publicKey);



var msgHash = m.getBytes();

var signature = key.sign(msgHash);

console.log("\nSignature:",signature);
// Export DER encoded signature in Array
var derSign = signature.toDER();

// console.log(derSign);

// Verify signature
console.log("Signature verified:",key.verify(msgHash, derSign));