> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/grafana/k6/llms.txt
> Use this file to discover all available pages before exploring further.

# k6/crypto

> Cryptographic hashing and HMAC functions.

The `k6/crypto` module provides cryptographic hashing and HMAC functionality.

## Hash Functions

### md4(input, outputEncoding)

Generates MD4 hash.

<ParamField path="input" type="string | ArrayBuffer">
  Data to hash
</ParamField>

<ParamField path="outputEncoding" type="string">
  Output encoding: `hex`, `base64`, `base64url`, `base64rawurl`, or `binary`
</ParamField>

### md5(input, outputEncoding)

Generates MD5 hash.

```javascript theme={null}
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.

```javascript theme={null}
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.

```javascript theme={null}
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.

<ParamField path="algorithm" type="string">
  Hash algorithm: `md4`, `md5`, `sha1`, `sha256`, `sha384`, `sha512`, `sha512_224`, `sha512_256`, or `ripemd160`
</ParamField>

```javascript theme={null}
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.

<ParamField path="input" type="string | ArrayBuffer">
  Data to add to the hash
</ParamField>

#### digest(outputEncoding)

Returns the final hash value.

<ParamField path="outputEncoding" type="string">
  Output encoding: `hex`, `base64`, `base64url`, `base64rawurl`, or `binary`
</ParamField>

## HMAC Functions

### hmac(algorithm, key, input, outputEncoding)

Generates HMAC.

<ParamField path="algorithm" type="string">
  Hash algorithm
</ParamField>

<ParamField path="key" type="string | ArrayBuffer">
  Secret key
</ParamField>

<ParamField path="input" type="string | ArrayBuffer">
  Data to hash
</ParamField>

<ParamField path="outputEncoding" type="string">
  Output encoding
</ParamField>

```javascript theme={null}
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.

<ParamField path="algorithm" type="string">
  Hash algorithm
</ParamField>

<ParamField path="key" type="string | ArrayBuffer">
  Secret key
</ParamField>

```javascript theme={null}
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.

<ParamField path="size" type="number">
  Number of bytes to generate (must be > 0)
</ParamField>

<ResponseField name="result" type="ArrayBuffer">
  Random bytes
</ResponseField>

```javascript theme={null}
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.

<ParamField path="data" type="ArrayBuffer">
  Data to encode
</ParamField>

<ResponseField name="result" type="string">
  Hex string
</ResponseField>

```javascript theme={null}
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

```javascript theme={null}
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

```javascript theme={null}
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

```javascript theme={null}
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

```javascript theme={null}
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,
  });
}
```
