Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/v0.48.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/generatekey/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
generateKey
The generateKey() generates a new cryptographic key and returns it as a
CryptoKey object that can be used with the Web Crypto API.
Usage
generateKey(algorithm, extractable, keyUsages)Parameters
| Name | Type | Description |
|---|---|---|
algorithm | string,
AesKeyGenParams or
HmacKeyGenParams | The type of key to generate. Can be either a string with any of the currently supported algorithms: AES-CBC, AES-GCM, AES-CTR, and HMAC as value, or any of the
AesKeyGenParams or
HmacKeyGenParams objects. |
extractable | boolean | Whether the key can be exported using exportKey. |
keyUsages | Array<string> | An array of strings describing what operations can be performed with the key. Currently supported usages include encrypt, decrypt, sign, and verify. |
Return Value
A Promise that resolves with the generated key as a
CryptoKey object.
Throws
| Type | Description |
|---|---|
SyntaxError | Raised when the keyUsages parameter is empty but the key is of type secret or private. |
Example
import { crypto } from 'k6/experimental/webcrypto';
export default async function () {
const key = await crypto.subtle.generateKey(
{
name: 'AES-CBC',
length: 256,
},
true,
['encrypt', 'decrypt']
);
console.log(JSON.stringify(key));
}Was this page helpful?
Related resources from Grafana Labs

