小编Sza*_*ron的帖子

从字符串转换后,Web Crypto Api 无法验证签名

我正在存储我的数据,包括字符串格式的签名,在按照 MDN 文档将签名与字符串相互转换后,验证过程每次都会失败。相关的代码位是:

const publicKey: CryptoKey = await window.crypto.subtle.importKey("spki", str2ab(window.atob(publicKeyStr)), { name: "RSA-PSS", hash: { name: "SHA-256" } }, false, ["verify"]);
const privateKey: CryptoKey = await window.crypto.subtle.importKey("pkcs8", str2ab(window.atob(privateKeyStr)), { name: "RSA-PSS", hash: { name: "SHA-256" } }, false, ["sign"])
    
if(privateKey && publicKey) {
    try {
        const sign = await window.crypto.subtle.sign({name: "RSA-PSS", saltLength: 32}, privateKey, encodedMessage);
        const signature = window.btoa(String.fromCharCode.apply(null, [...new Uint8Array(sign)])); //the way said signature is stored and I can load it
        const res = await window.crypto.subtle.verify({name: "RSA-PSS", saltLength: 32}, …
Run Code Online (Sandbox Code Playgroud)

javascript cryptography node.js typescript webcrypto-api

4
推荐指数
1
解决办法
939
查看次数