Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Gerry | f371fc9 | 2011-09-25 00:28:09 +0800 | [diff] [blame] | 43 | * @link http://codeigniter.com/user_guide/libraries/encryption.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | */ |
Phil Sturgeon | f2b9c91 | 2011-02-16 16:56:37 +0000 | [diff] [blame] | 45 | class CI_SHA1 { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 47 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 58 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 77 | $a = 1732584193; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | $b = -271733879; |
| 79 | $c = -1732584194; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 80 | $d = 271733878; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | $e = -1009589776; |
| 82 | |
Derek Jones | 3355910 | 2009-02-02 18:50:38 +0000 | [diff] [blame] | 83 | for ($i = 0; $i < count($x); $i += 16) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | { |
| 85 | $olda = $a; |
| 86 | $oldb = $b; |
| 87 | $oldc = $c; |
| 88 | $oldd = $d; |
| 89 | $olde = $e; |
| 90 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 91 | for ($j = 0; $j < 80; $j++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * Convert a decimal to hex |
| 125 | * |
| 126 | * @access private |
| 127 | * @param string |
| 128 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 145 | * Return result based on iteration |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | * |
| 147 | * @access private |
| 148 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Add integers, wrapping at 2^32 |
| 194 | * |
| 195 | * @access private |
| 196 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // -------------------------------------------------------------------- |
| 207 | |
| 208 | /** |
| 209 | * Bitwise rotate a 32-bit number |
| 210 | * |
| 211 | * @access private |
| 212 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 */ |
Gerry | f371fc9 | 2011-09-25 00:28:09 +0800 | [diff] [blame] | 251 | /* Location: ./system/libraries/Sha1.php */ |