Javascript 如何使用rsa公钥验证文件

Javascript 如何使用rsa公钥验证文件,javascript,node.js,rsa,node-crypto,Javascript,Node.js,Rsa,Node Crypto,我的工作是建立在 我正在尝试使用公钥验证文件。这是我的密码: var hash = crypto.createHash("sha256"); hash.setEncoding("hex"); var fd = fs.createReadStream("path/to/my/file"); fd.on("end", function() { hash.end(); var fileHash = hash.read(); const publicKey = fs.re

我的工作是建立在

我正在尝试使用公钥验证文件。这是我的密码:

var hash = crypto.createHash("sha256");
hash.setEncoding("hex");
var fd = fs.createReadStream("path/to/my/file");
fd.on("end", function() {
    hash.end();
    var fileHash = hash.read();    
    const publicKey = fs.readFileSync('keys/public_key.pem');
    const verifier = crypto.createVerify('RSA-SHA256');
    const testSignature = verifier.verify(publicKey, fileSignature, 'base64');
    console.log("testSignature: \n" + testSignature);
    if (testSignature === fileHash)
        console.log("ok");
    else
        console.log("not ok");
});
fd.pipe(hash);
我不知道这个代码是否正确,但是当我在控制台中打印它时,
testSignature
等于“false”。为什么?

testSignature:
false
加密的散列(
文件签名
变量)是正确的。base64字符串与我预期的相同

你知道我的代码有什么问题吗?谢谢

编辑

以下是生成签名的代码:

var hash = crypto.createHash("sha256");
hash.setEncoding("hex");
var fd = fs.createReadStream("path/to/file");
fd.on("end", function() {
    hash.end();
    var fileHash = hash.read();
    var privateKey = fs.readFileSync('keys/private_key.pem');
    var signer = crypto.createSign('RSA-SHA256');
    signer.update(fileHash);
    fileSignature = signer.sign(privateKey, 'base64');
});
fd.pipe(hash);

假设
path/to/my/file
是需要验证其内容的文件,则必须将其内容提供给
verifier.update()
。请尝试以下操作:

const input = fs.readFileSync('path/to/my/file'); // load data contents
const publicKey = fs.readFileSync('keys/public_key.pem').toString(); // load the signature, as a string!
const verifier = crypto.createVerify('RSA-SHA256');
verifier.update(input); // provide data contents to the verifier
const testSignature = verifier.verify(publicKey, fileSignature, 'base64');
console.log("testSignature: \n" + testSignature);
另外,请确保
文件签名
是字符串值,而不是字符串。出于某种原因(我仍在试图找出原因),如果将缓冲区对象传递给它,它将无法工作:

const fileSignatureBuffer = fs.readFileSync('signature.sha256');
const fileSignatureString = fileSignatureBuffer.toString();
// load public key, create the verifier, provide data contents to verifier, etc.
const testSignature = verifier.verify(publicKey, fileSignatureBuffer); // false
const testSignature = verifier.verify(publicKey, fileSignatureString, 'base64'); // true
编辑: 如果使用散列作为签名步骤的输入,则必须在验证步骤中传递相同的散列。然后代码将如下所示:

const publicKey = fs.readFileSync('keys/public_key.pem').toString(); // load the signature, as a string!
const verifier = crypto.createVerify('RSA-SHA256');
verifier.update(fileSignature); // provide the file signature to the verifier
const testSignature = verifier.verify(publicKey, fileSignature, 'base64');
console.log("testSignature: \n" + testSignature);

您在哪里使用openssl?(标签的作用是什么?)我刚刚重用了我提到的问题中的标签,实际上我使用openssl创建密钥。我已经更新了标记您使用的节点版本是什么?这些编辑会改变很多事情,并使我以前的注释过时。需要警惕的一件事是加密操作缺乏对称性。注意您在签名生成中如何调用
update()
,而不是在签名验证中?这是你做错了什么的线索
fileHash
根本没有使用。我刚刚绑定了它,但结果仍然是
false
fileSignature
是一个字符串,而不是缓冲区。我看到您是在对文件哈希进行签名,而不是对输入文件内容本身进行签名,因此您必须将哈希提供给
verify.update()
,而不是输入文件内容。