7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
26 require_once 'Zend/Crypt.php';
29 * PHP implementation of the RFC 2104 Hash based Message Authentication Code
32 * @todo Patch for refactoring failed tests (key block sizes >80 using internal algo)
33 * @todo Check if mhash() is a required alternative (will be PECL-only soon)
36 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Crypt_Hmac extends Zend_Crypt
43 * The key to use for the hash
47 protected static $_key = null;
50 * pack() format to be used for current hashing method
54 protected static $_packFormat = null;
57 * Hashing algorithm; can be the md5/sha1 functions or any algorithm name
58 * listed in the output of PHP 5.1.2+ hash_algos().
62 protected static $_hashAlgorithm = 'md5';
65 * List of algorithms supported my mhash()
69 protected static $_supportedMhashAlgorithms = array('adler32',' crc32', 'crc32b', 'gost',
70 'haval128', 'haval160', 'haval192', 'haval256', 'md4', 'md5', 'ripemd160',
71 'sha1', 'sha256', 'tiger', 'tiger128', 'tiger160');
74 * Constants representing the output mode of the hash algorithm
76 const STRING = 'string';
77 const BINARY = 'binary';
80 * Performs a HMAC computation given relevant details such as Key, Hashing
81 * algorithm, the data to compute MAC of, and an output format of String,
82 * Binary notation or BTWOC.
87 * @param string $output
88 * @param boolean $internal
91 public static function compute($key, $hash, $data, $output = self::STRING)
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');
101 self::_setHashAlgorithm($hash);
103 // perform hashing and return
104 return self::_hash($data, $output);
108 * Setter for the hash method.
110 * @param string $hash
111 * @return Zend_Crypt_Hmac
113 protected static function _setHashAlgorithm($hash)
115 if (!isset($hash) || empty($hash)) {
116 require_once 'Zend/Crypt/Hmac/Exception.php';
117 throw new Zend_Crypt_Hmac_Exception('provided hash string is null or empty');
120 $hash = strtolower($hash);
121 $hashSupported = false;
123 if (function_exists('hash_algos') && in_array($hash, hash_algos())) {
124 $hashSupported = true;
127 if ($hashSupported === false && function_exists('mhash') && in_array($hash, self::$_supportedAlgosMhash)) {
128 $hashSupported = true;
131 if ($hashSupported === false) {
132 require_once 'Zend/Crypt/Hmac/Exception.php';
133 throw new Zend_Crypt_Hmac_Exception('hash algorithm provided is not supported on this PHP installation; please enable the hash or mhash extensions');
135 self::$_hashAlgorithm = $hash;
139 * Perform HMAC and return the keyed data
141 * @param string $data
142 * @param string $output
143 * @param bool $internal Option to not use hash() functions for testing
146 protected static function _hash($data, $output = self::STRING, $internal = false)
148 if (function_exists('hash_hmac')) {
149 if ($output == self::BINARY) {
150 return hash_hmac(self::$_hashAlgorithm, $data, self::$_key, 1);
152 return hash_hmac(self::$_hashAlgorithm, $data, self::$_key);
155 if (function_exists('mhash')) {
156 if ($output == self::BINARY) {
157 return mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
159 $bin = mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
160 return bin2hex($bin);
165 * Since MHASH accepts an integer constant representing the hash algorithm
166 * we need to make a small detour to get the correct integer matching our
169 * @param string $hashAlgorithm
172 protected static function _getMhashDefinition($hashAlgorithm)
174 for ($i = 0; $i <= mhash_count(); $i++)
176 $types[mhash_get_hash_name($i)] = $i;
178 return $types[strtoupper($hashAlgorithm)];