random_hex
Random bytes from the operating system, written as lowercase hexadecimal: the usual form of
a master key or a session token (random_hex(32) is a 256-bit key in 64 characters).
Needs the random-entropy feature.
Import
Section titled “Import”Cargo feature random (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
byte_len | usize | The number of random bytes; the result has twice as many characters. |
Returns
Section titled “Returns”Result<String, EntropyError> — 2 * byte_len characters in 0-9a-f, or an EntropyError.
Errors
Section titled “Errors”Returns an EntropyError when the operating system cannot provide random bytes.
Examples
Section titled “Examples”Error type: EntropyError
Section titled “Error type: EntropyError”The operating system could not provide random bytes.
Rare, but possible (no entropy source at boot, a sandbox that blocks the system call, an unsupported platform). The functions that return it never fall back to weaker randomness: a key or a token made from predictable bytes is worse than no key.
