The Rand
class provides set of static functions for getting
random values. It includes functions for adding entropy to
the PRNG algorithm.
Description: Generates and returns random data
The Rand::generate
method generates data from seeded buffer.
The buffer is seeded automatically when the extension is initialized.
Entropy can be added using Rand::seed
.
The second parameter $must_be_strong
, whose default value is true,
specify whether the returned bytes are cryptographically strong
pseudo-random bytes. If it's false
, then information whether
the bytes are strong is saved in the third parameter. If it's true
and data are not strong, a RandException
is thrown
When using a default Rand implementation on Linux or BSD, then it's
very unlikely to get bytes that are not strong. It means that
the second and third parameters can be ignored and just
specify $num
and then possibly handle an exception.
num : int
- the number of bytes that are returned (string length)
must_be_strong : bool
- whether the result has to be strong -
default is true
returned_strong_result : bool
reference - output variable
which stores information whether the generated result is strong
string
: Random data.
It can throw RandException
with code
RandException::REQUESTED_BYTES_NUMBER_TOO_HIGH
- if the number of requested random bytes ($num
) is greater than CINT_MAX
RandException::GENERATE_PREDICTABLE
- if$must_be_strong
istrue
and the returned result is not strong.
$iv = \Crypto\Rand::generate(16);
Description: Mixes supplied data into the PRNG state
This method mixes data in $buf
into the internal PRNG state. It
means that it's not an actual seeding on its own but more adding
an extra entropy. The estimated value of entropy can be supplied
as the second parameter $entropy
. If it's not specified, then
length of the buffer is used.
buf : int
- a buffer to be mixed into the PRNG state
entropy : float
- estimated entropy of the data in $buf
null
: Nothing is returned.
It can throw RandException
with code
RandException::SEED_LENGTH_TOO_HIGH
- if the length of the buffer is greater than CINT_MAX
\Crypto\Rand::seed($buf);
Description: Erases the PRNG state
This method globally erases the memory that is used by PRNG.
This method has no parameters.
This method does not throw any exception.
null
: Nothing is returned.
\Crypto\Rand::cleanup();
Description: Reads data from file and mixes them to the PRNG state
This method mixes data from the file in $filename
into
the internal PRNG state. It reads at most $max_bytes
bytes.
filename : string
- file path of the file that is read
max_bytes : int
- maximal number of bytes that is read
from file
int
: The number of bytes read.
It can throw RandException
with code
RandException::REQUESTED_BYTES_NUMBER_TOO_HIGH
- if the supplied$max_bytes
is greater than CINT_MAX
\Crypto\Rand::loadFile($filename, 1024);
Description: Writes random bytes to the file
This method writes 1024 bytes the the file $filename
. It
can be used later to initial PRNG state by calling
Rand::load
.
filename : string
- file path of the file where
random data are written.
int
: The number of bytes read.
It can throw RandException
with code
RandException::REQUESTED_BYTES_NUMBER_TOO_HIGH
- if the written bytes were generated without appropriate seed.
\Crypto\Rand::writeFile($filename);