---
title: "generateKey | Grafana k6 documentation"
description: "generateKey generates a new key."
---

# generateKey

The `generateKey()` generates a new cryptographic key and returns it as a [CryptoKey](/docs/k6/latest/javascript-api/crypto/cryptokey/) object or a [CryptoKeyPair](/docs/k6/latest/javascript-api/crypto/cryptokeypair/) object that can be used with the Web Crypto API.

## Usage

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
generateKey(algorithm, extractable, keyUsages)
```

## Parameters

Expand table

| Name          | Type                                                       | Description                                                                                                                                                      |
|---------------|------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `algorithm`   | a `string` or algorithm object with a single `name` string | 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](/docs/k6/latest/javascript-api/crypto/subtlecrypto/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.                                  |

### Supported algorithms

Expand table

| AES-CBC                                                               | AES-CTR                                                               | AES-GCM                                                               | AES-KW | ECDH                                                                                | ECDSA                                                               | HMAC                                                                          | PBKDF2                                                                | RSA-OAEP                                                                                | RSASSA-PKCS1-v1\_5                                                                      | RSA-PSS                                                                                 |
|-----------------------------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------------------------------------------|--------|-------------------------------------------------------------------------------------|---------------------------------------------------------------------|-------------------------------------------------------------------------------|-----------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| ✅ [AesCbcParams](/docs/k6/latest/javascript-api/crypto/aescbcparams/) | ✅ [AesCtrParams](/docs/k6/latest/javascript-api/crypto/aesctrparams/) | ✅ [AesGcmParams](/docs/k6/latest/javascript-api/crypto/aesgcmparams/) | ❌      | ✅ [EcdhKeyDeriveParams](/docs/k6/latest/javascript-api/crypto/ecdhkeyderiveparams/) | ✅ [EcdsaParams](/docs/k6/latest/javascript-api/crypto/ecdsaparams/) | ✅ [HmacKeyGenParams](/docs/k6/latest/javascript-api/crypto/hmackeygenparams/) | ✅ [Pbkdf2Params](/docs/k6/latest/javascript-api/crypto/pbkdf2params/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) | ✅ [RsaHashedImportParams](/docs/k6/latest/javascript-api/crypto/rsahashedimportparams/) |

## Return Value

A `Promise` that resolves with the generated key as a [CryptoKey](/docs/k6/latest/javascript-api/crypto/cryptokey/) object or a [CryptoKeyPair](/docs/k6/latest/javascript-api/crypto/cryptokeypair/) object.

### Algorithm specific input

Expand table

|                        | HMAC                                                                          | AES                                                                         | ECDH                                                                      | ECDSA                                                                     | RSA-OAEP                                                                                | RSASSA-PKCS1-v1\_5                                                                      | RSA-PSS                                                                                 |
|------------------------|-------------------------------------------------------------------------------|-----------------------------------------------------------------------------|---------------------------------------------------------------------------|---------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------|
| Parameters type to use | [`HmacKeyGenParams`](/docs/k6/latest/javascript-api/crypto/hmackeygenparams/) | [`AesKeyGenParams`](/docs/k6/latest/javascript-api/crypto/aeskeygenparams/) | [`EcKeyGenParams`](/docs/k6/latest/javascript-api/crypto/eckeygenparams/) | [`EcKeyGenParams`](/docs/k6/latest/javascript-api/crypto/eckeygenparams/) | [`RSAHashedKeyGenParams`](/docs/k6/latest/javascript-api/crypto/rsahashedkeygenparams/) | [`RSAHashedKeyGenParams`](/docs/k6/latest/javascript-api/crypto/rsahashedkeygenparams/) | [`RSAHashedKeyGenParams`](/docs/k6/latest/javascript-api/crypto/rsahashedkeygenparams/) |
| Possible key usages    | `sign`, `verify`                                                              | `encrypt`, `decrypt`                                                        | `deriveKey`, `deriveBits`                                                 | `sign`, `verify`                                                          | `encrypt`, `decrypt`                                                                    | `sign`, `verify`                                                                        | `sign`, `verify`                                                                        |

## Throws

Expand table

| Type          | Description                                                                                   |
|---------------|-----------------------------------------------------------------------------------------------|
| `SyntaxError` | Raised when the `keyUsages` parameter is empty, but the key is of type `secret` or `private`. |

## Example

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
export default async function () {
  const key = await crypto.subtle.generateKey(
    {
      name: 'AES-CBC',
      length: 256,
    },
    true,
    ['encrypt', 'decrypt']
  );

  console.log(JSON.stringify(key));
}
```
