67 $state .= rand() . uniqid( mt_rand(),
true );
77 $files[] = dirname( __DIR__ );
84 if ( defined(
'MW_CONFIG_FILE' ) ) {
89 MediaWiki\suppressWarnings();
90 $stat = stat( $file );
91 MediaWiki\restoreWarnings();
94 foreach ( $stat
as $k => $v ) {
95 if ( is_numeric( $k ) ) {
100 $path = realpath( $file );
101 if (
$path !==
false ) {
106 $state .= implode(
'', $stat );
116 if ( function_exists(
'getmypid' ) ) {
117 $state .= getmypid();
122 if ( function_exists(
'memory_get_usage' ) ) {
123 $state .= memory_get_usage(
true );
131 if ( $wgSecretKey ) {
148 $minIterations = self::MIN_ITERATIONS;
152 $bufLength = 10000000;
153 $buffer = str_repeat(
' ', $bufLength );
158 $startTime = microtime(
true );
159 $currentTime = $startTime;
160 while ( $iterations < $minIterations || $currentTime - $startTime < $duration ) {
163 $bufPos = ( $bufPos + 13 ) % $bufLength;
166 $nextTime = microtime(
true );
167 $delta = (int)( ( $nextTime - $currentTime ) * 1000000 );
170 if ( $iterations % 100 === 0 ) {
171 $data = sha1( $data );
173 $currentTime = $nextTime;
176 $timeTaken = $currentTime - $startTime;
179 wfDebug( __METHOD__ .
": Clock drift calculation " .
180 "(time-taken=" . ( $timeTaken * 1000 ) .
"ms, " .
181 "iterations=$iterations, " .
182 "time-per-iteration=" . ( $timeTaken / $iterations * 1e6 ) .
"us)\n" );
192 static $state = null;
193 if ( is_null( $state ) ) {
210 if ( is_null( $this->strong ) ) {
211 throw new MWException( __METHOD__ .
' called before generation of random data' );
222 wfDebug( __METHOD__ .
": Generating cryptographic random bytes for " .
225 $bytes = floor( $bytes );
227 if ( is_null( $this->strong ) ) {
229 $this->strong =
true;
232 if ( strlen(
$buffer ) < $bytes ) {
239 if ( function_exists(
'mcrypt_create_iv' ) ) {
240 $rem = $bytes - strlen(
$buffer );
241 $iv = mcrypt_create_iv( $rem, MCRYPT_DEV_URANDOM );
242 if ( $iv ===
false ) {
243 wfDebug( __METHOD__ .
": mcrypt_create_iv returned false.\n" );
246 wfDebug( __METHOD__ .
": mcrypt_create_iv generated " . strlen( $iv ) .
247 " bytes of randomness.\n" );
252 if ( strlen(
$buffer ) < $bytes ) {
253 if ( function_exists(
'openssl_random_pseudo_bytes' ) ) {
254 $rem = $bytes - strlen(
$buffer );
255 $openssl_bytes = openssl_random_pseudo_bytes( $rem, $openssl_strong );
256 if ( $openssl_bytes ===
false ) {
257 wfDebug( __METHOD__ .
": openssl_random_pseudo_bytes returned false.\n" );
260 wfDebug( __METHOD__ .
": openssl_random_pseudo_bytes generated " .
261 strlen( $openssl_bytes ) .
" bytes of " .
262 ( $openssl_strong ?
"strong" :
"weak" ) .
" randomness.\n" );
264 if ( strlen(
$buffer ) >= $bytes ) {
267 $this->strong = !!$openssl_strong;
273 if ( strlen(
$buffer ) < $bytes &&
274 ( function_exists(
'stream_set_read_buffer' ) || $forceStrong )
276 $rem = $bytes - strlen(
$buffer );
277 if ( !function_exists(
'stream_set_read_buffer' ) && $forceStrong ) {
278 wfDebug( __METHOD__ .
": Was forced to read from /dev/urandom " .
279 "without control over the buffer size.\n" );
283 MediaWiki\suppressWarnings();
284 $urandom = fopen(
"/dev/urandom",
"rb" );
285 MediaWiki\restoreWarnings();
295 $chunk_size = 1024 * 8;
296 if ( function_exists(
'stream_set_read_buffer' ) ) {
298 stream_set_read_buffer( $urandom, $rem );
301 $random_bytes = fread( $urandom, max( $chunk_size, $rem ) );
304 wfDebug( __METHOD__ .
": /dev/urandom generated " . strlen( $random_bytes ) .
305 " bytes of randomness.\n" );
307 if ( strlen(
$buffer ) >= $bytes ) {
309 $this->strong =
true;
312 wfDebug( __METHOD__ .
": /dev/urandom could not be opened.\n" );
322 if ( strlen(
$buffer ) < $bytes ) {
324 ": Falling back to using a pseudo random state to generate randomness.\n" );
326 while ( strlen(
$buffer ) < $bytes ) {
330 $this->strong =
false;
336 $generated = substr(
$buffer, 0, $bytes );
340 " bytes of randomness leftover in the buffer.\n" );
352 $bytes = ceil( $chars / 2 );
354 $hex = bin2hex( $this->
generate( $bytes, $forceStrong ) );
363 return substr( $hex, 0, $chars );
373 if ( is_null( self::$singleton ) ) {
374 self::$singleton =
new self;
377 return self::$singleton;
388 return self::singleton()->realWasStrong();
403 public static function generate( $bytes, $forceStrong =
false ) {
404 return self::singleton()->realGenerate( $bytes, $forceStrong );
419 public static function generateHex( $chars, $forceStrong =
false ) {
420 return self::singleton()->realGenerateHex( $chars, $forceStrong );
const MIN_ITERATIONS
Minimum number of iterations we want to make in our drift calculations.
wfHostname()
Fetch server name for use in error reporting etc.
$wgSecretKey
This should always be customised in LocalSettings.php.
static wasStrong()
Return a boolean indicating whether or not the source used for cryptographic random bytes generation ...
when a variable name is used in a it is silently declared as a new local masking the global
static hash($data, $raw=true)
Generate an acceptably unstable one-way-hash of some text making use of the best hash algorithm that ...
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
$strong
A boolean indicating whether the previous random generation was done using cryptographically strong r...
initialRandomState()
Initialize an initial random state based off of whatever we can find.
randomState()
Return a rolling random state initially build using data from unstable sources.
static singleton()
Publicly exposed static methods.
realGenerate($bytes, $forceStrong=false)
realGenerateHex($chars, $forceStrong=false)
const MSEC_PER_BYTE
Number of milliseconds we want to spend generating each separate byte of the final generated bytes...
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
static hashLength($raw=true)
Return the byte-length output of the hash algorithm we are using in self::hash and self::hmac...
wfGetAllCallers($limit=3)
Return a string consisting of callers in the stack.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
static hmac($data, $key, $raw=true)
Generate an acceptably unstable one-way-hmac of some text making use of the best hash algorithm that ...
static $singleton
Singleton instance for public use.
static generateHex($chars, $forceStrong=false)
Generate a run of (ideally) cryptographically random data and return it in hexadecimal string format...
driftHash($data)
Randomly hash data while mixing in clock drift data for randomness.
static generate($bytes, $forceStrong=false)
Generate a run of (ideally) cryptographically random data and return it in raw binary form...