使用 Web Cryptography API 和 RSASSA-PKCS1-v1_5 算法密钥验证签名失败。
- 将使用postgreSQL 中的sign 函数生成的签名保存为字符串:
window.crypto.subtle
.sign("RSASSA-PKCS1-v1_5", key, this.str2ab(hash))
.then(sigNature => {
const sig = this.ab2hex(sigNature); //save sig -> postgreSQL
});
- 我尝试使用我从 postgreSQL 获得的签名来验证:
window.crypto.subtle
.verify(
"RSASSA-PKCS1-v1_5",
key,
this.str2ab(_signature), //from pSQL
this.str2ab(_hash) //from pSQL
)
.then(isVerified => {
console.log(isVerified);
});
实用程序:* String -> ArrayBuffer 函数:
private str2ab(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
- ArrayBuffer -> HEX 函数:
private ab2hex(buffer) {
// buffer is an ArrayBuffer
return Array.prototype.map
.call(new Uint8Array(buffer), x => ("00" + x.toString(16)).slice(-2))
.join("");
}
问题是,即使我使用正确的密钥对进行签名和验证,我也总是得到“假”作为验证功能的响应