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.52.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/v0.52.x/javascript-api/k6-experimental/webcrypto/subtlecrypto/derivebits/. 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.
deriveBits
It takes as its arguments the base key, the derivation algorithm to use, and the length of the bits to derive. It returns a Promise which will be fulfilled with an ArrayBuffer containing the derived bits. This array of bits can be used as a key for encryption or decryption as a shared secret.
Usage
deriveBits(algorithm, baseKey, length)Parameters
| Name | Type | Description |
|---|---|---|
algorithm | EcdhKeyDeriveParams | An object defining a derivation algorithm to use. |
baseKey | CryptoKey | Represent an input to derivation algorithm. Currently it could be only a private ECDH key. |
length | number | Optional. A length of the bits to derive. Currently, only multiplies of 8 are supported. |
Supported algorithms
| ECDH | HKDF | PBKDF2 |
|---|---|---|
| ✅ EcdhKeyDeriveParams | ❌ | ❌ |
Return Value
A Promise that resolves to a new ArrayBuffer containing the derived bits.
Example
import { crypto } from 'k6/experimental/webcrypto';
export default async function () {
// Generate a key pair for Alice
const aliceKeyPair = await crypto.subtle.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256',
},
true,
['deriveKey', 'deriveBits']
);
// Generate a key pair for Bob
const bobKeyPair = await crypto.subtle.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256',
},
true,
['deriveKey', 'deriveBits']
);
// Derive shared secret for Alice
const aliceSharedSecret = await deriveSharedSecret(aliceKeyPair.privateKey, bobKeyPair.publicKey);
// Derive shared secret for Bob
const bobSharedSecret = await deriveSharedSecret(bobKeyPair.privateKey, aliceKeyPair.publicKey);
console.log('alice shared secret: ' + printArrayBuffer(aliceSharedSecret));
console.log('bob shared secret: ' + printArrayBuffer(bobSharedSecret));
}
async function deriveSharedSecret(privateKey, publicKey) {
return crypto.subtle.deriveBits(
{
name: 'ECDH',
public: publicKey,
},
privateKey,
256
);
}
const printArrayBuffer = (buffer) => {
const view = new Uint8Array(buffer);
return Array.from(view);
};Was this page helpful?
Related resources from Grafana Labs

