我正在尝试使用nodejs crypto lib使用以下代码签名/验证信息字符串:
// Generated by CoffeeScript 1.8.0
(function() {
var crypto, tPriv, tPub1, tPub2, tSig, __Key, __Sign, __Verify;
crypto = require('crypto');
__Sign = function(aObj, aPrivKey) {
var tRet, tSign;
tSign = crypto.createSign('RSA-SHA256');
tSign.update(aObj);
tRet = tSign.sign(aPrivKey, 'base64');
console.log(tRet);
return tRet;
};
__Verify = function(aObj, aPubKey) {
var tRet, tVerify;
tVerify = crypto.createVerify('RSA-SHA256');
tRet = tVerify.verify(aObj, aPubKey, 'base64');
console.log(tRet);
return tRet;
};
__Key = crypto.getDiffieHellman('modp5');
__Key.generateKeys();
tPub1 = __Key.getPublicKey('base64');
tPub2 = __Key.getPublicKey('base64');
tPriv = __Key.getPrivateKey('base64');
tSig = __Sign(tPub1, tPriv);
console.log(__Verify(tSig, tPub2));
}).call(this);
但是,运行时会出现以下错误:
# 140735266407184:error:0906D06C:PEM routines:PEM_read_bio:no start line:../deps/openssl/openssl/crypto/pem/pem_lib.c:703:Expecting: ANY PRIVATE KEY#
#
# crypto.js:398
# var ret = this._binding.sign(toBuf(key));
# ^
# Error: SignFinal error
# at Sign.sign (crypto.js:398:27)
# at __Sign (~/testSig.js:11:18)
# at Object.<anonymous> (~/testSig.js:34:10)
# at Object.<anonymous> (~/testSig.js:38:4)
# at Module._compile (module.js:456:26)
# at Object.Module._extensions..js (module.js:474:10)
# at Module.load (module.js:356:32)
# at Function.Module._load (module.js:312:12)
# at Function.Module.runMain (module.js:497:10)
# at startup (node.js:119:16)
我已阅读了文档@ http://nodejs.org/api/crypto.html#crypto_crypto_createsign_algorithm,但老实说,我甚至不知道从哪里开始这个错误。我认为它抱怨私钥,但我给它的私钥来自它自己的库。
如果有人能指出我正确的方向,我将不胜感激。