This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
* Some parts of this feature may have varying levels of support.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
The generateKey() method of the SubtleCrypto interface is used to generate a new key (for symmetric algorithms) or key pair (for public-key algorithms).
generateKey(algorithm, extractable, keyUsages)
algorithmAn object defining the type of key to generate and providing extra algorithm-specific parameters.
RsaHashedKeyGenParams object.EcKeyGenParams object.HmacKeyGenParams object.AesKeyGenParams object.Ed25519 or an object of the form { name: "Ed25519" }.X25519 or an object of the form { name: "X25519" }.extractableA boolean value indicating whether it will be possible to export the key using SubtleCrypto.exportKey() or SubtleCrypto.wrapKey().
keyUsagesAn Array of strings indicating what can be done with the newly generated key. Possible values for array elements are:
encryptThe key may be used to encrypt messages.
decryptThe key may be used to decrypt messages.
signThe key may be used to sign messages.
verifyThe key may be used to verify signatures.
deriveKeyThe key may be used in deriving a new key.
deriveBitsThe key may be used in deriving bits.
wrapKeyThe key may be used to wrap a key.
unwrapKeyThe key may be used to unwrap a key.
A Promise that fulfills with a CryptoKey (for symmetric algorithms) or a CryptoKeyPair (for public-key algorithms).
The promise is rejected when the following exception is encountered:
SyntaxError DOMException
Raised when the result is a CryptoKey of type secret or private but keyUsages is empty, or invalid for the algorithm type.
SyntaxError DOMException
Raised when the result is a CryptoKeyPair and its privateKey.usages attribute is empty, or invalid for the algorithm type.
Note: You can try the working examples on GitHub.
This code generates an RSA-OAEP encryption key pair. See the complete code on GitHub.
let keyPair = await window.crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["encrypt", "decrypt"],
);
This code generates an ECDSA signing key pair. See the complete code on GitHub.
let keyPair = await window.crypto.subtle.generateKey(
{
name: "ECDSA",
namedCurve: "P-384",
},
true,
["sign", "verify"],
);
This code generates an HMAC signing key. See the complete code on GitHub.
let key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-512" },
},
true,
["sign", "verify"],
);
This code generates an AES-GCM encryption key. See the complete code on GitHub.
let key = await window.crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"],
);
This code generates an Ed25519 signing key pair. It is derived from this source code on GitHub, which you can run live here.
Code for generating a key pair using the Ed25519 algorithm and logging the information in each key is shown below. Note that the code is run in a try..catch block because not all browsers support this algorithm.
The JavaScript first gets the #sign-button and #message <input> elements, then adds a listener for the click event on the button. The event handler clears the log and runs the other operations passing the content of the <input> element.
const button = document.querySelector("#run-button");
const input = document.querySelector("#log");
button.addEventListener("click", () => {
// Clear log
logElement.innerText = "";
logElement.scrollTop = logElement.scrollHeight;
// Run test
test();
});
async function test() {
try {
// Create a key pair and use destructuring assignment to assign to variables
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"],
);
// Log the properties of the keys
log(`publicKey: ${publicKey}`);
log(` type: ${publicKey.type}`);
log(` extractable: ${publicKey.extractable}`);
log(` algorithm: ${JSON.stringify(publicKey.algorithm)}`);
log(` usages: ${publicKey.usages}`);
log(`privateKey: ${privateKey}`);
log(` type: ${privateKey.type}`);
log(` extractable: ${privateKey.extractable}`);
log(` algorithm: ${JSON.stringify(privateKey.algorithm)}`);
log(` usages: ${privateKey.usages}`);
} catch (error) {
log(error);
}
}
The information about the created keys is logged below (or an error string if the browser does not allow the key to be created).
This code generates an X25519 public and private key pair that can be used in SubtleCrypto.deriveKey() to create a shared key, or in SubtleCrypto.deriveBits() to create a shared secret.
Code for generating a key pair using the X25519 algorithm and logging the information in each key is shown below. Note that the code is run in a try..catch block because not all browsers support this algorithm.
The JavaScript first gets the #run-button and #log <input> elements, then adds a listener for the click event on the button. The event handler clears the log, generates an X25519 key pair, and logs some of its properties.
const button = document.querySelector("#run-button");
const input = document.querySelector("#log");
button.addEventListener("click", () => {
// Clear log
logElement.innerText = "";
logElement.scrollTop = logElement.scrollHeight;
// Run test
test();
});
async function test() {
try {
// Create a key pair and use destructuring assignment to assign to variables
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
name: "X25519",
},
true,
["deriveKey", "deriveBits"],
);
// Log the properties of the keys
log(`publicKey: ${publicKey}`);
log(` type: ${publicKey.type}`);
log(` extractable: ${publicKey.extractable}`);
log(` algorithm: ${JSON.stringify(publicKey.algorithm)}`);
log(` usages: ${publicKey.usages}`);
log(`privateKey: ${privateKey}`);
log(` type: ${privateKey.type}`);
log(` extractable: ${privateKey.extractable}`);
log(` algorithm: ${JSON.stringify(privateKey.algorithm)}`);
log(` usages: ${privateKey.usages}`);
} catch (error) {
log(error);
}
}
The information about the created keys is logged below (or an error string if the browser does not allow the key to be created).
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
generateKey |
37 | 7912–79["Not supported: RSA-PSS, ECDSA, ECDH.", "Not supported: AES-CTR."] |
34 | 24 | 7 | 37 | 34 | 24 | 7 | 3.0 | 37 | 7 |
ed25519 |
137 | 137 | 129 | 121 | 17 | 137 | 129 | 90 | 17 | No | 137 | 17 |
x25519 |
133 | 133 | 130 | 118 | 17 | 133 | 130 | 88 | 17 | No | 133 | 17 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey