Danny Moerkerke
Danny Moerkerke

Reputation: 462

Web Crypto API: generate key pair and make only private key non-extractable

Is it possible with the Web Crypto API to generate a key pair and make only the private key non-extractable?

I want to safely store the private key so I make it non-extractable but I also want to share the public key.

The generateKey method of the SubtleCrypto object only allows to make both keys either extractable or non-extractable.

Is there any other way to accomplish this?

Upvotes: 3

Views: 960

Answers (1)

Andrew Parks
Andrew Parks

Reputation: 8087

The generateKey method of the SubtleCrypto object only allows to make both keys either extractable or non-extractable.

This is not true. It will only make the private key non-extractable.

(async ()=>{
let keyPair = await window.crypto.subtle.generateKey(
  {
    name: "ECDSA",
    namedCurve: "P-384"
  },
  false,
  ["sign", "verify"]
);

console.log(keyPair.privateKey.extractable) // false
console.log(keyPair.publicKey.extractable) // true
console.log(await window.crypto.subtle.exportKey('jwk', keyPair.publicKey))
})();

Upvotes: 3

Related Questions