Torsten
Torsten

Reputation: 253

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!

Upvotes: 12

Views: 22202

Answers (1)

Bill
Bill

Reputation: 25565

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');

Upvotes: 28

Related Questions