Menu
Open source
generateKey
The generateKey()
generates a new cryptographic key and returns it as a CryptoKey object that can be used with the Web Crypto API.
Currently, supported algorithms are: AES-CBC
, AES-GCM
, AES-CTR
, HMAC
, ECDH
and ECDSA
.
Usage
generateKey(algorithm, extractable, keyUsages)
Parameters
Name | Type | Description |
---|---|---|
algorithm | string ,AesKeyGenParams , HmacKeyGenParams or EcKeyGenParams | The type of key to generate. It can be either a string with any of the currently supported algorithms as a value or any of the generation key parameter 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. Key usages could vary depending on the algorithm. |
Return Value
A Promise
that resolves with the generated key as a CryptoKey object or a CryptoKeyPair.
Algorithm specific input
HMAC | AES | ECDH | ECDSA | |
---|---|---|---|---|
Parameters type to use | HmacKeyGenParams | AesKeyGenParams | EcKeyGenParams | EcKeyGenParams |
Possible key usages | sign , verify | encrypt , decrypt | deriveKey , deriveBits | sign , verify |
Throws
Type | Description |
---|---|
SyntaxError | Raised when the keyUsages parameter is empty but the key is of type secret or private . |
Example
JavaScript
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 documentation
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video
Performance testing and observability in Grafana Cloud
In this webinar, learn how Grafana Cloud k6 offers you the best developer experience for performance testing.
User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.