我已阅读此document以通过 ECDH-CURVE25519 算法生成密钥对。但是当我在 window.crypto.subtle.generateKey中强化 ECDH-CURVE25519 作为算法名称时,抛出了JS错误( DOMException:算法:无法识别的名称)
window.crypto.subtle.generateKey(
{
name: "ECDH-CURVE25519"
},
true,
["deriveKey", "deriveBits"]
)
.then(function(key){
console.log(key);
pk = key.publicKey;
vk = key.privateKey;
})
.catch(function(err){
console.error(err);
});
答案 0 :(得分:3)
WebCryptographyApi不支持Curve25519。
可以使用P-256
(secp256r1),P-384
(secp386r1)和P-521
(secp521r1)。见https://www.w3.org/TR/WebCryptoAPI/#dfn-EcKeyGenParams
代码应该是这样的
window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256", // "P-256", "P-384", or "P-521"
},
true,
["deriveKey", "deriveBits"]
)
.then(function(key){
console.log(key);
pk = key.publicKey;
vk = key.privateKey;
})
.catch(function(err){
console.error(err);
});