Sha1.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 class CI_SHA {
00046
00047 function CI_SHA()
00048 {
00049 log_message('debug', "SHA1 Class Initialized");
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 function generate($str)
00060 {
00061 $n = ((strlen($str) + 8) >> 6) + 1;
00062
00063 for ($i = 0; $i < $n * 16; $i++)
00064 {
00065 $x[$i] = 0;
00066 }
00067
00068 for ($i = 0; $i < strlen($str); $i++)
00069 {
00070 $x[$i >> 2] |= ord(substr($str, $i, 1)) << (24 - ($i % 4) * 8);
00071 }
00072
00073 $x[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8);
00074
00075 $x[$n * 16 - 1] = strlen($str) * 8;
00076
00077 $a = 1732584193;
00078 $b = -271733879;
00079 $c = -1732584194;
00080 $d = 271733878;
00081 $e = -1009589776;
00082
00083 for ($i = 0; $i < sizeof($x); $i += 16)
00084 {
00085 $olda = $a;
00086 $oldb = $b;
00087 $oldc = $c;
00088 $oldd = $d;
00089 $olde = $e;
00090
00091 for($j = 0; $j < 80; $j++)
00092 {
00093 if ($j < 16)
00094 {
00095 $w[$j] = $x[$i + $j];
00096 }
00097 else
00098 {
00099 $w[$j] = $this->_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1);
00100 }
00101
00102 $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)));
00103
00104 $e = $d;
00105 $d = $c;
00106 $c = $this->_rol($b, 30);
00107 $b = $a;
00108 $a = $t;
00109 }
00110
00111 $a = $this->_safe_add($a, $olda);
00112 $b = $this->_safe_add($b, $oldb);
00113 $c = $this->_safe_add($c, $oldc);
00114 $d = $this->_safe_add($d, $oldd);
00115 $e = $this->_safe_add($e, $olde);
00116 }
00117
00118 return $this->_hex($a).$this->_hex($b).$this->_hex($c).$this->_hex($d).$this->_hex($e);
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 function _hex($str)
00131 {
00132 $str = dechex($str);
00133
00134 if (strlen($str) == 7)
00135 {
00136 $str = '0'.$str;
00137 }
00138
00139 return $str;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 function _ft($t, $b, $c, $d)
00151 {
00152 if ($t < 20)
00153 return ($b & $c) | ((~$b) & $d);
00154 if ($t < 40)
00155 return $b ^ $c ^ $d;
00156 if ($t < 60)
00157 return ($b & $c) | ($b & $d) | ($c & $d);
00158
00159 return $b ^ $c ^ $d;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 function _kt($t)
00171 {
00172 if ($t < 20)
00173 {
00174 return 1518500249;
00175 }
00176 else if ($t < 40)
00177 {
00178 return 1859775393;
00179 }
00180 else if ($t < 60)
00181 {
00182 return -1894007588;
00183 }
00184 else
00185 {
00186 return -899497514;
00187 }
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198 function _safe_add($x, $y)
00199 {
00200 $lsw = ($x & 0xFFFF) + ($y & 0xFFFF);
00201 $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16);
00202
00203 return ($msw << 16) | ($lsw & 0xFFFF);
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 function _rol($num, $cnt)
00215 {
00216 return ($num << $cnt) | $this->_zero_fill($num, 32 - $cnt);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 function _zero_fill($a, $b)
00228 {
00229 $bin = decbin($a);
00230
00231 if (strlen($bin) < $b)
00232 {
00233 $bin = 0;
00234 }
00235 else
00236 {
00237 $bin = substr($bin, 0, strlen($bin) - $b);
00238 }
00239
00240 for ($i=0; $i < $b; $i++)
00241 {
00242 $bin = "0".$bin;
00243 }
00244
00245 return bindec($bin);
00246 }
00247 }
00248
00249
00250
00251