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;
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()" );
69 * Return the byte-length output of the hash algorithm we are
70 * using in self::hash and self::hmac.
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
76 public static function hashLength( $raw = true ) {
78 if ( is_null( self
::$hashLength[$raw] ) ) {
79 self
::$hashLength[$raw] = strlen( self
::hash( '', $raw ) );
82 return self
::$hashLength[$raw];
86 * Generate an acceptably unstable one-way-hash of some text
87 * making use of the best hash algorithm that we have available.
90 * @param bool $raw True to return binary data, false to return it hex-encoded
91 * @return string A hash of the data
93 public static function hash( $data, $raw = true ) {
94 return hash( self
::hashAlgo(), $data, $raw );
98 * Generate an acceptably unstable one-way-hmac of some text
99 * making use of the best hash algorithm that we have available.
101 * @param string $data
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
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 ) );
111 return hash_hmac( self
::hashAlgo(), $data, $key, $raw );