Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Hmac.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Crypt.php';
27 
40 {
41 
47  protected static $_key = null;
48 
54  protected static $_packFormat = null;
55 
62  protected static $_hashAlgorithm = 'md5';
63 
69  protected static $_supportedMhashAlgorithms = array('adler32',' crc32', 'crc32b', 'gost',
70  'haval128', 'haval160', 'haval192', 'haval256', 'md4', 'md5', 'ripemd160',
71  'sha1', 'sha256', 'tiger', 'tiger128', 'tiger160');
72 
76  const STRING = 'string';
77  const BINARY = 'binary';
78 
91  public static function compute($key, $hash, $data, $output = self::STRING)
92  {
93  // set the key
94  if (!isset($key) || empty($key)) {
95  #require_once 'Zend/Crypt/Hmac/Exception.php';
96  throw new Zend_Crypt_Hmac_Exception('provided key is null or empty');
97  }
98  self::$_key = $key;
99 
100  // set the hash
102 
103  // perform hashing and return
104  return self::_hash($data, $output);
105  }
106 
114  protected static function _setHashAlgorithm($hash)
115  {
116  if (!isset($hash) || empty($hash)) {
117  #require_once 'Zend/Crypt/Hmac/Exception.php';
118  throw new Zend_Crypt_Hmac_Exception('provided hash string is null or empty');
119  }
120 
121  $hash = strtolower($hash);
122  $hashSupported = false;
123 
124  if (function_exists('hash_algos') && in_array($hash, hash_algos())) {
125  $hashSupported = true;
126  }
127 
128  if ($hashSupported === false && function_exists('mhash') && in_array($hash, self::$_supportedAlgosMhash)) {
129  $hashSupported = true;
130  }
131 
132  if ($hashSupported === false) {
133  #require_once 'Zend/Crypt/Hmac/Exception.php';
134  throw new Zend_Crypt_Hmac_Exception('hash algorithm provided is not supported on this PHP installation; please enable the hash or mhash extensions');
135  }
136  self::$_hashAlgorithm = $hash;
137  }
138 
147  protected static function _hash($data, $output = self::STRING, $internal = false)
148  {
149  if (function_exists('hash_hmac')) {
150  if ($output == self::BINARY) {
151  return hash_hmac(self::$_hashAlgorithm, $data, self::$_key, true);
152  }
153  return hash_hmac(self::$_hashAlgorithm, $data, self::$_key);
154  }
155 
156  if (function_exists('mhash')) {
157  if ($output == self::BINARY) {
158  return mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
159  }
160  $bin = mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
161  return bin2hex($bin);
162  }
163  }
164 
173  protected static function _getMhashDefinition($hashAlgorithm)
174  {
175  for ($i = 0; $i <= mhash_count(); $i++)
176  {
177  $types[mhash_get_hash_name($i)] = $i;
178  }
179  return $types[strtoupper($hashAlgorithm)];
180  }
181 
182 }
static _hash($data, $output=self::STRING, $internal=false)
Definition: Hmac.php:147
const BINARY
Definition: Hmac.php:77
static $_hashAlgorithm
Definition: Hmac.php:62
static _getMhashDefinition($hashAlgorithm)
Definition: Hmac.php:173
const STRING
Definition: Hmac.php:76
static compute($key, $hash, $data, $output=self::STRING)
Definition: Hmac.php:91
static $_key
Definition: Hmac.php:47
static $_supportedMhashAlgorithms
Definition: Hmac.php:69
static $_packFormat
Definition: Hmac.php:54
static _setHashAlgorithm($hash)
Definition: Hmac.php:114
$i
Definition: gallery.phtml:31