---
title: "SubtleCrypto | Grafana k6 documentation"
description: "SubtleCrypto offers low-level cryptographic functions."
---

# SubtleCrypto

The `SubtleCrypto` interface provides a set of low-level cryptographic primitives such as encryption, decryption, digital signature generation and verification, and key generation and management. It is useful for using secure and efficient cryptographic operations within k6 scripts.

## Methods

Expand table

| Method                                                                         | Description                                                                                                          |
|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| [encrypt](/docs/k6/latest/javascript-api/crypto/subtlecrypto/encrypt/)         | Encrypts the given plaintext data using the specified algorithm and key.                                             |
| [decrypt](/docs/k6/latest/javascript-api/crypto/subtlecrypto/decrypt/)         | Decrypts the given ciphertext data using the specified algorithm and key.                                            |
| [sign](/docs/k6/latest/javascript-api/crypto/subtlecrypto/sign/)               | Signs the given data using the specified algorithm and key.                                                          |
| [verify](/docs/k6/latest/javascript-api/crypto/subtlecrypto/verify/)           | Verifies the signature of the given data using the specified algorithm and key.                                      |
| [digest](/docs/k6/latest/javascript-api/crypto/subtlecrypto/digest/)           | Computes the digest (hash) of the given data using the specified algorithm.                                          |
| [generateKey](/docs/k6/latest/javascript-api/crypto/subtlecrypto/generatekey/) | Generates a new cryptographic key for use with the specified algorithm.                                              |
| [importKey](/docs/k6/latest/javascript-api/crypto/subtlecrypto/importkey/)     | Imports a raw key material into the Web Crypto API, generating a new key object to use with the specified algorithm. |
| [exportKey](/docs/k6/latest/javascript-api/crypto/subtlecrypto/exportkey/)     | Exports the raw key material of the given key object.                                                                |
| [deriveBits](/docs/k6/latest/javascript-api/crypto/subtlecrypto/derivebits/)   | Derives bits using provided input.                                                                                   |
| [deriveKey](/docs/k6/latest/javascript-api/crypto/subtlecrypto/derivekey/)     | Derives a secret key from a master key.                                                                              |

## Example

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

```javascript
export default async function () {
  const plaintext = stringToArrayBuffer('Hello, World!');

  /**
   * Generate a symmetric key using the AES-CBC algorithm.
   */
  const key = await crypto.subtle.generateKey(
    {
      name: 'AES-CBC',
      length: 256,
    },
    true,
    ['encrypt', 'decrypt']
  );

  /**
   * Encrypt the plaintext using the AES-CBC key with
   * have generated.
   */
  const iv = crypto.getRandomValues(new Uint8Array(16));
  const ciphertext = await crypto.subtle.encrypt(
    {
      name: 'AES-CBC',
      iv: iv,
    },
    key,
    plaintext
  );

  /**
   * Decrypt the ciphertext using the same key to verify
   * that the resulting plaintext is the same as the original.
   */
  const deciphered = await crypto.subtle.decrypt(
    {
      name: 'AES-CBC',
      iv: iv,
    },
    key,
    ciphertext
  );

  console.log(
    'deciphered text == original plaintext: ',
    arrayBufferToHex(deciphered) === arrayBufferToHex(plaintext)
  );
}

function arrayBufferToHex(buffer) {
  return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('');
}

function stringToArrayBuffer(str) {
  const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
  const bufView = new Uint16Array(buf);
  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}
```
