From: Kunal Mehta Date: Sun, 2 Oct 2016 06:19:28 +0000 (-0700) Subject: Move MWCryptHash into libs/ X-Git-Tag: 1.31.0-rc.0~5227^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/modifier.php?a=commitdiff_plain;h=9300bc293378b215d8df2f44d7e00f00b0f739a1;p=lhc%2Fweb%2Fwiklou.git Move MWCryptHash into libs/ Remove the single wfDebug() call that was making this class MW specific, someone can log the return value of MWCryptHash::hashAlgo() if they'd like to know the specific implementation being used. Change-Id: Ibb7ead7594edab7861631046dd8316daab613401 --- diff --git a/autoload.php b/autoload.php index 8d9a80fe3b..5e81e833c9 100644 --- a/autoload.php +++ b/autoload.php @@ -771,7 +771,7 @@ $wgAutoloadLocalClasses = [ 'MWCallableUpdate' => __DIR__ . '/includes/deferred/MWCallableUpdate.php', 'MWContentSerializationException' => __DIR__ . '/includes/content/ContentHandler.php', 'MWCryptHKDF' => __DIR__ . '/includes/utils/MWCryptHKDF.php', - 'MWCryptHash' => __DIR__ . '/includes/utils/MWCryptHash.php', + 'MWCryptHash' => __DIR__ . '/includes/libs/MWCryptHash.php', 'MWCryptRand' => __DIR__ . '/includes/utils/MWCryptRand.php', 'MWDebug' => __DIR__ . '/includes/debug/MWDebug.php', 'MWDocGen' => __DIR__ . '/maintenance/mwdocgen.php', diff --git a/includes/libs/MWCryptHash.php b/includes/libs/MWCryptHash.php new file mode 100644 index 0000000000..f9b71729af --- /dev/null +++ b/includes/libs/MWCryptHash.php @@ -0,0 +1,114 @@ + null, + false => null, + ]; + + /** + * Decide on the best acceptable hash algorithm we have available for hash() + * @return string A hash algorithm + */ + public static function hashAlgo() { + if ( !is_null( self::$algo ) ) { + return self::$algo; + } + + $algos = hash_algos(); + $preference = [ 'whirlpool', 'sha256', 'sha1', 'md5' ]; + + foreach ( $preference as $algorithm ) { + if ( in_array( $algorithm, $algos ) ) { + self::$algo = $algorithm; + + return self::$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 DomainException( "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. + * + * @param bool $raw True to return the length for binary data, false to + * return for hex-encoded + * @return int Number of bytes the hash outputs + */ + public static function hashLength( $raw = true ) { + $raw = (bool)$raw; + if ( is_null( self::$hashLength[$raw] ) ) { + self::$hashLength[$raw] = strlen( self::hash( '', $raw ) ); + } + + return self::$hashLength[$raw]; + } + + /** + * Generate an acceptably unstable one-way-hash of some text + * making use of the best hash algorithm that we have available. + * + * @param string $data + * @param bool $raw True to return binary data, false to return it hex-encoded + * @return string A hash of the data + */ + public static function hash( $data, $raw = true ) { + return hash( self::hashAlgo(), $data, $raw ); + } + + /** + * 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 + * @param bool $raw True to return binary data, false to return it hex-encoded + * @return string An hmac hash of the data + key + */ + public static function hmac( $data, $key, $raw = true ) { + if ( !is_string( $key ) ) { + // a fatal error in HHVM; an exception will at least give us a stack trace + throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) ); + } + return hash_hmac( self::hashAlgo(), $data, $key, $raw ); + } + +} diff --git a/includes/utils/MWCryptHash.php b/includes/utils/MWCryptHash.php deleted file mode 100644 index 11173573e7..0000000000 --- a/includes/utils/MWCryptHash.php +++ /dev/null @@ -1,115 +0,0 @@ - null, - false => null, - ]; - - /** - * Decide on the best acceptable hash algorithm we have available for hash() - * @return string A hash algorithm - */ - public static function hashAlgo() { - if ( !is_null( self::$algo ) ) { - return self::$algo; - } - - $algos = hash_algos(); - $preference = [ 'whirlpool', 'sha256', 'sha1', 'md5' ]; - - foreach ( $preference as $algorithm ) { - if ( in_array( $algorithm, $algos ) ) { - self::$algo = $algorithm; - wfDebug( __METHOD__ . ': Using the ' . self::$algo . " hash algorithm.\n" ); - - return self::$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 DomainException( "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. - * - * @param bool $raw True to return the length for binary data, false to - * return for hex-encoded - * @return int Number of bytes the hash outputs - */ - public static function hashLength( $raw = true ) { - $raw = (bool)$raw; - if ( is_null( self::$hashLength[$raw] ) ) { - self::$hashLength[$raw] = strlen( self::hash( '', $raw ) ); - } - - return self::$hashLength[$raw]; - } - - /** - * Generate an acceptably unstable one-way-hash of some text - * making use of the best hash algorithm that we have available. - * - * @param string $data - * @param bool $raw True to return binary data, false to return it hex-encoded - * @return string A hash of the data - */ - public static function hash( $data, $raw = true ) { - return hash( self::hashAlgo(), $data, $raw ); - } - - /** - * 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 - * @param bool $raw True to return binary data, false to return it hex-encoded - * @return string An hmac hash of the data + key - */ - public static function hmac( $data, $key, $raw = true ) { - if ( !is_string( $key ) ) { - // a fatal error in HHVM; an exception will at least give us a stack trace - throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) ); - } - return hash_hmac( self::hashAlgo(), $data, $key, $raw ); - } - -}