wiki.techinc.nl/includes/libs/MWCryptHash.php
Kevin Israel 21d9c94f7b MWCryptHash: Clean up check for acceptable hashing algorithms
* Replaced use of hash_algos() with hash_hmac_algos(), which was added
  in PHP 7.2 and omits non-cryptographic hashing algorithms that are not
  supported by hash_hmac().
* Removed MD5 and SHA-1 from the list of acceptable hash functions.
  These functions are cryptographically broken and are not being used
  anyway; all supported PHP versions support Whirlpool and SHA-256.
* Made the in_array() comparison strict as per coding conventions.

Change-Id: I3a3d43a790f71a4875a39c8788bef67ecf0181f0
2022-07-10 05:15:56 -04:00

109 lines
3.3 KiB
PHP

<?php
/**
* Utility functions for generating hashes
*
* This is based in part on Drupal code as well as what we used in our own code
* prior to introduction of this class, by way of MWCryptRand.
*
* 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
*
* @file
*/
class MWCryptHash {
/**
* The hash algorithm being used
*/
protected static $algo = null;
/**
* The number of bytes outputted by the hash algorithm
*/
protected static $hashLength = [
'binary' => null,
'hex' => null,
];
/**
* Decide on the best acceptable hash algorithm we have available for hash()
* @return string A hash algorithm
*/
public static function hashAlgo() {
if ( self::$algo !== null ) {
return self::$algo;
}
$algos = hash_hmac_algos();
$preference = [ 'whirlpool', 'sha256' ];
foreach ( $preference as $algorithm ) {
if ( in_array( $algorithm, $algos, true ) ) {
self::$algo = $algorithm;
return self::$algo;
}
}
throw new DomainException( 'Could not find an acceptable hashing function.' );
}
/**
* 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 ) {
$key = $raw ? 'binary' : 'hex';
if ( self::$hashLength[$key] === null ) {
self::$hashLength[$key] = strlen( self::hash( '', $raw ) );
}
// @phan-suppress-next-line PhanTypeMismatchReturnNullable False positive
return self::$hashLength[$key];
}
/**
* Generate a cryptographic hash value (message digest) for a string,
* 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 a keyed cryptographic hash value (HMAC) for a string,
* 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 ) ) {
// hash_hmac tolerates non-string (would return null with warning)
throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) );
}
return hash_hmac( self::hashAlgo(), $data, $key, $raw );
}
}