3 * Utility functions for generating hashes
5 * This is based in part on Drupal code as well as what we used in our own code
6 * prior to introduction of this class, by way of MWCryptRand.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 * The hash algorithm being used
30 protected static $algo = null;
33 * The number of bytes outputted by the hash algorithm
35 protected static $hashLength = [
41 * Decide on the best acceptable hash algorithm we have available for hash()
42 * @return string A hash algorithm
44 public static function hashAlgo() {
45 if ( !is_null( self
::$algo ) ) {
49 $algos = hash_algos();
50 $preference = [ 'whirlpool', 'sha256', 'sha1', 'md5' ];
52 foreach ( $preference as $algorithm ) {
53 if ( in_array( $algorithm, $algos ) ) {
54 self
::$algo = $algorithm;
55 wfDebug( __METHOD__
. ': Using the ' . self
::$algo . " hash algorithm.\n" );
61 // We only reach here if no acceptable hash is found in the list, this should
62 // be a technical impossibility since most of php's hash list is fixed and
63 // some of the ones we list are available as their own native functions
64 // But since we already require at least 5.2 and hash() was default in
65 // 5.1.2 we don't bother falling back to methods like sha1 and md5.
66 throw new DomainException( "Could not find an acceptable hashing function in hash_algos()" );
70 * Return the byte-length output of the hash algorithm we are
71 * using in self::hash and self::hmac.
73 * @param bool $raw True to return the length for binary data, false to
74 * return for hex-encoded
75 * @return int Number of bytes the hash outputs
77 public static function hashLength( $raw = true ) {
79 if ( is_null( self
::$hashLength[$raw] ) ) {
80 self
::$hashLength[$raw] = strlen( self
::hash( '', $raw ) );
83 return self
::$hashLength[$raw];
87 * Generate an acceptably unstable one-way-hash of some text
88 * making use of the best hash algorithm that we have available.
91 * @param bool $raw True to return binary data, false to return it hex-encoded
92 * @return string A hash of the data
94 public static function hash( $data, $raw = true ) {
95 return hash( self
::hashAlgo(), $data, $raw );
99 * Generate an acceptably unstable one-way-hmac of some text
100 * making use of the best hash algorithm that we have available.
102 * @param string $data
104 * @param bool $raw True to return binary data, false to return it hex-encoded
105 * @return string An hmac hash of the data + key
107 public static function hmac( $data, $key, $raw = true ) {
108 if ( !is_string( $key ) ) {
109 // a fatal error in HHVM; an exception will at least give us a stack trace
110 throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) );
112 return hash_hmac( self
::hashAlgo(), $data, $key, $raw );