Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / libs / MWCryptHash.php
blob215dee7ccf9355f06ee8bbb70f90833acbfbff23
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 ?string $algo = null;
32 /**
33 * The number of bytes outputted by the hash algorithm
35 protected static int $hashLength;
37 /**
38 * Decide on the best acceptable hash algorithm we have available for hash()
39 * @return string A hash algorithm
41 public static function hashAlgo() {
42 $algorithm = self::$algo;
43 if ( $algorithm !== null ) {
44 return $algorithm;
47 $algos = hash_hmac_algos();
48 $preference = [ 'whirlpool', 'sha256' ];
50 foreach ( $preference as $algorithm ) {
51 if ( in_array( $algorithm, $algos, true ) ) {
52 self::$algo = $algorithm;
53 return $algorithm;
57 throw new DomainException( 'Could not find an acceptable hashing function.' );
60 /**
61 * Return the byte-length output of the hash algorithm we are
62 * using in self::hash and self::hmac.
64 * @param bool $raw True to return the length for binary data, false to
65 * return for hex-encoded
66 * @return int Number of bytes the hash outputs
68 public static function hashLength( $raw = true ) {
69 self::$hashLength ??= strlen( self::hash( '', true ) );
70 // Optimisation: Skip computing the length of non-raw hashes.
71 // The algos in hashAlgo() all produce a digest that is a multiple
72 // of 8 bits, where hex is always twice the length of binary byte length.
73 return $raw ? self::$hashLength : self::$hashLength * 2;
76 /**
77 * Generate a cryptographic hash value (message digest) for a string,
78 * making use of the best hash algorithm that we have available.
80 * @param string $data
81 * @param bool $raw True to return binary data, false to return it hex-encoded
82 * @return string A hash of the data
84 public static function hash( $data, $raw = true ) {
85 return hash( self::hashAlgo(), $data, $raw );
88 /**
89 * Generate a keyed cryptographic hash value (HMAC) for a string,
90 * making use of the best hash algorithm that we have available.
92 * @param string $data
93 * @param string $key
94 * @param bool $raw True to return binary data, false to return it hex-encoded
95 * @return string An HMAC hash of the data + key
97 public static function hmac( $data, $key, $raw = true ) {
98 if ( !is_string( $key ) ) {
99 // hash_hmac tolerates non-string (would return null with warning)
100 throw new InvalidArgumentException( 'Invalid key type: ' . get_debug_type( $key ) );
102 return hash_hmac( self::hashAlgo(), $data, $key, $raw );