WebCrypto 中的 Curve25519 ECDH

Chi*_*nta 2 javascript cryptography webcrypto-api

我已阅读此文档以通过ECDH-CURVE25519算法生成密钥对。但是当我在window.crypto.subtle.generateKey中指定ECDH-CURVE25519作为算法名称时,会抛出JS 错误(DOMException: Algorithm: Unrecognized name ) 。

    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);
});
Run Code Online (Sandbox Code Playgroud)

ped*_*ofb 6

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);
});
Run Code Online (Sandbox Code Playgroud)