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/next/javascript-api/crypto/subtlecrypto/encrypt.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/next/javascript-api/crypto/subtlecrypto/encrypt/. 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.
This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
encrypt
The encrypt() method encrypts some data.
Usage
encrypt(algorithm, key, data)Parameters
| Name | Type | Description |
|---|---|---|
algorithm | AesCbcParams, AesCtrParams, or AesGcmParams object | Defines the algorithm to use and any extra-parameters. |
key | CryptoKey | The key to use for encryption. |
data | ArrayBuffer, TypedArray, or DataView | The data to be encrypted (also known as “plaintext”). |
Supported algorithms
| AES-CBC | AES-CTR | AES-GCM | RSA-OAEP |
|---|---|---|---|
| ✅ AesCbcParams | ✅ AesCtrParams | ✅ AesGcmParams | ✅ RsaOaepParams |
Return Value
A Promise that resolves to a new ArrayBuffer containing the encrypted data.
Throws
| Type | Description |
|---|---|
InvalidAccessError | Raised when the requested operation is not valid with the provided key. For instance when an invalid encryption algorithm is used, or a key not matching the selected algorithm is provided. |
OperationError | Raised when the operation failed for an operation-specific reason. For instance, if the algorithm size is invalid, or errors occurred during the process of decrypting the ciphertext. |
Example
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;
}Was this page helpful?
Related resources from Grafana Labs

