HMAC.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * This is the HMACSHA1 implementation for the OpenID library.
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: See the COPYING file included in this distribution.
  8. *
  9. * @access private
  10. * @package OpenID
  11. * @author JanRain, Inc. <openid@janrain.com>
  12. * @copyright 2005-2008 Janrain, Inc.
  13. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  14. */
  15. require_once 'Auth/OpenID.php';
  16. /**
  17. * SHA1_BLOCKSIZE is this module's SHA1 blocksize used by the fallback
  18. * implementation.
  19. */
  20. define('Auth_OpenID_SHA1_BLOCKSIZE', 64);
  21. function Auth_OpenID_SHA1($text)
  22. {
  23. if (function_exists('hash') &&
  24. function_exists('hash_algos') &&
  25. (in_array('sha1', hash_algos()))) {
  26. // PHP 5 case (sometimes): 'hash' available and 'sha1' algo
  27. // supported.
  28. return hash('sha1', $text, true);
  29. } else if (function_exists('sha1')) {
  30. // PHP 4 case: 'sha1' available.
  31. $hex = sha1($text);
  32. $raw = '';
  33. for ($i = 0; $i < 40; $i += 2) {
  34. $hexcode = substr($hex, $i, 2);
  35. $charcode = (int)base_convert($hexcode, 16, 10);
  36. $raw .= chr($charcode);
  37. }
  38. return $raw;
  39. } else {
  40. // Explode.
  41. trigger_error('No SHA1 function found', E_USER_ERROR);
  42. }
  43. }
  44. /**
  45. * Compute an HMAC/SHA1 hash.
  46. *
  47. * @access private
  48. * @param string $key The HMAC key
  49. * @param string $text The message text to hash
  50. * @return string $mac The MAC
  51. */
  52. function Auth_OpenID_HMACSHA1($key, $text)
  53. {
  54. if (Auth_OpenID::bytes($key) > Auth_OpenID_SHA1_BLOCKSIZE) {
  55. $key = Auth_OpenID_SHA1($key, true);
  56. }
  57. if (function_exists('hash_hmac') &&
  58. function_exists('hash_algos') &&
  59. (in_array('sha1', hash_algos()))) {
  60. return hash_hmac('sha1', $text, $key, true);
  61. }
  62. // Home-made solution
  63. $key = str_pad($key, Auth_OpenID_SHA1_BLOCKSIZE, chr(0x00));
  64. $ipad = str_repeat(chr(0x36), Auth_OpenID_SHA1_BLOCKSIZE);
  65. $opad = str_repeat(chr(0x5c), Auth_OpenID_SHA1_BLOCKSIZE);
  66. $hash1 = Auth_OpenID_SHA1(($key ^ $ipad) . $text, true);
  67. $hmac = Auth_OpenID_SHA1(($key ^ $opad) . $hash1, true);
  68. return $hmac;
  69. }
  70. if (function_exists('hash') &&
  71. function_exists('hash_algos') &&
  72. (in_array('sha256', hash_algos()))) {
  73. function Auth_OpenID_SHA256($text)
  74. {
  75. // PHP 5 case: 'hash' available and 'sha256' algo supported.
  76. return hash('sha256', $text, true);
  77. }
  78. define('Auth_OpenID_SHA256_SUPPORTED', true);
  79. } else {
  80. define('Auth_OpenID_SHA256_SUPPORTED', false);
  81. }
  82. if (function_exists('hash_hmac') &&
  83. function_exists('hash_algos') &&
  84. (in_array('sha256', hash_algos()))) {
  85. function Auth_OpenID_HMACSHA256($key, $text)
  86. {
  87. // Return raw MAC (not hex string).
  88. return hash_hmac('sha256', $text, $key, true);
  89. }
  90. define('Auth_OpenID_HMACSHA256_SUPPORTED', true);
  91. } else {
  92. define('Auth_OpenID_HMACSHA256_SUPPORTED', false);
  93. }