class RandomReader extends AbstractGenerator (View source)

Generates bytes reading directly from the random device.

RandomReader generates random bytes by reading directly from the random device (i.e. from /dev/urandom or /dev/random).

Methods

string
getBytes(int $count)

Returns securely generated random bytes.

string|false
readBytes(int $count)

Reads bytes from the randomness source.

__construct(bool $urandom = true)

Creates new instance of RandomReader.

__destruct()

Closes the file pointer.

bool
isSupported()

Tells if the generator is supported by the operating system.

Details

string getBytes(int $count)

Returns securely generated random bytes.

This method will always return the correct number of bytes that have been securely generated. If the bytes cannot be securely generated or if the correct number of bytes cannot be generated, the method MUST throw an exception.

Parameters

int $count Number of bytes to return

Return Value

string Bytes generated by the secure random byte generator

Exceptions

GeneratorException If any error occurs

protected string|false readBytes(int $count)

Reads bytes from the randomness source.

Parameters

int $count number of bytes to read

Return Value

string|false The bytes read from the randomness source or false on error

Exceptions

GeneratorException If error occurs in byte generation

__construct(bool $urandom = true)

Creates new instance of RandomReader.

Parameters

bool $urandom True to read from /dev/urandom and false to read from /dev/random

__destruct()

Closes the file pointer.

bool isSupported()

Tells if the generator is supported by the operating system.

Return Value

bool True if the generator is supported, false if not