Menu
Open source
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.
Currently, the only ECDH algorithm is supported.
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. |
Return Value
A Promise
that resolves to a new ArrayBuffer
containing the derived bits.
Example
JavaScript
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 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.