Skip to content

random_bytes

Random bytes from the operating system, suitable for keys, tokens and salts.

Needs the random-entropy feature. It returns an error rather than weaker randomness when the system cannot provide bytes.

use helpers4::random::random_bytes;

Cargo feature random (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features random

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.9", default-features = false, features = ["random"] }
pub fn random_bytes(len: usize) -> Result<Vec<u8>, EntropyError>
ParameterTypeDescription
lenusizeThe number of bytes.

Result<Vec<u8>, EntropyError> — len random bytes, or an EntropyError.

Returns an EntropyError when the operating system cannot provide random bytes.

use helpers4::random::random_bytes;

let key = random_bytes(32)?;
assert_eq!(key.len(), 32);

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.

use helpers4::random::EntropyError;

pub struct EntropyError(getrandom::Error) { /* private fields */ }
use helpers4::random::random_bytes;

match random_bytes(16) {
    Ok(bytes) => assert_eq!(bytes.len(), 16),
    Err(error) => eprintln!("no randomness available: {error}"),
}

src/random/random_bytes.rs