5 Module N_RANDOM
gullradriel edited this page 2026-10-09 00:55:18 +02:00

RANDOM: cryptographically secure random helpers

Functions

Name
N_STR * n_random_hex_token(size_t nbytes)
generate nbytes secure random bytes and return them as a lowercase hex N_STR (2*nbytes characters); free with free_nstr.
int n_random_bytes(unsigned char * buf, size_t n)
fill buf with n cryptographically secure random bytes from the system RNG: BCryptGenRandom on Windows (there is no /dev/urandom there), otherwise getrandom(2) when available and /dev/urandom as the fallback.

Functions Documentation

function n_random_hex_token

N_STR * n_random_hex_token(
    size_t nbytes
)

generate nbytes secure random bytes and return them as a lowercase hex N_STR (2*nbytes characters); free with free_nstr.

Returns NULL on error.

function n_random_bytes

int n_random_bytes(
    unsigned char * buf,
    size_t n
)

fill buf with n cryptographically secure random bytes from the system RNG: BCryptGenRandom on Windows (there is no /dev/urandom there), otherwise getrandom(2) when available and /dev/urandom as the fallback.

A NULL buffer is rejected unless n is 0. Returns 0 on success, -1 on error.