私は以下のjavascriptを持っています。 ECDSAの公開鍵と秘密の鍵ペアを生成し、BASE64でエンコードされた公開鍵を文字列としてコンソールに出力する必要があります。私は各リロードで新しいキーを生成すると期待しています。しかし、それは常に同じものを常に印刷し、私はなぜそれが理解できません。常に同じ鍵を生成していますか?代わりに新しいキーを取得するために何をすればいいですか?Javascript crypto.subtle.generateKeyは引き続き同じキーを返しますか?
JSfiddle:私は自分の質問にお答えしますので、https://jsfiddle.net/35bk4maw/
window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-256", //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key)
{
window.crypto.subtle.exportKey(
"spki", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
key.publicKey //can be a publicKey or privateKey, as long as extractable was true
).then(function(keydata)
{
// this always prints something like "A21ixmVqdCBccnOheQJ1cmNlcl0="
// I would expect it to print different string on each reload!
console.log(btoa(keydata));
})
.catch(function(err){ console.error(err); });
}).catch(function(err){ console.error(err); });