Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NodeJS crypto to sign a file?

I want to use nodeJS to sign a file. I got one p12 certificate (which includes the private key), a passphrase and a pem certificate.

This here shows how it is been done in ruby: https://gist.github.com/de4b602a213b4b264706

Thanks in advance!

like image 750
Torsten Avatar asked Oct 02 '12 01:10

Torsten


People also ask

How do I sign a string in node JS?

sign() is used to create signature of data. Parameters: This function accepts the following parameters: algorithm: It is a string-type value. A signature can be created by applying the name of signature algorithms, like 'SHA256', in place of a digest algorithm.

What does crypto do in NodeJS?

The node:crypto module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions.


1 Answers

You should be able to use createSign in the crypto module (see http://nodejs.org/docs/v0.4.2/api/all.html#crypto) to do what you want. The code will end up looking something like this (from http://chimera.labs.oreilly.com/books/1234000001808/ch05.html#chap7_id35952189):

var crypto = require('crypto');
var fs = require('fs');

var pem = fs.readFileSync('key.pem');
var key = pem.toString('ascii');

var sign = crypto.createSign('RSA-SHA256');
sign.update('abcdef');  // data from your file would go here
var sig = sign.sign(key, 'hex');
like image 174
Bill Avatar answered Sep 27 '22 23:09

Bill