Menu
Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/cryptobreadcrumb arrow hmac( algorithm, secret, data, outputEncoding )
Open source

hmac( algorithm, secret, data, outputEncoding )

Note

A module with a better and standard API exists.

The new k6/experimental/webcrypto API partially implements the WebCryptoAPI, supporting more features than k6/crypto.

Use HMAC to sign a piece of data using a shared secret.

ParameterTypeDescription
algorithmstringThe hashing algorithm to use. One of md4, md5, sha1, sha256, sha384, sha512, sha512_224, sha512_256 or ripemd160.
secretstring / ArrayBufferA shared secret used to sign the data.
datastring / ArrayBufferThe data to sign.
outputEncodingstringDescribes the type of encoding to use for the hash value. Can be “base64”, “base64url”, “base64rawurl”, “hex” or “binary”.

Returns

TypeDescription
string / ArrayThe hash digest as string (for “base64”, “base64url”, “base64rawurl”, “hex” outputEncoding) or raw array of integers (for “binary” outputEncoding).

Example

JavaScript
import crypto from 'k6/crypto';

export default function () {
  let hash = crypto.hmac('sha256', 'mysecret', 'hello world!', 'hex');
  console.log(hash);
  const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
  hash = crypto.hmac('sha256', 'mysecret', new Uint8Array(binArray).buffer, 'hex');
  console.log(hash);
}

The above script should result in the following being printed during execution:

bash
INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f
INFO[0000] 893a72d8cab129e5ba85aea4599fd53f59bfe652cff4098a3780313228d8c20f