TYPO3 API  SVNRelease
HMAC.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * This is the HMACSHA1 implementation for the OpenID library.
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * LICENSE: See the COPYING file included in this distribution.
00009  *
00010  * @access private
00011  * @package OpenID
00012  * @author JanRain, Inc. <openid@janrain.com>
00013  * @copyright 2005-2008 Janrain, Inc.
00014  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
00015  */
00016 
00017 require_once 'Auth/OpenID.php';
00018 
00019 /**
00020  * SHA1_BLOCKSIZE is this module's SHA1 blocksize used by the fallback
00021  * implementation.
00022  */
00023 define('Auth_OpenID_SHA1_BLOCKSIZE', 64);
00024 
00025 function Auth_OpenID_SHA1($text)
00026 {
00027     if (function_exists('hash') &&
00028         function_exists('hash_algos') &&
00029         (in_array('sha1', hash_algos()))) {
00030         // PHP 5 case (sometimes): 'hash' available and 'sha1' algo
00031         // supported.
00032         return hash('sha1', $text, true);
00033     } else if (function_exists('sha1')) {
00034         // PHP 4 case: 'sha1' available.
00035         $hex = sha1($text);
00036         $raw = '';
00037         for ($i = 0; $i < 40; $i += 2) {
00038             $hexcode = substr($hex, $i, 2);
00039             $charcode = (int)base_convert($hexcode, 16, 10);
00040             $raw .= chr($charcode);
00041         }
00042         return $raw;
00043     } else {
00044         // Explode.
00045         trigger_error('No SHA1 function found', E_USER_ERROR);
00046     }
00047 }
00048 
00049 /**
00050  * Compute an HMAC/SHA1 hash.
00051  *
00052  * @access private
00053  * @param string $key The HMAC key
00054  * @param string $text The message text to hash
00055  * @return string $mac The MAC
00056  */
00057 function Auth_OpenID_HMACSHA1($key, $text)
00058 {
00059     if (Auth_OpenID::bytes($key) > Auth_OpenID_SHA1_BLOCKSIZE) {
00060         $key = Auth_OpenID_SHA1($key, true);
00061     }
00062 
00063     $key = str_pad($key, Auth_OpenID_SHA1_BLOCKSIZE, chr(0x00));
00064     $ipad = str_repeat(chr(0x36), Auth_OpenID_SHA1_BLOCKSIZE);
00065     $opad = str_repeat(chr(0x5c), Auth_OpenID_SHA1_BLOCKSIZE);
00066     $hash1 = Auth_OpenID_SHA1(($key ^ $ipad) . $text, true);
00067     $hmac = Auth_OpenID_SHA1(($key ^ $opad) . $hash1, true);
00068     return $hmac;
00069 }
00070 
00071 if (function_exists('hash') &&
00072     function_exists('hash_algos') &&
00073     (in_array('sha256', hash_algos()))) {
00074     function Auth_OpenID_SHA256($text)
00075     {
00076         // PHP 5 case: 'hash' available and 'sha256' algo supported.
00077         return hash('sha256', $text, true);
00078     }
00079     define('Auth_OpenID_SHA256_SUPPORTED', true);
00080 } else {
00081     define('Auth_OpenID_SHA256_SUPPORTED', false);
00082 }
00083 
00084 if (function_exists('hash_hmac') &&
00085     function_exists('hash_algos') &&
00086     (in_array('sha256', hash_algos()))) {
00087 
00088     function Auth_OpenID_HMACSHA256($key, $text)
00089     {
00090         // Return raw MAC (not hex string).
00091         return hash_hmac('sha256', $text, $key, true);
00092     }
00093 
00094     define('Auth_OpenID_HMACSHA256_SUPPORTED', true);
00095 } else {
00096     define('Auth_OpenID_HMACSHA256_SUPPORTED', false);
00097 }
00098 
00099 ?>