Skip to main content
The k6/crypto module provides cryptographic hashing and HMAC functionality.

Hash Functions

md4(input, outputEncoding)

Generates MD4 hash.
input
string | ArrayBuffer
Data to hash
outputEncoding
string
Output encoding: hex, base64, base64url, base64rawurl, or binary

md5(input, outputEncoding)

Generates MD5 hash.
import crypto from 'k6/crypto';

export default function () {
  const hash = crypto.md5('hello world', 'hex');
  console.log(hash);
}

sha1(input, outputEncoding)

Generates SHA-1 hash.
import crypto from 'k6/crypto';

export default function () {
  const hash = crypto.sha1('hello world', 'hex');
  console.log(hash);
}

sha256(input, outputEncoding)

Generates SHA-256 hash.
import crypto from 'k6/crypto';

export default function () {
  const hash = crypto.sha256('hello world', 'hex');
  console.log(hash);
}

sha384(input, outputEncoding)

Generates SHA-384 hash.

sha512(input, outputEncoding)

Generates SHA-512 hash.

sha512_224(input, outputEncoding)

Generates SHA-512/224 hash.

sha512_256(input, outputEncoding)

Generates SHA-512/256 hash.

ripemd160(input, outputEncoding)

Generates RIPEMD-160 hash.

Hasher Objects

createHash(algorithm)

Creates a reusable hasher object.
algorithm
string
Hash algorithm: md4, md5, sha1, sha256, sha384, sha512, sha512_224, sha512_256, or ripemd160
import crypto from 'k6/crypto';

export default function () {
  const hasher = crypto.createHash('sha256');
  hasher.update('hello ');
  hasher.update('world');
  console.log(hasher.digest('hex'));
}

update(input)

Adds data to the hash.
input
string | ArrayBuffer
Data to add to the hash

digest(outputEncoding)

Returns the final hash value.
outputEncoding
string
Output encoding: hex, base64, base64url, base64rawurl, or binary

HMAC Functions

hmac(algorithm, key, input, outputEncoding)

Generates HMAC.
algorithm
string
Hash algorithm
key
string | ArrayBuffer
Secret key
input
string | ArrayBuffer
Data to hash
outputEncoding
string
Output encoding
import crypto from 'k6/crypto';

export default function () {
  const hmac = crypto.hmac('sha256', 'secret-key', 'hello world', 'hex');
  console.log(hmac);
}

createHMAC(algorithm, key)

Creates a reusable HMAC hasher.
algorithm
string
Hash algorithm
key
string | ArrayBuffer
Secret key
import crypto from 'k6/crypto';

export default function () {
  const hasher = crypto.createHMAC('sha256', 'secret-key');
  hasher.update('hello ');
  hasher.update('world');
  console.log(hasher.digest('hex'));
}

Utility Functions

randomBytes(size)

Generates cryptographically secure random bytes.
size
number
Number of bytes to generate (must be > 0)
result
ArrayBuffer
Random bytes
import crypto from 'k6/crypto';

export default function () {
  const randomData = crypto.randomBytes(16);
  console.log(new Uint8Array(randomData));
}

hexEncode(data)

Encodes data as hexadecimal string.
data
ArrayBuffer
Data to encode
result
string
Hex string
import crypto from 'k6/crypto';

export default function () {
  const data = new Uint8Array([72, 101, 108, 108, 111]);
  const hex = crypto.hexEncode(data.buffer);
  console.log(hex); // "48656c6c6f"
}

Examples

Simple Hash Example

import crypto from 'k6/crypto';

export default function () {
  // Shorthand API
  const hash = crypto.sha1('some text', 'hex');
  console.log(hash);

  // Flexible API
  const hasher = crypto.createHash('sha1');
  hasher.update('some other text');
  console.log(hasher.digest('hex'));
  console.log(hasher.digest('base64'));
}

API Request Signing

import http from 'k6/http';
import crypto from 'k6/crypto';

export default function () {
  const timestamp = Date.now().toString();
  const message = `timestamp=${timestamp}`;
  const signature = crypto.hmac('sha256', 'api-secret', message, 'hex');

  const params = {
    headers: {
      'X-Timestamp': timestamp,
      'X-Signature': signature,
    },
  };

  http.get('https://api.example.com/data', params);
}

Password Hashing

import crypto from 'k6/crypto';

export default function () {
  const password = 'user-password';
  const salt = crypto.randomBytes(16);
  const saltHex = crypto.hexEncode(salt);

  // Hash password with salt
  const hasher = crypto.createHash('sha256');
  hasher.update(password);
  hasher.update(salt);
  const hash = hasher.digest('hex');

  console.log('Salt:', saltHex);
  console.log('Hash:', hash);
}

Checksum Verification

import http from 'k6/http';
import crypto from 'k6/crypto';
import { check } from 'k6';

export default function () {
  const res = http.get('https://example.com/file.bin');
  const expectedChecksum = 'abc123...';
  const actualChecksum = crypto.sha256(res.body, 'hex');

  check(res, {
    'checksum matches': () => actualChecksum === expectedChecksum,
  });
}

Build docs developers (and LLMs) love