-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyGeneration.js
22 lines (19 loc) · 948 Bytes
/
keyGeneration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export default async () => {
const keyPair = await window.crypto.subtle.generateKey(
{
name: "ECDH",
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)
["deriveKey", "deriveBits"] //can be any combination of "deriveKey" and "deriveBits"
);
const publicKeyJwk = await window.crypto.subtle.exportKey(
"jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
keyPair.publicKey //can be a publicKey or privateKey, as long as extractable was true
);
const privateKeyJwk = await window.crypto.subtle.exportKey(
"jwk", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
keyPair.privateKey //can be a publicKey or privateKey, as long as extractable was true
);
return { publicKeyJwk, privateKeyJwk };
};