major-mann
major-mann

Reputation: 2652

Node.js crypto Signer won't generate signature

I have the following code:

var crypto = require("crypto");
var fs = require("fs");
var path = require("path");

var signer = crypto.createSign("sha1");
signer.update("abc123");
var key = fs.readFileSync(path.join(__dirname,"sign.key"),"utf8");
var sig = signer.sign(key,"hex");

I have tried with "sha1", "sha", "sha256" (Which is the one I want), and "RSA-SHA256". No matter what I do, the "signer.sign" call always returns an empty string.

I have also tried all of these on 2 separate machines (OSX and Linux)

The private key is in the form: -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- And I know it is correct since I have successfully tested in with another system

Any ideas as to what I could be doing wrong

Upvotes: 2

Views: 765

Answers (2)

Sebastian Bromberg
Sebastian Bromberg

Reputation: 31

I think you can do this:

var key = fs.readFileSync(path.join(__dirname,"sign.key"),"binary");

Hope it works.

Upvotes: 0

major-mann
major-mann

Reputation: 2652

OK, I found the answer so I'm going to answer myself here in case anyone is looking it.

It seems the certificate file MUST be saved as ASCII.

Even if you specify ascii as the encoding, it will still not work.

I'm not sure exactly why this is, as I would expect V8 to normalize all strings into the same encoding (UTF-8?), but apparently not.

Upvotes: 2

Related Questions