blob: 477b92bcb4d9ab2621cb418f200c93b84d108309 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * SHA1 Encoding Class
32 *
33 * Purpose: Provides 160 bit hashing using The Secure Hash Algorithm
34 * developed at the National Institute of Standards and Technology. The 40
35 * character SHA1 message hash is computationally infeasible to crack.
36 *
37 * This class is a fallback for servers that are not running PHP greater than
38 * 4.3, or do not have the MHASH library.
39 *
40 * This class is based on two scripts:
41 *
42 * Marcus Campbell's PHP implementation (GNU license)
43 * http://www.tecknik.net/sha-1/
44 *
45 * ...which is based on Paul Johnston's JavaScript version
46 * (BSD license). http://pajhome.org.uk/
47 *
48 * I encapsulated the functions and wrote one additional method to fix
49 * a hex conversion bug. - Rick Ellis
50 *
51 * @package CodeIgniter
52 * @subpackage Libraries
53 * @category Encryption
Derek Jonesf4a4bd82011-10-20 12:18:42 -050054 * @author EllisLab Dev Team
Gerryf371fc92011-09-25 00:28:09 +080055 * @link http://codeigniter.com/user_guide/libraries/encryption.html
Derek Allard2067d1a2008-11-13 22:59:24 +000056 */
Phil Sturgeonf2b9c912011-02-16 16:56:37 +000057class CI_SHA1 {
Derek Allard2067d1a2008-11-13 22:59:24 +000058
Greg Akera9263282010-11-10 15:26:43 -060059 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000060 {
61 log_message('debug', "SHA1 Class Initialized");
62 }
63
64 /**
65 * Generate the Hash
66 *
67 * @access public
68 * @param string
69 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020070 */
Derek Allard2067d1a2008-11-13 22:59:24 +000071 function generate($str)
72 {
73 $n = ((strlen($str) + 8) >> 6) + 1;
74
75 for ($i = 0; $i < $n * 16; $i++)
76 {
77 $x[$i] = 0;
78 }
79
80 for ($i = 0; $i < strlen($str); $i++)
81 {
82 $x[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
83 }
84
85 $x[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
86
87 $x[$n * 16 - 1] = strlen($str) * 8;
88
Derek Jones4b9c6292011-07-01 17:40:48 -050089 $a = 1732584193;
Derek Allard2067d1a2008-11-13 22:59:24 +000090 $b = -271733879;
91 $c = -1732584194;
Derek Jones4b9c6292011-07-01 17:40:48 -050092 $d = 271733878;
Derek Allard2067d1a2008-11-13 22:59:24 +000093 $e = -1009589776;
94
Derek Jones33559102009-02-02 18:50:38 +000095 for ($i = 0; $i < count($x); $i += 16)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 $olda = $a;
98 $oldb = $b;
99 $oldc = $c;
100 $oldd = $d;
101 $olde = $e;
102
Pascal Kriete14287f32011-02-14 13:39:34 -0500103 for ($j = 0; $j < 80; $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
105 if ($j < 16)
106 {
107 $w[$j] = $x[$i + $j];
108 }
109 else
110 {
111 $w[$j] = $this->_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
112 }
113
114 $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)));
115
116 $e = $d;
117 $d = $c;
118 $c = $this->_rol($b, 30);
119 $b = $a;
120 $a = $t;
121 }
122
123 $a = $this->_safe_add($a, $olda);
124 $b = $this->_safe_add($b, $oldb);
125 $c = $this->_safe_add($c, $oldc);
126 $d = $this->_safe_add($d, $oldd);
127 $e = $this->_safe_add($e, $olde);
128 }
129
130 return $this->_hex($a).$this->_hex($b).$this->_hex($c).$this->_hex($d).$this->_hex($e);
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
134
135 /**
136 * Convert a decimal to hex
137 *
138 * @access private
139 * @param string
140 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200141 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 function _hex($str)
143 {
144 $str = dechex($str);
145
146 if (strlen($str) == 7)
147 {
148 $str = '0'.$str;
149 }
150
151 return $str;
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // --------------------------------------------------------------------
155
156 /**
Derek Jones4b9c6292011-07-01 17:40:48 -0500157 * Return result based on iteration
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 *
159 * @access private
160 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200161 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 function _ft($t, $b, $c, $d)
163 {
164 if ($t < 20)
165 return ($b & $c) | ((~$b) & $d);
166 if ($t < 40)
167 return $b ^ $c ^ $d;
168 if ($t < 60)
169 return ($b & $c) | ($b & $d) | ($c & $d);
170
171 return $b ^ $c ^ $d;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Determine the additive constant
178 *
179 * @access private
180 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200181 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 function _kt($t)
183 {
184 if ($t < 20)
185 {
186 return 1518500249;
187 }
188 else if ($t < 40)
189 {
190 return 1859775393;
191 }
192 else if ($t < 60)
193 {
194 return -1894007588;
195 }
196 else
197 {
198 return -899497514;
199 }
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // --------------------------------------------------------------------
203
204 /**
205 * Add integers, wrapping at 2^32
206 *
207 * @access private
208 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200209 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 function _safe_add($x, $y)
211 {
212 $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
213 $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
214
215 return ($msw << 16) | ($lsw & 0xFFFF);
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
219
220 /**
221 * Bitwise rotate a 32-bit number
222 *
223 * @access private
224 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200225 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 function _rol($num, $cnt)
227 {
228 return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Pad string with zero
235 *
236 * @access private
237 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200238 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 function _zero_fill($a, $b)
240 {
241 $bin = decbin($a);
242
243 if (strlen($bin) < $b)
244 {
245 $bin = 0;
246 }
247 else
248 {
249 $bin = substr($bin, 0, strlen($bin) - $b);
250 }
251
252 for ($i=0; $i < $b; $i++)
253 {
254 $bin = "0".$bin;
255 }
256
257 return bindec($bin);
258 }
259}
260// END CI_SHA
261
262/* End of file Sha1.php */
Gerryf371fc92011-09-25 00:28:09 +0800263/* Location: ./system/libraries/Sha1.php */