Skip to content

Instantly share code, notes, and snippets.

@moughamir
Forked from ajinabraham/sign.js
Created July 29, 2019 19:30
Show Gist options
  • Save moughamir/bf0acb42624399a126387639191a8dd1 to your computer and use it in GitHub Desktop.
Save moughamir/bf0acb42624399a126387639191a8dd1 to your computer and use it in GitHub Desktop.
Node.js Digital Signature - Sign
//Create Private Key with OpenSSL
//openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -pkeyopt rsa_keygen_pubexp:3 -out privateKey.pem
//Generate Public Key to be used at the client side (Mobile)
//openssl pkey -in privateKey.pem -out publicKey.pem -pubout
const crypto = require('crypto')
const fs = require('fs')
const private_key = fs.readFileSync('digital_sign/privateKey.pem', 'utf-8')
//File to be signed
const package = fs.readFileSync('webpackage.zip')
//Signing
const signer = crypto.createSign('sha256');
signer.update(package);
signer.end();
const signature = signer.sign(private_key)
const buff = new Buffer(signature);
const base64data = buff.toString('base64');
console.log('Digital Signature: ' + base64data);
//Equivalent to openssl dgst -sha256 -sign digital_sign/privateKey.pem webpackage.zip | base64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment