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

Import

Hash Functions

createHash()

Creates a Hasher object that can be fed with data repeatedly to generate hash digests.
string
required
The hashing algorithm to use. One of: "md4", "md5", "sha1", "sha256", "sha384", "sha512", "sha512_224", "sha512_256", "ripemd160".
Hasher
A Hasher object with update(data) and digest(encoding) methods.
Output:

createHMAC()

Creates an HMAC hashing object for generating signed hash digests.
string
required
The hashing algorithm. One of: "md4", "md5", "sha1", "sha256", "sha384", "sha512", "sha512_224", "sha512_256", "ripemd160".
string | ArrayBuffer
required
A shared secret used to sign the data.
Hasher
A Hasher object with update(data) and digest(encoding) methods.
Output:

Direct Hash Functions

md5()

Computes MD5 hash of input data.
string | ArrayBuffer
required
The input string or ArrayBuffer to hash.
string
required
Output encoding: "base64", "base64url", "base64rawurl", "hex", or "binary".
string | Array
Hash digest as string or array of integers (for binary encoding).
Output:

sha256()

Computes SHA-256 hash of input data.
string | ArrayBuffer
required
The input string or ArrayBuffer to hash.
string
required
Output encoding: "base64", "base64url", "base64rawurl", "hex", or "binary".
string | Array
Hash digest as string or array of integers (for binary encoding).
Output:

Other Hash Functions

All hash functions follow the same signature as md5() and sha256():

md4()

MD4 hashing algorithm

sha1()

SHA-1 hashing algorithm

sha384()

SHA-384 hashing algorithm

sha512()

SHA-512 hashing algorithm

sha512_224()

SHA-512/224 hashing algorithm

sha512_256()

SHA-512/256 hashing algorithm

ripemd160()

RIPEMD-160 hashing algorithm

hmac()

HMAC with any supported algorithm

Random Generation

randomBytes()

Generates cryptographically random bytes.
integer
required
The length of the returned ArrayBuffer.
ArrayBuffer
An ArrayBuffer with cryptographically random bytes.
Use randomBytes() for cryptographically secure random data. For non-cryptographic randomness, use Math.random().

Output Encodings

All hash functions support these output encodings:
  • "hex" - Hexadecimal string (most common)
  • "base64" - Standard base64 encoding
  • "base64url" - URL-safe base64 encoding
  • "base64rawurl" - URL-safe base64 without padding
  • "binary" - Array of integers (0-255)

Use Cases

Generate HMAC signatures for API authentication:
Hash passwords before sending (though use bcrypt/argon2 in production):
Verify file or data integrity:
Generate random tokens or IDs:
MD4 and MD5 are cryptographically broken. Use them only for non-security purposes like checksums. For security, use SHA-256 or higher.