Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Class to calculate RFC 2104 compliant hashes
6
 *
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * that is available through the world-wide-web at the following URI:
12
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * the PHP License and are unable to obtain it through the web, please
14
 * send a note to license@php.net so we can mail you a copy immediately.
15
 *
16
 * @category   Encryption
17
 * @package    Crypt_HMAC
18
 * @author     Derick Rethans <derick@php.net>
19
 * @author     Matthew Fonda <mfonda@dotgeek.org>
20
 * @copyright  1997-2005 The PHP Group
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
22
 * @version    CVS: $Id: HMAC.php 965 2012-02-28 07:37:41Z tiefland $
23
 * @link       http://pear.php.net/package/Crypt_HMAC
24
 */
25
 
26
 
27
 
28
 
29
/**
30
* Calculates RFC 2104 compliant HMACs
31
*
32
* @access     public
33
* @category   Encryption
34
* @package    Crypt_HMAC
35
* @author     Derick Rethans <derick@php.net>
36
* @author     Matthew Fonda <mfonda@dotgeek.org>
37
* @copyright  1997-2005 The PHP Group
38
* @license    http://www.php.net/license/3_0.txt  PHP License 3.0
39
* @link       http://pear.php.net/package/Crypt_HMAC
40
*/
41
class Crypt_HMAC
42
{
43
 
44
    /**
45
    * Hash function to use
46
    * @var string
47
    * @access private
48
    */
49
    var $_func;
50
 
51
    /**
52
    * Inner padded key
53
    * @var string
54
    * @access private
55
    */
56
    var $_ipad;
57
 
58
    /**
59
    * Outer padded key
60
    * @var string
61
    * @access private
62
    */
63
    var $_opad;
64
 
65
    /**
66
    * Pack format
67
    * @var string
68
    * @access private
69
    */
70
    var $_pack;
71
 
72
 
73
    /**
74
    * Constructor
75
    * Pass method as first parameter
76
    *
77
    * @param string $key  Key to use for hash
78
    * @param string $func  Hash function used for the calculation
79
    * @return void
80
    * @access public
81
    */
82
    function Crypt_HMAC($key, $func = 'md5')
83
    {
84
        $this->setFunction($func);
85
        $this->setKey($key);
86
    }
87
 
88
 
89
    /**
90
    * Sets hash function
91
    *
92
    * @param string $func  Hash function to use
93
    * @return void
94
    * @access public
95
    */
96
    function setFunction($func)
97
    {
98
        if (!$this->_pack = $this->_getPackFormat($func)) {
99
            die('Unsupported hash function');
100
        }
101
        $this->_func = $func;
102
    }
103
 
104
 
105
    /**
106
    * Sets key to use with hash
107
    *
108
    * @param string $key
109
    * @return void
110
    * @access public
111
    */
112
    function setKey($key)
113
    {
114
        /*
115
        * Pad the key as the RFC wishes
116
        */
117
        $func = $this->_func;
118
 
119
        if (strlen($key) > 64) {
120
           $key =  pack($this->_pack, $func($key));
121
        }
122
        if (strlen($key) < 64) {
123
            $key = str_pad($key, 64, chr(0));
124
        }
125
 
126
 
127
        /* Calculate the padded keys and save them */
128
        $this->_ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
129
        $this->_opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
130
    }
131
 
132
 
133
    /**
134
    * Gets pack formats for specifed hash function
135
    *
136
    * @param string $func
137
    * @return mixed  false if hash function doesnt exist, pack format on success
138
    * @access private
139
    */
140
    function _getPackFormat($func)
141
    {
142
        $packs = array('md5' => 'H32', 'sha1' => 'H40');
143
        return isset($packs[$func]) ? $packs[$func] : false;
144
    }
145
 
146
 
147
    /**
148
    * Hashing function
149
    *
150
    * @param  string $data  string that will encrypted
151
    * @return string
152
    * @access public
153
    */
154
    function hash($data)
155
    {
156
        $func = $this->_func;
157
        return $func($this->_opad . pack($this->_pack, $func($this->_ipad . $data)));
158
    }
159
 
160
}
161
 
162
 
163
?>