PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Crypt/Hmac.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 181 lines | 67 code | 19 blank | 95 comment | 17 complexity | 263965ebc65320101b7b812674d27c35 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage Hmac
  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
  20. */
  21. /**
  22. * @see Zend_Crypt
  23. */
  24. require_once 'Zend/Crypt.php';
  25. /**
  26. * PHP implementation of the RFC 2104 Hash based Message Authentication Code
  27. * algorithm.
  28. *
  29. * @todo Patch for refactoring failed tests (key block sizes >80 using internal algo)
  30. * @todo Check if mhash() is a required alternative (will be PECL-only soon)
  31. * @category Zend
  32. * @package Zend_Crypt
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Crypt_Hmac extends Zend_Crypt
  37. {
  38. /**
  39. * The key to use for the hash
  40. *
  41. * @var string
  42. */
  43. protected static $_key = null;
  44. /**
  45. * pack() format to be used for current hashing method
  46. *
  47. * @var string
  48. */
  49. protected static $_packFormat = null;
  50. /**
  51. * Hashing algorithm; can be the md5/sha1 functions or any algorithm name
  52. * listed in the output of PHP 5.1.2+ hash_algos().
  53. *
  54. * @var string
  55. */
  56. protected static $_hashAlgorithm = 'md5';
  57. /**
  58. * List of algorithms supported my mhash()
  59. *
  60. * @var array
  61. */
  62. protected static $_supportedMhashAlgorithms = array('adler32',' crc32', 'crc32b', 'gost',
  63. 'haval128', 'haval160', 'haval192', 'haval256', 'md4', 'md5', 'ripemd160',
  64. 'sha1', 'sha256', 'tiger', 'tiger128', 'tiger160');
  65. /**
  66. * Constants representing the output mode of the hash algorithm
  67. */
  68. const STRING = 'string';
  69. const BINARY = 'binary';
  70. /**
  71. * Performs a HMAC computation given relevant details such as Key, Hashing
  72. * algorithm, the data to compute MAC of, and an output format of String,
  73. * Binary notation or BTWOC.
  74. *
  75. * @param string $key
  76. * @param string $hash
  77. * @param string $data
  78. * @param string $output
  79. * @param boolean $internal
  80. * @return string
  81. */
  82. public static function compute($key, $hash, $data, $output = self::STRING)
  83. {
  84. // set the key
  85. if (!isset($key) || empty($key)) {
  86. require_once 'Zend/Crypt/Hmac/Exception.php';
  87. throw new Zend_Crypt_Hmac_Exception('provided key is null or empty');
  88. }
  89. self::$_key = $key;
  90. // set the hash
  91. self::_setHashAlgorithm($hash);
  92. // perform hashing and return
  93. return self::_hash($data, $output);
  94. }
  95. /**
  96. * Setter for the hash method.
  97. *
  98. * @param string $hash
  99. * @return Zend_Crypt_Hmac
  100. */
  101. protected static function _setHashAlgorithm($hash)
  102. {
  103. if (!isset($hash) || empty($hash)) {
  104. require_once 'Zend/Crypt/Hmac/Exception.php';
  105. throw new Zend_Crypt_Hmac_Exception('provided hash string is null or empty');
  106. }
  107. $hash = strtolower($hash);
  108. $hashSupported = false;
  109. if (function_exists('hash_algos') && in_array($hash, hash_algos())) {
  110. $hashSupported = true;
  111. }
  112. if ($hashSupported === false && function_exists('mhash') && in_array($hash, self::$_supportedAlgosMhash)) {
  113. $hashSupported = true;
  114. }
  115. if ($hashSupported === false) {
  116. require_once 'Zend/Crypt/Hmac/Exception.php';
  117. throw new Zend_Crypt_Hmac_Exception('hash algorithm provided is not supported on this PHP installation; please enable the hash or mhash extensions');
  118. }
  119. self::$_hashAlgorithm = $hash;
  120. }
  121. /**
  122. * Perform HMAC and return the keyed data
  123. *
  124. * @param string $data
  125. * @param string $output
  126. * @param bool $internal Option to not use hash() functions for testing
  127. * @return string
  128. */
  129. protected static function _hash($data, $output = self::STRING, $internal = false)
  130. {
  131. if (function_exists('hash_hmac')) {
  132. if ($output == self::BINARY) {
  133. return hash_hmac(self::$_hashAlgorithm, $data, self::$_key, 1);
  134. }
  135. return hash_hmac(self::$_hashAlgorithm, $data, self::$_key);
  136. }
  137. if (function_exists('mhash')) {
  138. if ($output == self::BINARY) {
  139. return mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
  140. }
  141. $bin = mhash(self::_getMhashDefinition(self::$_hashAlgorithm), $data, self::$_key);
  142. return bin2hex($bin);
  143. }
  144. }
  145. /**
  146. * Since MHASH accepts an integer constant representing the hash algorithm
  147. * we need to make a small detour to get the correct integer matching our
  148. * algorithm's name.
  149. *
  150. * @param string $hashAlgorithm
  151. * @return integer
  152. */
  153. protected static function _getMhashDefinition($hashAlgorithm)
  154. {
  155. for ($i = 0; $i <= mhash_count(); $i++)
  156. {
  157. $types[mhash_get_hash_name($i)] = $i;
  158. }
  159. return $types[strtoupper($hashAlgorithm)];
  160. }
  161. }