wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / utils / MWCryptHash.php
blobb46de60234ab4a937d21dc1753ec485fc7621cc5
1 <?php
2 /**
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
23 * @file
26 class MWCryptHash {
27 /**
28 * The hash algorithm being used
30 protected static $algo = null;
32 /**
33 * The number of bytes outputted by the hash algorithm
35 protected static $hashLength = array(
36 true => null,
37 false => null,
40 /**
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 ) ) {
46 return self::$algo;
49 $algos = hash_algos();
50 $preference = array( '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" );
57 return self::$algo;
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()" );
69 /**
70 * Return the byte-length output of the hash algorithm we are
71 * using in self::hash and self::hmac.
73 * @param boolean $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 ) {
78 $raw = (bool)$raw;
79 if ( is_null( self::$hashLength[$raw] ) ) {
80 self::$hashLength[$raw] = strlen( self::hash( '', $raw ) );
83 return self::$hashLength[$raw];
86 /**
87 * Generate an acceptably unstable one-way-hash of some text
88 * making use of the best hash algorithm that we have available.
90 * @param string $data
91 * @param boolean $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 );
98 /**
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
103 * @param string $key
104 * @param boolean $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 return hash_hmac( self::hashAlgo(), $data, $key, $raw );