2012-03-20 05:17:40 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* A cryptographic random generator class used for generating secret keys
|
|
|
|
|
*
|
|
|
|
|
* This is based in part on Drupal code as well as what we used in our own code
|
|
|
|
|
* prior to introduction of this class.
|
|
|
|
|
*
|
2012-05-21 19:56:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-03-20 05:17:40 +00:00
|
|
|
* @author Daniel Friesen
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class MWCryptRand {
|
|
|
|
|
/**
|
|
|
|
|
* Minimum number of iterations we want to make in our drift calculations.
|
|
|
|
|
*/
|
|
|
|
|
const MIN_ITERATIONS = 1000;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
const MSEC_PER_BYTE = 0.5;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Singleton instance for public use
|
|
|
|
|
*/
|
|
|
|
|
protected static $singleton = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A boolean indicating whether the previous random generation was done using
|
|
|
|
|
* cryptographically strong random number generator or not.
|
|
|
|
|
*/
|
|
|
|
|
protected $strong = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize an initial random state based off of whatever we can find
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return string
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
|
|
|
|
protected function initialRandomState() {
|
|
|
|
|
// $_SERVER contains a variety of unstable user and system specific information
|
|
|
|
|
// It'll vary a little with each page, and vary even more with separate users
|
|
|
|
|
// It'll also vary slightly across different machines
|
|
|
|
|
$state = serialize( $_SERVER );
|
|
|
|
|
|
2012-04-02 15:17:09 +00:00
|
|
|
// To try vary the system information of the state a bit more
|
2012-03-20 05:17:40 +00:00
|
|
|
// by including the system's hostname into the state
|
|
|
|
|
$state .= wfHostname();
|
|
|
|
|
|
|
|
|
|
// Try to gather a little entropy from the different php rand sources
|
|
|
|
|
$state .= rand() . uniqid( mt_rand(), true );
|
|
|
|
|
|
|
|
|
|
// Include some information about the filesystem's current state in the random state
|
2016-02-17 09:09:32 +00:00
|
|
|
$files = [];
|
2012-04-02 18:21:02 +00:00
|
|
|
|
2013-03-13 07:42:41 +00:00
|
|
|
// We know this file is here so grab some info about ourselves
|
2012-03-20 05:17:40 +00:00
|
|
|
$files[] = __FILE__;
|
2012-04-02 18:21:02 +00:00
|
|
|
|
|
|
|
|
// We must also have a parent folder, and with the usual file structure, a grandparent
|
2012-08-27 19:03:15 +00:00
|
|
|
$files[] = __DIR__;
|
|
|
|
|
$files[] = dirname( __DIR__ );
|
2012-04-02 18:21:02 +00:00
|
|
|
|
2013-11-04 10:02:48 +00:00
|
|
|
// The config file is likely the most often edited file we know should
|
|
|
|
|
// be around so include its stat info into the state.
|
|
|
|
|
// The constant with its location will almost always be defined, as
|
|
|
|
|
// WebStart.php defines MW_CONFIG_FILE to $IP/LocalSettings.php unless
|
|
|
|
|
// being configured with MW_CONFIG_CALLBACK (e.g. the installer).
|
2012-03-20 05:17:40 +00:00
|
|
|
if ( defined( 'MW_CONFIG_FILE' ) ) {
|
|
|
|
|
$files[] = MW_CONFIG_FILE;
|
|
|
|
|
}
|
2012-04-02 15:17:09 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
foreach ( $files as $file ) {
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2012-03-20 05:17:40 +00:00
|
|
|
$stat = stat( $file );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2012-03-20 05:17:40 +00:00
|
|
|
if ( $stat ) {
|
|
|
|
|
// stat() duplicates data into numeric and string keys so kill off all the numeric ones
|
|
|
|
|
foreach ( $stat as $k => $v ) {
|
|
|
|
|
if ( is_numeric( $k ) ) {
|
|
|
|
|
unset( $k );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// The absolute filename itself will differ from install to install so don't leave it out
|
2015-11-01 19:56:20 +00:00
|
|
|
$path = realpath( $file );
|
|
|
|
|
if ( $path !== false ) {
|
2013-01-19 20:20:29 +00:00
|
|
|
$state .= $path;
|
|
|
|
|
} else {
|
|
|
|
|
$state .= $file;
|
|
|
|
|
}
|
2012-03-20 05:17:40 +00:00
|
|
|
$state .= implode( '', $stat );
|
|
|
|
|
} else {
|
|
|
|
|
// The fact that the file isn't there is worth at least a
|
|
|
|
|
// minuscule amount of entropy.
|
|
|
|
|
$state .= '0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try and make this a little more unstable by including the varying process
|
|
|
|
|
// id of the php process we are running inside of if we are able to access it
|
|
|
|
|
if ( function_exists( 'getmypid' ) ) {
|
|
|
|
|
$state .= getmypid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If available try to increase the instability of the data by throwing in
|
|
|
|
|
// the precise amount of memory that we happen to be using at the moment.
|
|
|
|
|
if ( function_exists( 'memory_get_usage' ) ) {
|
|
|
|
|
$state .= memory_get_usage( true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// It's mostly worthless but throw the wiki's id into the data for a little more variance
|
|
|
|
|
$state .= wfWikiID();
|
|
|
|
|
|
2014-06-11 20:47:03 +00:00
|
|
|
// If we have a secret key set then throw it into the state as well
|
|
|
|
|
global $wgSecretKey;
|
2012-03-20 05:17:40 +00:00
|
|
|
if ( $wgSecretKey ) {
|
|
|
|
|
$state .= $wgSecretKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Randomly hash data while mixing in clock drift data for randomness
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $data The data to randomly hash.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @return string The hashed bytes
|
2012-03-20 05:17:40 +00:00
|
|
|
* @author Tim Starling
|
|
|
|
|
*/
|
|
|
|
|
protected function driftHash( $data ) {
|
2013-11-04 10:02:48 +00:00
|
|
|
// Minimum number of iterations (to avoid slow operations causing the
|
|
|
|
|
// loop to gather little entropy)
|
2012-03-20 05:17:40 +00:00
|
|
|
$minIterations = self::MIN_ITERATIONS;
|
|
|
|
|
// Duration of time to spend doing calculations (in seconds)
|
2014-08-26 17:09:54 +00:00
|
|
|
$duration = ( self::MSEC_PER_BYTE / 1000 ) * MWCryptHash::hashLength();
|
2012-03-20 05:17:40 +00:00
|
|
|
// Create a buffer to use to trigger memory operations
|
|
|
|
|
$bufLength = 10000000;
|
|
|
|
|
$buffer = str_repeat( ' ', $bufLength );
|
|
|
|
|
$bufPos = 0;
|
|
|
|
|
|
2013-03-13 07:42:41 +00:00
|
|
|
// Iterate for $duration seconds or at least $minIterations number of iterations
|
2012-03-20 05:17:40 +00:00
|
|
|
$iterations = 0;
|
|
|
|
|
$startTime = microtime( true );
|
|
|
|
|
$currentTime = $startTime;
|
|
|
|
|
while ( $iterations < $minIterations || $currentTime - $startTime < $duration ) {
|
|
|
|
|
// Trigger some memory writing to trigger some bus activity
|
|
|
|
|
// This may create variance in the time between iterations
|
|
|
|
|
$bufPos = ( $bufPos + 13 ) % $bufLength;
|
|
|
|
|
$buffer[$bufPos] = ' ';
|
|
|
|
|
// Add the drift between this iteration and the last in as entropy
|
|
|
|
|
$nextTime = microtime( true );
|
|
|
|
|
$delta = (int)( ( $nextTime - $currentTime ) * 1000000 );
|
|
|
|
|
$data .= $delta;
|
|
|
|
|
// Every 100 iterations hash the data and entropy
|
|
|
|
|
if ( $iterations % 100 === 0 ) {
|
|
|
|
|
$data = sha1( $data );
|
|
|
|
|
}
|
|
|
|
|
$currentTime = $nextTime;
|
|
|
|
|
$iterations++;
|
|
|
|
|
}
|
|
|
|
|
$timeTaken = $currentTime - $startTime;
|
2014-08-26 17:09:54 +00:00
|
|
|
$data = MWCryptHash::hash( $data );
|
2012-03-20 05:17:40 +00:00
|
|
|
|
|
|
|
|
wfDebug( __METHOD__ . ": Clock drift calculation " .
|
|
|
|
|
"(time-taken=" . ( $timeTaken * 1000 ) . "ms, " .
|
|
|
|
|
"iterations=$iterations, " .
|
|
|
|
|
"time-per-iteration=" . ( $timeTaken / $iterations * 1e6 ) . "us)\n" );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a rolling random state initially build using data from unstable sources
|
2012-03-23 00:45:17 +00:00
|
|
|
* @return string A new weak random state
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
|
|
|
|
protected function randomState() {
|
|
|
|
|
static $state = null;
|
|
|
|
|
if ( is_null( $state ) ) {
|
|
|
|
|
// Initialize the state with whatever unstable data we can find
|
|
|
|
|
// It's important that this data is hashed right afterwards to prevent
|
|
|
|
|
// it from being leaked into the output stream
|
2014-08-26 17:09:54 +00:00
|
|
|
$state = MWCryptHash::hash( $this->initialRandomState() );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
// Generate a new random state based on the initial random state or previous
|
|
|
|
|
// random state by combining it with clock drift
|
|
|
|
|
$state = $this->driftHash( $state );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
return $state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see self::wasStrong()
|
|
|
|
|
*/
|
|
|
|
|
public function realWasStrong() {
|
|
|
|
|
if ( is_null( $this->strong ) ) {
|
|
|
|
|
throw new MWException( __METHOD__ . ' called before generation of random data' );
|
|
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
return $this->strong;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see self::generate()
|
|
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public function realGenerate( $bytes, $forceStrong = false ) {
|
2012-03-20 05:17:40 +00:00
|
|
|
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": Generating cryptographic random bytes for " .
|
|
|
|
|
wfGetAllCallers( 5 ) . "\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
|
|
|
|
|
$bytes = floor( $bytes );
|
|
|
|
|
static $buffer = '';
|
|
|
|
|
if ( is_null( $this->strong ) ) {
|
|
|
|
|
// Set strength to false initially until we know what source data is coming from
|
|
|
|
|
$this->strong = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( strlen( $buffer ) < $bytes ) {
|
|
|
|
|
// If available make use of mcrypt_create_iv URANDOM source to generate randomness
|
|
|
|
|
// On unix-like systems this reads from /dev/urandom but does it without any buffering
|
2012-04-02 15:17:09 +00:00
|
|
|
// and bypasses openbasedir restrictions, so it's preferable to reading directly
|
2012-03-20 05:17:40 +00:00
|
|
|
// On Windows starting in PHP 5.3.0 Windows' native CryptGenRandom is used to generate
|
|
|
|
|
// entropy so this is also preferable to just trying to read urandom because it may work
|
|
|
|
|
// on Windows systems as well.
|
|
|
|
|
if ( function_exists( 'mcrypt_create_iv' ) ) {
|
|
|
|
|
$rem = $bytes - strlen( $buffer );
|
|
|
|
|
$iv = mcrypt_create_iv( $rem, MCRYPT_DEV_URANDOM );
|
|
|
|
|
if ( $iv === false ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": mcrypt_create_iv returned false.\n" );
|
|
|
|
|
} else {
|
2012-03-23 00:45:17 +00:00
|
|
|
$buffer .= $iv;
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": mcrypt_create_iv generated " . strlen( $iv ) .
|
|
|
|
|
" bytes of randomness.\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( strlen( $buffer ) < $bytes ) {
|
2016-02-10 21:27:24 +00:00
|
|
|
if ( function_exists( 'openssl_random_pseudo_bytes' ) ) {
|
2012-03-20 05:17:40 +00:00
|
|
|
$rem = $bytes - strlen( $buffer );
|
|
|
|
|
$openssl_bytes = openssl_random_pseudo_bytes( $rem, $openssl_strong );
|
|
|
|
|
if ( $openssl_bytes === false ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes returned false.\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$buffer .= $openssl_bytes;
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes generated " .
|
|
|
|
|
strlen( $openssl_bytes ) . " bytes of " .
|
|
|
|
|
( $openssl_strong ? "strong" : "weak" ) . " randomness.\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
if ( strlen( $buffer ) >= $bytes ) {
|
|
|
|
|
// openssl tells us if the random source was strong, if some of our data was generated
|
|
|
|
|
// using it use it's say on whether the randomness is strong
|
|
|
|
|
$this->strong = !!$openssl_strong;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only read from urandom if we can control the buffer size or were passed forceStrong
|
2013-11-04 10:02:48 +00:00
|
|
|
if ( strlen( $buffer ) < $bytes &&
|
|
|
|
|
( function_exists( 'stream_set_read_buffer' ) || $forceStrong )
|
|
|
|
|
) {
|
2012-03-20 05:17:40 +00:00
|
|
|
$rem = $bytes - strlen( $buffer );
|
|
|
|
|
if ( !function_exists( 'stream_set_read_buffer' ) && $forceStrong ) {
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": Was forced to read from /dev/urandom " .
|
|
|
|
|
"without control over the buffer size.\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
// /dev/urandom is generally considered the best possible commonly
|
|
|
|
|
// available random source, and is available on most *nix systems.
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2012-03-20 05:17:40 +00:00
|
|
|
$urandom = fopen( "/dev/urandom", "rb" );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2012-03-20 05:17:40 +00:00
|
|
|
|
|
|
|
|
// Attempt to read all our random data from urandom
|
|
|
|
|
// php's fread always does buffered reads based on the stream's chunk_size
|
|
|
|
|
// so in reality it will usually read more than the amount of data we're
|
|
|
|
|
// asked for and not storing that risks depleting the system's random pool.
|
|
|
|
|
// If stream_set_read_buffer is available set the chunk_size to the amount
|
|
|
|
|
// of data we need. Otherwise read 8k, php's default chunk_size.
|
|
|
|
|
if ( $urandom ) {
|
|
|
|
|
// php's default chunk_size is 8k
|
|
|
|
|
$chunk_size = 1024 * 8;
|
|
|
|
|
if ( function_exists( 'stream_set_read_buffer' ) ) {
|
|
|
|
|
// If possible set the chunk_size to the amount of data we need
|
|
|
|
|
stream_set_read_buffer( $urandom, $rem );
|
|
|
|
|
$chunk_size = $rem;
|
|
|
|
|
}
|
|
|
|
|
$random_bytes = fread( $urandom, max( $chunk_size, $rem ) );
|
|
|
|
|
$buffer .= $random_bytes;
|
|
|
|
|
fclose( $urandom );
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": /dev/urandom generated " . strlen( $random_bytes ) .
|
|
|
|
|
" bytes of randomness.\n" );
|
|
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
if ( strlen( $buffer ) >= $bytes ) {
|
|
|
|
|
// urandom is always strong, set to true if all our data was generated using it
|
|
|
|
|
$this->strong = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( __METHOD__ . ": /dev/urandom could not be opened.\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we cannot use or generate enough data from a secure source
|
|
|
|
|
// use this loop to generate a good set of pseudo random data.
|
|
|
|
|
// This works by initializing a random state using a pile of unstable data
|
|
|
|
|
// and continually shoving it through a hash along with a variable salt.
|
|
|
|
|
// We hash the random state with more salt to avoid the state from leaking
|
|
|
|
|
// out and being used to predict the /randomness/ that follows.
|
|
|
|
|
if ( strlen( $buffer ) < $bytes ) {
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ .
|
|
|
|
|
": Falling back to using a pseudo random state to generate randomness.\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
while ( strlen( $buffer ) < $bytes ) {
|
2016-07-18 19:26:07 +00:00
|
|
|
$buffer .= MWCryptHash::hmac( $this->randomState(), strval( mt_rand() ) );
|
2012-03-20 05:17:40 +00:00
|
|
|
// This code is never really cryptographically strong, if we use it
|
|
|
|
|
// at all, then set strong to false.
|
|
|
|
|
$this->strong = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Once the buffer has been filled up with enough random data to fulfill
|
|
|
|
|
// the request shift off enough data to handle the request and leave the
|
|
|
|
|
// unused portion left inside the buffer for the next request for random data
|
|
|
|
|
$generated = substr( $buffer, 0, $bytes );
|
|
|
|
|
$buffer = substr( $buffer, $bytes );
|
|
|
|
|
|
2013-11-04 10:02:48 +00:00
|
|
|
wfDebug( __METHOD__ . ": " . strlen( $buffer ) .
|
|
|
|
|
" bytes of randomness leftover in the buffer.\n" );
|
2012-03-20 05:17:40 +00:00
|
|
|
|
|
|
|
|
return $generated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see self::generateHex()
|
|
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public function realGenerateHex( $chars, $forceStrong = false ) {
|
2012-03-20 05:17:40 +00:00
|
|
|
// hex strings are 2x the length of raw binary so we divide the length in half
|
|
|
|
|
// odd numbers will result in a .5 that leads the generate() being 1 character
|
|
|
|
|
// short, so we use ceil() to ensure that we always have enough bytes
|
|
|
|
|
$bytes = ceil( $chars / 2 );
|
|
|
|
|
// Generate the data and then convert it to a hex string
|
2012-03-21 10:27:34 +00:00
|
|
|
$hex = bin2hex( $this->generate( $bytes, $forceStrong ) );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
// A bit of paranoia here, the caller asked for a specific length of string
|
|
|
|
|
// here, and it's possible (eg when given an odd number) that we may actually
|
|
|
|
|
// have at least 1 char more than they asked for. Just in case they made this
|
|
|
|
|
// call intending to insert it into a database that does truncation we don't
|
|
|
|
|
// want to give them too much and end up with their database and their live
|
|
|
|
|
// code having two different values because part of what we gave them is truncated
|
|
|
|
|
// hence, we strip out any run of characters longer than what we were asked for.
|
|
|
|
|
return substr( $hex, 0, $chars );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Publicly exposed static methods **/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a singleton instance of MWCryptRand
|
2012-03-23 00:45:17 +00:00
|
|
|
* @return MWCryptRand
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
|
|
|
|
protected static function singleton() {
|
|
|
|
|
if ( is_null( self::$singleton ) ) {
|
|
|
|
|
self::$singleton = new self;
|
|
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
return self::$singleton;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a boolean indicating whether or not the source used for cryptographic
|
|
|
|
|
* random bytes generation in the previously run generate* call
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
|
|
|
|
* @return bool Returns true if the source was strong, false if not.
|
|
|
|
|
*/
|
|
|
|
|
public static function wasStrong() {
|
|
|
|
|
return self::singleton()->realWasStrong();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a run of (ideally) cryptographically random data and return
|
|
|
|
|
* it in raw binary form.
|
|
|
|
|
* You can use MWCryptRand::wasStrong() if you wish to know if the source used
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $bytes The number of bytes of random data to generate
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool $forceStrong Pass true if you want generate to prefer cryptographically
|
2012-03-20 05:17:40 +00:00
|
|
|
* strong sources of entropy even if reading from them may steal
|
|
|
|
|
* more entropy from the system than optimal.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @return string Raw binary random data
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public static function generate( $bytes, $forceStrong = false ) {
|
|
|
|
|
return self::singleton()->realGenerate( $bytes, $forceStrong );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a run of (ideally) cryptographically random data and return
|
|
|
|
|
* it in hexadecimal string format.
|
|
|
|
|
* You can use MWCryptRand::wasStrong() if you wish to know if the source used
|
|
|
|
|
* was cryptographically strong.
|
|
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $chars The number of hex chars of random data to generate
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool $forceStrong Pass true if you want generate to prefer cryptographically
|
2012-03-20 05:17:40 +00:00
|
|
|
* strong sources of entropy even if reading from them may steal
|
|
|
|
|
* more entropy from the system than optimal.
|
2014-07-21 18:58:14 +00:00
|
|
|
* @return string Hexadecimal random data
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
2012-03-21 10:27:34 +00:00
|
|
|
public static function generateHex( $chars, $forceStrong = false ) {
|
|
|
|
|
return self::singleton()->realGenerateHex( $chars, $forceStrong );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
}
|