Subversion Repositories Applications.annuaire

Rev

Rev 42 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 aurelien 1
<?php
2
 
3
/**
4
 * This is the HMACSHA1 implementation for the OpenID library.
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * LICENSE: See the COPYING file included in this distribution.
9
 *
10
 * @access private
11
 * @package OpenID
12
 * @author JanRain, Inc. <openid@janrain.com>
13
 * @copyright 2005 Janrain, Inc.
14
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
15
 */
16
 
17
/**
18
 * SHA1_BLOCKSIZE is this module's SHA1 blocksize used by the fallback
19
 * implementation.
20
 */
21
define('Auth_OpenID_SHA1_BLOCKSIZE', 64);
22
 
23
if (!function_exists('sha1')) {
24
    /**
25
     * Return a raw SHA1 hash of the given string
26
     *
27
     * XXX: include the SHA1 code from Dan Libby's OpenID library
28
     */
29
    function Auth_OpenID_SHA1($text)
30
    {
31
        trigger_error('No SHA1 function found', E_USER_ERROR);
32
    }
33
} else {
34
    /**
35
     * @ignore
36
     */
37
    function Auth_OpenID_SHA1($text)
38
        {
39
            $hex = sha1($text);
40
            $raw = '';
41
            for ($i = 0; $i < 40; $i += 2) {
42
                $hexcode = substr($hex, $i, 2);
43
                $charcode = (int)base_convert($hexcode, 16, 10);
44
                $raw .= chr($charcode);
45
            }
46
            return $raw;
47
        }
48
}
49
 
50
/**
51
 * Compute an HMAC/SHA1 hash.
52
 *
53
 * @access private
54
 * @param string $key The HMAC key
55
 * @param string $text The message text to hash
56
 * @return string $mac The MAC
57
 */
58
function Auth_OpenID_HMACSHA1($key, $text)
59
{
60
    if (strlen($key) > Auth_OpenID_SHA1_BLOCKSIZE) {
61
        $key = Auth_OpenID_SHA1($key, true);
62
    }
63
 
64
    $key = str_pad($key, Auth_OpenID_SHA1_BLOCKSIZE, chr(0x00));
65
    $ipad = str_repeat(chr(0x36), Auth_OpenID_SHA1_BLOCKSIZE);
66
    $opad = str_repeat(chr(0x5c), Auth_OpenID_SHA1_BLOCKSIZE);
67
    $hash1 = Auth_OpenID_SHA1(($key ^ $ipad) . $text, true);
68
    $hmac = Auth_OpenID_SHA1(($key ^ $opad) . $hash1, true);
69
    return $hmac;
70
}
71
 
72
?>