Skip to content

crypto.rand

fn bytes #

fn bytes(bytes_needed int) ![]u8

bytes returns an array of bytes_needed random bytes.

Note: this call can block your program for a long period of time, if your system does not have access to enough entropy. See also rand.bytes(), if you do not need really random bytes, but instead pseudo random ones, from a pseudo random generator that can be seeded, and that is usually faster.

fn int_big #

fn int_big(n big.Integer) !big.Integer

int_big creates a random big.Integer with range [0, n) returns an error if n is 0 or negative.

fn int_u64 #

fn int_u64(max u64) !u64

int_u64 returns a random unsigned 64-bit integer u64 read from a real OS source of entropy.

fn prime #

fn prime(bits int) !big.Integer

prime returns a random prime of exactly bits bits, drawn from the operating system's source of entropy.

The two highest bits and the lowest bit are set. The lowest makes the candidate odd; the second highest means the product of two bits-sized primes has exactly 2 * bits bits, as RSA key generation expects.

Primality comes from big.Integer.is_probably_prime, whose error probability is negligible but not zero.

fn read #

fn read(mut buffer []u8) !

read fills buffer with random bytes from the OS.

fn safe_prime #

fn safe_prime(bits int) !big.Integer

safe_prime returns a random prime p of exactly bits bits for which (p - 1) / 2 is also prime. Safe primes are used as Diffie-Hellman moduli, where they rule out small subgroup attacks. They are far rarer than ordinary primes, so this is much slower than prime.