# Module /crypto

Cryptographic hashing (SHA-256), HMAC authentication, AES-GCM/AEAD helpers, CSPRNG tokens, and constant-time equality.

```tangu
(use /crypto)
```

## Functions & symbols

### hash-password

`(hash-password password [iterations]) -> String`

Hashes `password` with a fresh random salt using PBKDF2-HMAC-SHA256 and returns an encoded PHC-style hash string.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `password` | `String` | no | Plaintext password |
| `iterations` | `Int` | yes | Optional PBKDF2-HMAC-SHA256 iteration count (default 600000) |

```tangu
(let pw-hash (crypto/hash-password "hunter2"))
```

### hmac-sha256

`(hmac-sha256 key message) -> String`

Computes an HMAC-SHA256 authentication tag and returns a lowercase hex string.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `key` | `String | Slab` | no | Secret HMAC key |
| `message` | `String | Slab` | no | Message payload to authenticate |

```tangu
(crypto/hmac-sha256 secret payload)
```

### random-bytes

`(random-bytes count) -> Slab`

Generates `count` cryptographically secure random bytes using the OS CSPRNG.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `count` | `Int` | no | Number of cryptographically secure bytes |

```tangu
(let nonce (crypto/random-bytes 16))
```

### random-hex

`(random-hex byte-count) -> String`

Generates `byte_count` CSPRNG bytes formatted as a lowercase hex string (`2 * byte_count` chars).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `byte_count` | `Int` | no | Number of random bytes to generate before hex encoding |

```tangu
(let token (crypto/random-hex 32))
```

### random-urlsafe

`(random-urlsafe [byte-count]) -> String`

Generates a URL-safe Base64 token (no padding) suitable for session IDs and CSRF nonces.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `byte_count` | `Int` | yes | Optional byte count (defaults to 32) |

```tangu
(let csrf-token (crypto/random-urlsafe 32))
```

### sha256

`(sha256 data) -> String`

Computes the SHA-256 cryptographic digest and returns a 64-character lowercase hex string.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `data` | `String | Slab` | no | Input data to hash |

```tangu
(crypto/sha256 "hello")
```

### timing-safe-eq?

`(timing-safe-eq? a b) -> Bool`

Compares two strings or byte Slabs in constant time to prevent timing side-channel attacks.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `String | Slab` | no | First digest or secret |
| `b` | `String | Slab` | no | Second digest or secret |

```tangu
(crypto/timing-safe-eq? expected actual)
```

### verify-password

`(verify-password password stored-hash) -> Bool`

Verifies a plaintext password against a stored PBKDF2 hash in constant time.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `password` | `String` | no | Candidate plaintext password |
| `stored_hash` | `String` | no | Stored hash returned by `hash-password` |

```tangu
(crypto/verify-password "hunter2" pw-hash)
```
