JavaScript Crypto Libraries


Cryptography in JavaScript

  • ECDSA with elliptic.js and js-sha3

ECDSA in JavaScript: Generate / Load Keys

npm install elliptic
npm install js-sha3


let elliptic = require('elliptic');
let sha3 = require('js-sha3');
let ec = new'secp256k1');

// let keyPair = ec.genKeyPair(); // Generate random keys
let keyPair = ec.keyFromPrivate(
let privKey = keyPair.getPrivate("hex");
let pubKey = keyPair.getPublic();
console.log(`Private key: ${privKey}`);
console.log("Public key :", pubKey.encode("hex").substr(2));
console.log("Public key (compressed):",

Run the above code example:

ECDSA in JavaScript: Sign Message

let msg = 'Message for signing';
let msgHash = sha3.keccak256(msg);
let signature = 
  ec.sign(msgHash, privKey, "hex", {canonical: true});

console.log(`Msg: ${msg}`);
console.log(`Msg hash: ${msgHash}`);
console.log("Signature:", signature);

Complete example:

ECDSA in JavaScript: Verify Signature

let hexToDecimal = (x) => ec.keyFromPrivate(x, "hex")
let pubKeyRecovered = ec.recoverPubKey(
  hexToDecimal(msgHash), signature,
  signature.recoveryParam, "hex");
console.log("Recovered pubKey:",
let validSig = ec.verify(
  msgHash, signature, pubKeyRecovered);
console.log("Signature valid?", validSig);

Complete example:

results matching ""

    No results matching ""