我正在尝试使用 WebCrypto 通过 RSA-PSS 对令牌进行签名,但我不断收到错误消息:
DataError: Data provided to an operation does not meet requirements
Run Code Online (Sandbox Code Playgroud)
在crypto.subtle.importKey。
这是我的 JavaScript 代码:
function signToken(token, key) {
crypto.subtle.importKey(
'pkcs8',
PEM2Binary(key),
{
name: 'RSA-PSS',
hash: { name: 'SHA-256' },
},
false,
['sign']
).then(function(privKey){
crypto.subtle.sign(
'RSA-PSS',
privKey,
new TextEncoder().encode(token)
).then(function(signedToken){
msg = JSON.stringify({ intent: 'authenticate-token', signedToken: signedToken });
socket.send(msg);
})
}).catch(function(error){
console.error(error);
})
}
function PEM2Binary(pem) {
var encoded = '';
var lines = pem.split('\n');
for (var i = 0; i < lines.length; i++) …Run Code Online (Sandbox Code Playgroud)