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