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 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 54 | * @author EllisLab Dev Team |
Gerry | f371fc9 | 2011-09-25 00:28:09 +0800 | [diff] [blame] | 55 | * @link http://codeigniter.com/user_guide/libraries/encryption.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | */ |
Phil Sturgeon | f2b9c91 | 2011-02-16 16:56:37 +0000 | [diff] [blame] | 57 | class CI_SHA1 { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 58 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 59 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | 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 Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 89 | $a = 1732584193; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | $b = -271733879; |
| 91 | $c = -1732584194; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 92 | $d = 271733878; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | $e = -1009589776; |
| 94 | |
Derek Jones | 3355910 | 2009-02-02 18:50:38 +0000 | [diff] [blame] | 95 | for ($i = 0; $i < count($x); $i += 16) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | { |
| 97 | $olda = $a; |
| 98 | $oldb = $b; |
| 99 | $oldc = $c; |
| 100 | $oldd = $d; |
| 101 | $olde = $e; |
| 102 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 103 | for ($j = 0; $j < 80; $j++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | /** |
| 136 | * Convert a decimal to hex |
| 137 | * |
| 138 | * @access private |
| 139 | * @param string |
| 140 | * @return string |
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 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | // -------------------------------------------------------------------- |
| 155 | |
| 156 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 157 | * Return result based on iteration |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | * |
| 159 | * @access private |
| 160 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | // -------------------------------------------------------------------- |
| 203 | |
| 204 | /** |
| 205 | * Add integers, wrapping at 2^32 |
| 206 | * |
| 207 | * @access private |
| 208 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | // -------------------------------------------------------------------- |
| 219 | |
| 220 | /** |
| 221 | * Bitwise rotate a 32-bit number |
| 222 | * |
| 223 | * @access private |
| 224 | * @return integer |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | 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 */ |
Gerry | f371fc9 | 2011-09-25 00:28:09 +0800 | [diff] [blame] | 263 | /* Location: ./system/libraries/Sha1.php */ |