digest
The digest()
method generates a cryptographically secure digest of the given data. A digest is a short fixed-length value derived from some input data. The digest()
method is commonly used to compute a checksum of data or to verify the integrity of data.
Usage
digest(algorithm, data)
Parameters
Supported algorithms
Return Value
A Promise
that resolves to a new ArrayBuffer
containing the digest.
Example
import { crypto } from 'k6/experimental/webcrypto';
export default async function () {
const digest = await crypto.subtle.digest('SHA-256', stringToArrayBuffer('Hello, world!'));
console.log(arrayBufferToHex(digest));
}
function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, '0')).join('');
}
function stringToArrayBuffer(s) {
return Uint8Array.from(new String(s), (x) => x.charCodeAt(0));
}