Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / libs / 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
56 return self::$algo;
57 }
58 }
59
60 // We only reach here if no acceptable hash is found in the list, this should
61 // be a technical impossibility since most of php's hash list is fixed and
62 // some of the ones we list are available as their own native functions
63 // But since we already require at least 5.2 and hash() was default in
64 // 5.1.2 we don't bother falling back to methods like sha1 and md5.
65 throw new DomainException( "Could not find an acceptable hashing function in hash_algos()" );
66 }
67
68 /**
69 * Return the byte-length output of the hash algorithm we are
70 * using in self::hash and self::hmac.
71 *
72 * @param bool $raw True to return the length for binary data, false to
73 * return for hex-encoded
74 * @return int Number of bytes the hash outputs
75 */
76 public static function hashLength( $raw = true ) {
77 $raw = (bool)$raw;
78 if ( is_null( self::$hashLength[$raw] ) ) {
79 self::$hashLength[$raw] = strlen( self::hash( '', $raw ) );
80 }
81
82 return self::$hashLength[$raw];
83 }
84
85 /**
86 * Generate an acceptably unstable one-way-hash of some text
87 * making use of the best hash algorithm that we have available.
88 *
89 * @param string $data
90 * @param bool $raw True to return binary data, false to return it hex-encoded
91 * @return string A hash of the data
92 */
93 public static function hash( $data, $raw = true ) {
94 return hash( self::hashAlgo(), $data, $raw );
95 }
96
97 /**
98 * Generate an acceptably unstable one-way-hmac of some text
99 * making use of the best hash algorithm that we have available.
100 *
101 * @param string $data
102 * @param string $key
103 * @param bool $raw True to return binary data, false to return it hex-encoded
104 * @return string An hmac hash of the data + key
105 */
106 public static function hmac( $data, $key, $raw = true ) {
107 if ( !is_string( $key ) ) {
108 // a fatal error in HHVM; an exception will at least give us a stack trace
109 throw new InvalidArgumentException( 'Invalid key type: ' . gettype( $key ) );
110 }
111 return hash_hmac( self::hashAlgo(), $data, $key, $raw );
112 }
113
114 }