【发布时间】:2023-03-10 22:04:01
【问题描述】:
我使用 node.js 玩数字签名。出于测试目的,我创建了一些 XML 数据的数字签名,首先仅使用 SHA256,然后使用 RSA-SHA256。
让我困惑的是两种签名方法都创建了完全相同的签名。两个签名是相同的。如果它们相同,那么为什么要使用两种不同的方法(SHA256 与 RSA-SHA256)?
我在下面包含代码:
var crypto = require('crypto'),
path = require('path'),
fs = require('fs'),
pkey_path = path.normalize('private_key.pem'),
pkey = '';
function testSignature(pkey) {
var sign1 = crypto.createSign('RSA-SHA256'),
sign2 = crypto.createSign('SHA256');
fs.ReadStream('some_document.xml')
.on('data', function (d) {
sign1.update(d);
sign2.update(d);
})
.on('end', function () {
var s1 = sign1.sign(pkey, "base64"),
s2 = sign2.sign(pkey, "base64");
console.log(s1);
console.log(s2);
});
}
// You need to read private key into a string and pass it to crypto module.
// If the key is password protected, program execution will stop and
// a prompt will appear in console, awaiting input of password.
testSignature(fs.readFileSync(pkey_path));
上面的代码输出一些字符串,即签名,然后再次输出完全相同的字符串,这也是相同数据的签名,但使用 - 据说 - 不同的算法创建,但它与前一个相同.. .
【问题讨论】:
-
@jweyrich 不幸的是它不相关。
标签: node.js rsa digital-signature sha256