randomHex.php
Table of Contents
Functions
- randomHex() : string
- Generates a cryptographically secure random token, encoded as a lowercase hexadecimal string.
Functions
randomHex()
Generates a cryptographically secure random token, encoded as a lowercase hexadecimal string.
randomHex([int $bytes = 32 ]) : string
The function reads $bytes of entropy from random_bytes() (a CSPRNG)
and encodes the result with bin2hex(). The output is safe to use in
URLs, logs, filenames, correlation/request IDs, JWT jti, refresh tokens
and similar contexts.
Output length is exactly 2 * $bytes characters. Default $bytes = 32
yields 256 bits of entropy → 64 characters.
For shorter URL-friendly tokens at equivalent entropy, prefer randomBase64Url().
Parameters
- $bytes : int = 32
-
Number of random bytes to generate. Must be
>= 1. Defaults to 32 (256 bits of entropy).
Tags
Return values
string —Lowercase hexadecimal string of length 2 * $bytes.