我正在使用Node的Crypto的generateKeyPairSync,Sign和Verify,但是我无法使用这个简单的代码:verify.verify
总是输出false,即使它应该输出true。我想念什么?
const crypto = require('crypto');
const txt = 'Some text to sign';
// generates asymmetric key pair
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
}
});
// generates a signature object
const sign = crypto.createSign('sha256');
sign.update(txt);
// generates a verify object
const verify = crypto.createVerify('sha256');
verify.update(txt);
// should logs true, but logs false
console.log(
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64')
)
);
答案 0 :(得分:1)
您可以精确地将signature_format: "latin1" | "hex" | "base64"
作为verify.verify
的第三个参数。按预期返回true
:
verify.verify(
keys.publicKey,
sign.sign(keys.privateKey, 'base64'),
'base64'
)