Mulan
Mulan

Reputation: 135396

How to use crypto.createSign with a DiffieHellman private key in node.js?

I'm trying to use a DH private key to sign a message with the node crypto lib. I'm running into an error that I can't seem to fix :{

var crypto = require("crypto");

var bob = crypto.getDiffieHellman("modp17");

bob.generateKeys();

var sign = crypto.createSign("RSA-SHA256");

sign.write("hello world");

var message = sign.sign(bob.getPrivateKey());

Error

140735140705040:error:0906D06C:PEM routines:PEM_read_bio:no start line:../deps/openssl/openssl/crypto/pem/pem_lib.c:703:Expecting: ANY PRIVATE KEY
Error: SignFinal error
    at Sign.sign (crypto.js:398:27)
    at repl:1:18
    at REPLServer.self.eval (repl.js:110:21)
    at repl.js:249:20
    at REPLServer.self.eval (repl.js:122:7)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)

Upvotes: 2

Views: 1470

Answers (2)

Dalton C&#233;zane
Dalton C&#233;zane

Reputation: 3782

You can put the private key in PEM format following the answer of @mscdex in this link:

Unable to sign a file with nodejs crypto

You will need the asn1.js and bn.js modules.

I hope this can help someone.

Upvotes: 0

mscdex
mscdex

Reputation: 106746

The private key you get from getPrivateKey() is not in PEM format, which is what sign() is expecting.

Upvotes: 2

Related Questions