upgrade to codeigniter 1.7.2 for f12
[www-register-wizard.git] / libraries / Sha1.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 4.3.2 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2009, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * SHA1 Encoding Class
20  *
21  * Purpose: Provides 160 bit hashing using The Secure Hash Algorithm
22  * developed at the National Institute of Standards and Technology. The 40
23  * character SHA1 message hash is computationally infeasible to crack.
24  *
25  * This class is a fallback for servers that are not running PHP greater than
26  * 4.3, or do not have the MHASH library.
27  *
28  * This class is based on two scripts:
29  *
30  * Marcus Campbell's PHP implementation (GNU license)
31  * http://www.tecknik.net/sha-1/
32  *
33  * ...which is based on Paul Johnston's JavaScript version
34  * (BSD license). http://pajhome.org.uk/
35  *
36  * I encapsulated the functions and wrote one additional method to fix
37  * a hex conversion bug. - Rick Ellis
38  *
39  * @package             CodeIgniter
40  * @subpackage  Libraries
41  * @category    Encryption
42  * @author              ExpressionEngine Dev Team
43  * @link                http://codeigniter.com/user_guide/general/encryption.html
44  */
45 class CI_SHA {
46
47         function CI_SHA()
48         {
49                 log_message('debug', "SHA1 Class Initialized");
50         }
51
52         /**
53          * Generate the Hash
54          *
55          * @access      public
56          * @param       string
57          * @return      string
58          */     
59         function generate($str)
60         {
61                 $n = ((strlen($str) + 8) >> 6) + 1;
62
63                 for ($i = 0; $i < $n * 16; $i++)
64                 {
65                         $x[$i] = 0;
66                 }
67
68                 for ($i = 0; $i < strlen($str); $i++)
69                 {
70                         $x[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
71                 }
72
73                 $x[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
74
75                 $x[$n * 16 - 1] = strlen($str) * 8;
76
77                 $a =  1732584193;
78                 $b = -271733879;
79                 $c = -1732584194;
80                 $d =  271733878;
81                 $e = -1009589776;
82
83                 for ($i = 0; $i < count($x); $i += 16)
84                 {
85                         $olda = $a;
86                         $oldb = $b;
87                         $oldc = $c;
88                         $oldd = $d;
89                         $olde = $e;
90
91                         for($j = 0; $j < 80; $j++)
92                         {
93                                 if ($j < 16)
94                                 {
95                                         $w[$j] = $x[$i + $j];
96                                 }
97                                 else
98                                 {
99                                         $w[$j] = $this->_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
100                                 }
101
102                                 $t = $this->_safe_add($this->_safe_add($this->_rol($a, 5), $this->_ft($j, $b, $c, $d)), $this->_safe_add($this->_safe_add($e, $w[$j]), $this->_kt($j)));
103
104                                 $e = $d;
105                                 $d = $c;
106                                 $c = $this->_rol($b, 30);
107                                 $b = $a;
108                                 $a = $t;
109                         }
110
111                         $a = $this->_safe_add($a, $olda);
112                         $b = $this->_safe_add($b, $oldb);
113                         $c = $this->_safe_add($c, $oldc);
114                         $d = $this->_safe_add($d, $oldd);
115                         $e = $this->_safe_add($e, $olde);
116                 }
117
118                 return $this->_hex($a).$this->_hex($b).$this->_hex($c).$this->_hex($d).$this->_hex($e);
119         }
120         
121         // --------------------------------------------------------------------
122
123         /**
124          * Convert a decimal to hex
125          *
126          * @access      private
127          * @param       string
128          * @return      string
129          */     
130         function _hex($str)
131         {
132                 $str = dechex($str);
133
134                 if (strlen($str) == 7)
135                 {
136                         $str = '0'.$str;
137                 }
138
139                 return $str;
140         }
141         
142         // --------------------------------------------------------------------
143
144         /**
145          *  Return result based on iteration
146          *
147          * @access      private
148          * @return      string
149          */     
150         function _ft($t, $b, $c, $d)
151         {
152                 if ($t < 20)
153                         return ($b & $c) | ((~$b) & $d);
154                 if ($t < 40)
155                         return $b ^ $c ^ $d;
156                 if ($t < 60)
157                         return ($b & $c) | ($b & $d) | ($c & $d);
158
159                 return $b ^ $c ^ $d;
160         }
161
162         // --------------------------------------------------------------------
163
164         /**
165          * Determine the additive constant
166          *
167          * @access      private
168          * @return      string
169          */     
170         function _kt($t)
171         {
172                 if ($t < 20)
173                 {
174                         return 1518500249;
175                 }
176                 else if ($t < 40)
177                 {
178                         return 1859775393;
179                 }
180                 else if ($t < 60)
181                 {
182                         return -1894007588;
183                 }
184                 else
185                 {
186                         return -899497514;
187                 }
188         }
189         
190         // --------------------------------------------------------------------
191
192         /**
193          * Add integers, wrapping at 2^32
194          *
195          * @access      private
196          * @return      string
197          */     
198         function _safe_add($x, $y)
199         {
200                 $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
201                 $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
202
203                 return ($msw << 16) | ($lsw & 0xFFFF);
204         }
205         
206         // --------------------------------------------------------------------
207
208         /**
209          * Bitwise rotate a 32-bit number
210          *
211          * @access      private
212          * @return      integer
213          */     
214         function _rol($num, $cnt)
215         {
216                 return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
217         }
218
219         // --------------------------------------------------------------------
220
221         /**
222          * Pad string with zero
223          *
224          * @access      private
225          * @return      string
226          */     
227         function _zero_fill($a, $b)
228         {
229                 $bin = decbin($a);
230
231                 if (strlen($bin) < $b)
232                 {
233                         $bin = 0;
234                 }
235                 else
236                 {
237                         $bin = substr($bin, 0, strlen($bin) - $b);
238                 }
239
240                 for ($i=0; $i < $b; $i++)
241                 {
242                         $bin = "0".$bin;
243                 }
244
245                 return bindec($bin);
246         }
247 }
248 // END CI_SHA
249
250 /* End of file Sha1.php */
251 /* Location: ./system/libraries/Sha1.php */