blob: 13196eb69062fd6b1649e192fb8b53a3c1ccfee0 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 Rick Ellis
43 * @link http://www.codeigniter.com/user_guide/general/encryption.html
44 */
45class 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 < sizeof($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 // END generate()
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Convert a decimal to hex
126 *
127 * @access private
128 * @param string
129 * @return string
130 */
131 function _hex($str)
132 {
133 $str = dechex($str);
134
135 if (strlen($str) == 7)
136 {
137 $str = '0'.$str;
138 }
139
140 return $str;
141 }
142 // END _hex()
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Return result based on iteration
148 *
149 * @access private
150 * @return string
151 */
152 function _ft($t, $b, $c, $d)
153 {
154 if ($t < 20)
155 return ($b & $c) | ((~$b) & $d);
156 if ($t < 40)
157 return $b ^ $c ^ $d;
158 if ($t < 60)
159 return ($b & $c) | ($b & $d) | ($c & $d);
160
161 return $b ^ $c ^ $d;
162 }
163 // END _ft()
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Determine the additive constant
169 *
170 * @access private
171 * @return string
172 */
173 function _kt($t)
174 {
175 if ($t < 20)
176 {
177 return 1518500249;
178 }
179 else if ($t < 40)
180 {
181 return 1859775393;
182 }
183 else if ($t < 60)
184 {
185 return -1894007588;
186 }
187 else
188 {
189 return -899497514;
190 }
191 }
192 // END _kt()
193
194 // --------------------------------------------------------------------
195
196 /**
197 * Add integers, wrapping at 2^32
198 *
199 * @access private
200 * @return string
201 */
202 function _safe_add($x, $y)
203 {
204 $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
205 $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
206
207 return ($msw << 16) | ($lsw & 0xFFFF);
208 }
209 // END _safe_add()
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Bitwise rotate a 32-bit number
215 *
216 * @access private
217 * @return integer
218 */
219 function _rol($num, $cnt)
220 {
221 return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Pad string with zero
228 *
229 * @access private
230 * @return string
231 */
232 function _zero_fill($a, $b)
233 {
234 $bin = decbin($a);
235
236 if (strlen($bin) < $b)
237 {
238 $bin = 0;
239 }
240 else
241 {
242 $bin = substr($bin, 0, strlen($bin) - $b);
243 }
244
245 for ($i=0; $i < $b; $i++)
246 {
247 $bin = "0".$bin;
248 }
249
250 return bindec($bin);
251 }
252}
253// END CI_SHA
254?>