Skip to content

Instantly share code, notes, and snippets.

Last active October 5, 2021 16:21
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zfael/4c580566d90ee4db04c652372ad98c0d to your computer and use it in GitHub Desktop.
Node.JS - CRYPTO How to sign a file
//how to execute: node sign.js <path file> <path private key>
//output: signature of file
var crypto = require('crypto');
var fs = require('fs');
var args = process.argv.slice(2);
var fileName = args[0];
var keyPath = args[1];
//openssl genrsa -out key.pem 1024
var privatePem = fs.readFileSync(keyPath);
var key = privatePem.toString();
var sign = crypto.createSign('RSA-SHA256');
var buffer = fs.readFileSync(fileName);
var sig = sign.sign(key, 'hex');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment