MediaWiki  1.30.0
CryptRand Class Reference
Collaboration diagram for CryptRand:

Public Member Functions

 __construct (array $randomFuncs, array $randomFiles, LoggerInterface $logger)
 
 generate ( $bytes, $forceStrong=false)
 Generate a run of (ideally) cryptographically random data and return it in raw binary form. More...
 
 generateHex ( $chars, $forceStrong=false)
 Generate a run of (ideally) cryptographically random data and return it in hexadecimal string format. More...
 
 wasStrong ()
 Return a boolean indicating whether or not the source used for cryptographic random bytes generation in the previously run generate* call was cryptographically strong. More...
 

Public Attributes

const MIN_ITERATIONS = 1000
 Minimum number of iterations we want to make in our drift calculations. More...
 
const MSEC_PER_BYTE = 0.5
 Number of milliseconds we want to spend generating each separate byte of the final generated bytes. More...
 

Protected Member Functions

 driftHash ( $data)
 Randomly hash data while mixing in clock drift data for randomness. More...
 
 initialRandomState ()
 Initialize an initial random state based off of whatever we can find. More...
 
 randomState ()
 Return a rolling random state initially build using data from unstable sources. More...
 

Protected Attributes

LoggerInterface $logger
 
string[] $randomFiles = []
 List of files to generate some random state from. More...
 
callable[] $randomFuncs = []
 List of functions to call to generate some random state. More...
 
 $strong = null
 A boolean indicating whether the previous random generation was done using cryptographically strong random number generator or not. More...
 

Detailed Description

Definition at line 28 of file CryptRand.php.

Constructor & Destructor Documentation

◆ __construct()

CryptRand::__construct ( array  $randomFuncs,
array  $randomFiles,
LoggerInterface  $logger 
)

Definition at line 67 of file CryptRand.php.

References $logger, $randomFiles, and $randomFuncs.

Member Function Documentation

◆ driftHash()

CryptRand::driftHash (   $data)
protected

Randomly hash data while mixing in clock drift data for randomness.

Parameters
string$dataThe data to randomly hash.
Returns
string The hashed bytes
Author
Tim Starling

Definition at line 148 of file CryptRand.php.

References $buffer, MWCryptHash\hash(), MWCryptHash\hashLength(), and MIN_ITERATIONS.

Referenced by randomState().

◆ generate()

CryptRand::generate (   $bytes,
  $forceStrong = false 
)

Generate a run of (ideally) cryptographically random data and return it in raw binary form.

You can use CryptRand::wasStrong() if you wish to know if the source used was cryptographically strong.

Parameters
int$bytesThe number of bytes of random data to generate
bool$forceStrongPass true if you want generate to prefer cryptographically strong sources of entropy even if reading from them may steal more entropy from the system than optimal.
Returns
string Raw binary random data

Definition at line 236 of file CryptRand.php.

References $buffer, MWCryptHash\hmac(), and randomState().

Referenced by generateHex().

◆ generateHex()

CryptRand::generateHex (   $chars,
  $forceStrong = false 
)

Generate a run of (ideally) cryptographically random data and return it in hexadecimal string format.

You can use CryptRand::wasStrong() if you wish to know if the source used was cryptographically strong.

Parameters
int$charsThe number of hex chars of random data to generate
bool$forceStrongPass true if you want generate to prefer cryptographically strong sources of entropy even if reading from them may steal more entropy from the system than optimal.
Returns
string Hexadecimal random data

Definition at line 387 of file CryptRand.php.

References generate().

◆ initialRandomState()

CryptRand::initialRandomState ( )
protected

Initialize an initial random state based off of whatever we can find.

Returns
string

Definition at line 77 of file CryptRand.php.

References $path, $randomFiles, as, and serialize().

Referenced by randomState().

◆ randomState()

CryptRand::randomState ( )
protected

Return a rolling random state initially build using data from unstable sources.

Returns
string A new weak random state

Definition at line 194 of file CryptRand.php.

References driftHash(), MWCryptHash\hash(), and initialRandomState().

Referenced by generate().

◆ wasStrong()

CryptRand::wasStrong ( )

Return a boolean indicating whether or not the source used for cryptographic random bytes generation in the previously run generate* call was cryptographically strong.

Returns
bool Returns true if the source was strong, false if not.

Definition at line 216 of file CryptRand.php.

References $strong.

Member Data Documentation

◆ $logger

LoggerInterface CryptRand::$logger
protected

Definition at line 65 of file CryptRand.php.

Referenced by __construct().

◆ $randomFiles

string [] CryptRand::$randomFiles = []
protected

List of files to generate some random state from.

Definition at line 60 of file CryptRand.php.

Referenced by __construct(), and initialRandomState().

◆ $randomFuncs

callable [] CryptRand::$randomFuncs = []
protected

List of functions to call to generate some random state.

Definition at line 53 of file CryptRand.php.

Referenced by __construct().

◆ $strong

CryptRand::$strong = null
protected

A boolean indicating whether the previous random generation was done using cryptographically strong random number generator or not.

Definition at line 46 of file CryptRand.php.

Referenced by wasStrong().

◆ MIN_ITERATIONS

const CryptRand::MIN_ITERATIONS = 1000

Minimum number of iterations we want to make in our drift calculations.

Definition at line 32 of file CryptRand.php.

Referenced by driftHash().

◆ MSEC_PER_BYTE

const CryptRand::MSEC_PER_BYTE = 0.5

Number of milliseconds we want to spend generating each separate byte of the final generated bytes.

This is used in combination with the hash length to determine the duration we should spend doing drift calculations.

Definition at line 40 of file CryptRand.php.


The documentation for this class was generated from the following file: