The k6/crypto module provides cryptographic hashing and HMAC functionality.
Hash Functions
Generates MD4 hash.
Output encoding: hex, base64, base64url, base64rawurl, or binary
Generates MD5 hash.
import crypto from 'k6/crypto';
export default function () {
const hash = crypto.md5('hello world', 'hex');
console.log(hash);
}
Generates SHA-1 hash.
import crypto from 'k6/crypto';
export default function () {
const hash = crypto.sha1('hello world', 'hex');
console.log(hash);
}
Generates SHA-256 hash.
import crypto from 'k6/crypto';
export default function () {
const hash = crypto.sha256('hello world', 'hex');
console.log(hash);
}
Generates SHA-384 hash.
Generates SHA-512 hash.
Generates SHA-512/224 hash.
Generates SHA-512/256 hash.
Generates RIPEMD-160 hash.
Hasher Objects
createHash(algorithm)
Creates a reusable hasher object.
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'));
}
Adds data to the hash.
digest(outputEncoding)
Returns the final hash value.
Output encoding: hex, base64, base64url, base64rawurl, or binary
HMAC Functions
Generates HMAC.
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.
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.
Number of bytes to generate (must be > 0)
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.
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,
});
}