*/
protected static $singleton = null;
- /**
- * The hash algorithm being used
- */
- protected $algo = null;
-
- /**
- * The number of bytes outputted by the hash algorithm
- */
- protected $hashLength = null;
-
/**
* A boolean indicating whether the previous random generation was done using
* cryptographically strong random number generator or not.
// loop to gather little entropy)
$minIterations = self::MIN_ITERATIONS;
// Duration of time to spend doing calculations (in seconds)
- $duration = ( self::MSEC_PER_BYTE / 1000 ) * $this->hashLength();
+ $duration = ( self::MSEC_PER_BYTE / 1000 ) * MWCryptHash::hashLength();
// Create a buffer to use to trigger memory operations
$bufLength = 10000000;
$buffer = str_repeat( ' ', $bufLength );
$iterations++;
}
$timeTaken = $currentTime - $startTime;
- $data = $this->hash( $data );
+ $data = MWCryptHash::hash( $data );
wfDebug( __METHOD__ . ": Clock drift calculation " .
"(time-taken=" . ( $timeTaken * 1000 ) . "ms, " .
// 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
- $state = $this->hash( $this->initialRandomState() );
+ $state = MWCryptHash::hash( $this->initialRandomState() );
}
// Generate a new random state based on the initial random state or previous
// random state by combining it with clock drift
return $state;
}
- /**
- * Decide on the best acceptable hash algorithm we have available for hash()
- * @throws MWException
- * @return string A hash algorithm
- */
- protected function hashAlgo() {
- if ( !is_null( $this->algo ) ) {
- return $this->algo;
- }
-
- $algos = hash_algos();
- $preference = array( 'whirlpool', 'sha256', 'sha1', 'md5' );
-
- foreach ( $preference as $algorithm ) {
- if ( in_array( $algorithm, $algos ) ) {
- $this->algo = $algorithm;
- wfDebug( __METHOD__ . ": Using the {$this->algo} hash algorithm.\n" );
-
- return $this->algo;
- }
- }
-
- // We only reach here if no acceptable hash is found in the list, this should
- // be a technical impossibility since most of php's hash list is fixed and
- // some of the ones we list are available as their own native functions
- // But since we already require at least 5.2 and hash() was default in
- // 5.1.2 we don't bother falling back to methods like sha1 and md5.
- throw new MWException( "Could not find an acceptable hashing function in hash_algos()" );
- }
-
- /**
- * Return the byte-length output of the hash algorithm we are
- * using in self::hash and self::hmac.
- *
- * @return int Number of bytes the hash outputs
- */
- protected function hashLength() {
- if ( is_null( $this->hashLength ) ) {
- $this->hashLength = strlen( $this->hash( '' ) );
- }
-
- return $this->hashLength;
- }
-
- /**
- * Generate an acceptably unstable one-way-hash of some text
- * making use of the best hash algorithm that we have available.
- *
- * @param string $data
- * @return string A raw hash of the data
- */
- protected function hash( $data ) {
- return hash( $this->hashAlgo(), $data, true );
- }
-
- /**
- * Generate an acceptably unstable one-way-hmac of some text
- * making use of the best hash algorithm that we have available.
- *
- * @param string $data
- * @param string $key
- * @return string A raw hash of the data
- */
- protected function hmac( $data, $key ) {
- return hash_hmac( $this->hashAlgo(), $data, $key, true );
- }
-
/**
* @see self::wasStrong()
*/
": Falling back to using a pseudo random state to generate randomness.\n" );
}
while ( strlen( $buffer ) < $bytes ) {
- $buffer .= $this->hmac( $this->randomState(), mt_rand() );
+ $buffer .= MWCryptHash::hmac( $this->randomState(), mt_rand() );
// This code is never really cryptographically strong, if we use it
// at all, then set strong to false.
$this->strong = false;