Add CollationFa
[lhc/web/wiklou.git] / includes / utils / MWCryptHash.php
1 <?php
2 /**
3 * Utility functions for generating hashes
4 *
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.
7 *
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.
12 *
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.
17 *
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
22 *
23 * @file
24 */
25
26 class MWCryptHash {
27 /**
28 * The hash algorithm being used
29 */
30 protected static $algo = null;
31
32 /**
33 * The number of bytes outputted by the hash algorithm
34 */
35 protected static $hashLength = [
36 true => null,
37 false => null,
38 ];
39
40 /**
41 * Decide on the best acceptable hash algorithm we have available for hash()
42 * @return string A hash algorithm
43 */
44 public static function hashAlgo() {
45 if ( !is_null( self::$algo ) ) {
46 return self::$algo;
47 }
48
49 $algos = hash_algos();
50 $preference = [ 'whirlpool', 'sha256', 'sha1', 'md5' ];
51
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" );
56
57 return self::$algo;
58 }
59 }
60
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()" );
67 }
68
69 /**
70 * Return the byte-length output of the hash algorithm we are
71 * using in self::hash and self::hmac.
72 *
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
76 */
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 ) );
81 }
82
83 return self::$hashLength[$raw];
84 }
85
86 /**
87 * Generate an acceptably unstable one-way-hash of some text
88 * making use of the best hash algorithm that we have available.
89 *
90 * @param string $data
91 * @param bool $raw True to return binary data, false to return it hex-encoded
92 * @return string A hash of the data
93 */
94 public static function hash( $data, $raw = true ) {
95 return hash( self::hashAlgo(), $data, $raw );
96 }
97
98 /**
99 * Generate an acceptably unstable one-way-hmac of some text
100 * making use of the best hash algorithm that we have available.
101 *
102 * @param string $data
103 * @param string $key
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
106 */
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 ) );
111 }
112 return hash_hmac( self::hashAlgo(), $data, $key, $raw );
113 }
114
115 }