| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,249 @@ |
| 1 |
+<?php |
|
| 2 |
+/* |
|
| 3 |
+ * A PHP implementation of the Secure Hash Algorithm, SHA-1, as defined |
|
| 4 |
+ * in FIPS PUB 180-1 |
|
| 5 |
+ * Adjusted from the Javascript implementation by Joror (daan@parse.nl). |
|
| 6 |
+ * |
|
| 7 |
+ * Javascript Version 2.1 Copyright Paul Johnston 2000 - 2002. |
|
| 8 |
+ * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet |
|
| 9 |
+ * Distributed under the BSD License |
|
| 10 |
+ * See http://pajhome.org.uk/crypt/md5 for details. |
|
| 11 |
+ */ |
|
| 12 |
+ |
|
| 13 |
+class Sha1Lib |
|
| 14 |
+{
|
|
| 15 |
+ /* |
|
| 16 |
+ * Configurable variables. You may need to tweak these to be compatible with |
|
| 17 |
+ * the server-side, but the defaults work in most cases. |
|
| 18 |
+ */ |
|
| 19 |
+ var $hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ |
|
| 20 |
+ var $b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */ |
|
| 21 |
+ var $chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ |
|
| 22 |
+ |
|
| 23 |
+ /* |
|
| 24 |
+ * These are the functions you'll usually want to call |
|
| 25 |
+ * They take string arguments and return either hex or base-64 encoded strings |
|
| 26 |
+ */ |
|
| 27 |
+ function hex_sha1($s){return $this->binb2hex($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
|
|
| 28 |
+ function b64_sha1($s){return $this->binb2b64($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
|
|
| 29 |
+ function str_sha1($s){return $this->binb2str($this->core_sha1($this->str2binb($s),strlen($s) * $this->chrsz));}
|
|
| 30 |
+ function hex_hmac_sha1($key, $data){ return $this->binb2hex($this->core_hmac_sha1($key, $data));}
|
|
| 31 |
+ function b64_hmac_sha1($key, $data){ return $this->binb2b64($this->core_hmac_sha1($key, $data));}
|
|
| 32 |
+ function str_hmac_sha1($key, $data){ return $this->binb2str($this->core_hmac_sha1($key, $data));}
|
|
| 33 |
+ |
|
| 34 |
+ /* |
|
| 35 |
+ * Perform a simple self-test to see if the VM is working |
|
| 36 |
+ */ |
|
| 37 |
+ function sha1_vm_test() |
|
| 38 |
+ {
|
|
| 39 |
+ return $this->hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
|
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ /* |
|
| 43 |
+ * Calculate the SHA-1 of an array of big-endian words, and a bit $length |
|
| 44 |
+ */ |
|
| 45 |
+ function core_sha1($x, $len) |
|
| 46 |
+ {
|
|
| 47 |
+ /* append padding */ |
|
| 48 |
+ $x[$len >> 5] |= 0x80 << (24 - $len % 32); |
|
| 49 |
+ $x[(($len + 64 >> 9) << 4) + 15] = $len; |
|
| 50 |
+ |
|
| 51 |
+ $w = Array(); |
|
| 52 |
+ $a = 1732584193; |
|
| 53 |
+ $b = -271733879; |
|
| 54 |
+ $c = -1732584194; |
|
| 55 |
+ $d = 271733878; |
|
| 56 |
+ $e = -1009589776; |
|
| 57 |
+ |
|
| 58 |
+ for($i = 0; $i < sizeof($x); $i += 16) |
|
| 59 |
+ {
|
|
| 60 |
+ $olda = $a; |
|
| 61 |
+ $oldb = $b; |
|
| 62 |
+ $oldc = $c; |
|
| 63 |
+ $oldd = $d; |
|
| 64 |
+ $olde = $e; |
|
| 65 |
+ |
|
| 66 |
+ for($j = 0; $j < 80; $j++) |
|
| 67 |
+ {
|
|
| 68 |
+ if ($j < 16) |
|
| 69 |
+ $w[$j] = $x[$i + $j]; |
|
| 70 |
+ else |
|
| 71 |
+ $w[$j] = $this->rol($w[$j-3] ^ $w[$j-8] ^ $w[$j-14] ^ $w[$j-16], 1); |
|
| 72 |
+ |
|
| 73 |
+ $t = $this->safe_add( $this->safe_add($this->rol($a, 5), $this->sha1_ft($j, $b, $c, $d)), |
|
| 74 |
+ $this->safe_add($this->safe_add($e, $w[$j]), $this->sha1_kt($j))); |
|
| 75 |
+ $e = $d; |
|
| 76 |
+ $d = $c; |
|
| 77 |
+ $c = $this->rol($b, 30); |
|
| 78 |
+ $b = $a; |
|
| 79 |
+ $a = $t; |
|
| 80 |
+ } |
|
| 81 |
+ |
|
| 82 |
+ $a = $this->safe_add($a, $olda); |
|
| 83 |
+ $b = $this->safe_add($b, $oldb); |
|
| 84 |
+ $c = $this->safe_add($c, $oldc); |
|
| 85 |
+ $d = $this->safe_add($d, $oldd); |
|
| 86 |
+ $e = $this->safe_add($e, $olde); |
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ return Array($a, $b, $c, $d, $e); |
|
| 90 |
+ } |
|
| 91 |
+ |
|
| 92 |
+ /* |
|
| 93 |
+ * Joror: PHP does not have the java(script) >>> operator, so this is a |
|
| 94 |
+ * replacement function. Credits to Terium. |
|
| 95 |
+ */ |
|
| 96 |
+ function zerofill_rightshift($a, $b) |
|
| 97 |
+ {
|
|
| 98 |
+ $z = hexdec(80000000); |
|
| 99 |
+ if ($z & $a) |
|
| 100 |
+ {
|
|
| 101 |
+ $a >>= 1; |
|
| 102 |
+ $a &= (~ $z); |
|
| 103 |
+ $a |= 0x40000000; |
|
| 104 |
+ $a >>= ($b-1); |
|
| 105 |
+ } |
|
| 106 |
+ else |
|
| 107 |
+ {
|
|
| 108 |
+ $a >>= $b; |
|
| 109 |
+ } |
|
| 110 |
+ return $a; |
|
| 111 |
+ } |
|
| 112 |
+ |
|
| 113 |
+ /* |
|
| 114 |
+ * Perform the appropriate triplet combination function for the current |
|
| 115 |
+ * iteration |
|
| 116 |
+ */ |
|
| 117 |
+ function sha1_ft($t, $b, $c, $d) |
|
| 118 |
+ {
|
|
| 119 |
+ if($t < 20) return ($b & $c) | ((~$b) & $d); |
|
| 120 |
+ if($t < 40) return $b ^ $c ^ $d; |
|
| 121 |
+ if($t < 60) return ($b & $c) | ($b & $d) | ($c & $d); |
|
| 122 |
+ return $b ^ $c ^ $d; |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ /* |
|
| 126 |
+ * Determine the appropriate additive constant for the current iteration |
|
| 127 |
+ * Silly php does not understand the inline-if operator well when nested, |
|
| 128 |
+ * so that's why it's ()ed now. |
|
| 129 |
+ */ |
|
| 130 |
+ function sha1_kt($t) |
|
| 131 |
+ {
|
|
| 132 |
+ return ($t < 20) ? 1518500249 : (($t < 40) ? 1859775393 : |
|
| 133 |
+ (($t < 60) ? -1894007588 : -899497514)); |
|
| 134 |
+ } |
|
| 135 |
+ |
|
| 136 |
+ /* |
|
| 137 |
+ * Calculate the HMAC-SHA1 of a key and some data |
|
| 138 |
+ */ |
|
| 139 |
+ function core_hmac_sha1($key, $data) |
|
| 140 |
+ {
|
|
| 141 |
+ $bkey = $this->str2binb($key); |
|
| 142 |
+ if(sizeof($bkey) > 16) $bkey = $this->core_sha1($bkey, sizeof($key) * $this->chrsz); |
|
| 143 |
+ |
|
| 144 |
+ $ipad = Array(); |
|
| 145 |
+ $opad = Array(); |
|
| 146 |
+ |
|
| 147 |
+ for($i = 0; $i < 16; $i++) |
|
| 148 |
+ {
|
|
| 149 |
+ $ipad[$i] = $bkey[$i] ^ 0x36363636; |
|
| 150 |
+ $opad[$i] = $bkey[$i] ^ 0x5C5C5C5C; |
|
| 151 |
+ } |
|
| 152 |
+ |
|
| 153 |
+ $hash = $this->core_sha1(array_merge($ipad,$this->str2binb($data)), 512 + sizeof($data) * $this->chrsz); |
|
| 154 |
+ return $this->core_sha1(array_merge($opad,$hash), 512 + 160); |
|
| 155 |
+ } |
|
| 156 |
+ |
|
| 157 |
+ /* |
|
| 158 |
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally |
|
| 159 |
+ * to work around bugs in some JS interpreters. |
|
| 160 |
+ */ |
|
| 161 |
+ function safe_add($x, $y) |
|
| 162 |
+ {
|
|
| 163 |
+ $lsw = ($x & 0xFFFF) + ($y & 0xFFFF); |
|
| 164 |
+ $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16); |
|
| 165 |
+ return ($msw << 16) | ($lsw & 0xFFFF); |
|
| 166 |
+ } |
|
| 167 |
+ |
|
| 168 |
+ /* |
|
| 169 |
+ * Bitwise rotate a 32-bit number to the left. |
|
| 170 |
+ */ |
|
| 171 |
+ function rol($num, $cnt) |
|
| 172 |
+ {
|
|
| 173 |
+ return ($num << $cnt) | $this->zerofill_rightshift($num, (32 - $cnt)); |
|
| 174 |
+ } |
|
| 175 |
+ |
|
| 176 |
+ /* |
|
| 177 |
+ * Convert an 8-bit or 16-bit string to an array of big-endian words |
|
| 178 |
+ * In 8-bit function, characters >255 have their hi-byte silently ignored. |
|
| 179 |
+ */ |
|
| 180 |
+ function str2binb($str) |
|
| 181 |
+ {
|
|
| 182 |
+ $bin = Array(); |
|
| 183 |
+ $mask = (1 << $this->chrsz) - 1; |
|
| 184 |
+ for($i = 0; $i < strlen($str) * $this->chrsz; $i += $this->chrsz) |
|
| 185 |
+ $bin[$i >> 5] |= (ord($str{$i / $this->chrsz}) & $mask) << (24 - $i%32);
|
|
| 186 |
+ |
|
| 187 |
+ return $bin; |
|
| 188 |
+ } |
|
| 189 |
+ |
|
| 190 |
+ /* |
|
| 191 |
+ * Convert an array of big-endian words to a string |
|
| 192 |
+ */ |
|
| 193 |
+ function binb2str($bin) |
|
| 194 |
+ {
|
|
| 195 |
+ $str = ""; |
|
| 196 |
+ $mask = (1 << $this->chrsz) - 1; |
|
| 197 |
+ for($i = 0; $i < sizeof($bin) * 32; $i += $this->chrsz) |
|
| 198 |
+ $str .= chr($this->zerofill_rightshift($bin[$i>>5], 24 - $i%32) & $mask); |
|
| 199 |
+ return $str; |
|
| 200 |
+ } |
|
| 201 |
+ |
|
| 202 |
+ /* |
|
| 203 |
+ * Convert an array of big-endian words to a hex string. |
|
| 204 |
+ */ |
|
| 205 |
+ function binb2hex($binarray) |
|
| 206 |
+ {
|
|
| 207 |
+ $hex_tab = $this->hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; |
|
| 208 |
+ $str = ""; |
|
| 209 |
+ for($i = 0; $i < sizeof($binarray) * 4; $i++) |
|
| 210 |
+ {
|
|
| 211 |
+ $str .= $hex_tab{($binarray[$i>>2] >> ((3 - $i%4)*8+4)) & 0xF} .
|
|
| 212 |
+ $hex_tab{($binarray[$i>>2] >> ((3 - $i%4)*8 )) & 0xF};
|
|
| 213 |
+ } |
|
| 214 |
+ |
|
| 215 |
+ return $str; |
|
| 216 |
+ } |
|
| 217 |
+ |
|
| 218 |
+ /* |
|
| 219 |
+ * Convert an array of big-endian words to a base-64 string |
|
| 220 |
+ */ |
|
| 221 |
+ function binb2b64($binarray) |
|
| 222 |
+ {
|
|
| 223 |
+ $tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
|
| 224 |
+ $str = ""; |
|
| 225 |
+ for($i = 0; i < sizeof($binarray) * 4; $i += 3) |
|
| 226 |
+ {
|
|
| 227 |
+ $triplet = ((($binarray[$i >> 2] >> 8 * (3 - $i %4)) & 0xFF) << 16) |
|
| 228 |
+ | ((($binarray[$i+1 >> 2] >> 8 * (3 - ($i+1)%4)) & 0xFF) << 8 ) |
|
| 229 |
+ | (($binarray[$i+2 >> 2] >> 8 * (3 - ($i+2)%4)) & 0xFF); |
|
| 230 |
+ for($j = 0; $j < 4; $j++) |
|
| 231 |
+ {
|
|
| 232 |
+ if($i * 8 + $j * 6 > sizeof($binarray) * 32) $str .= $this->b64pad; |
|
| 233 |
+ else $str .= $tab{($triplet >> 6*(3-j)) & 0x3F};
|
|
| 234 |
+ } |
|
| 235 |
+ } |
|
| 236 |
+ return $str; |
|
| 237 |
+ } |
|
| 238 |
+} |
|
| 239 |
+ |
|
| 240 |
+if ( !function_exists('sha1') )
|
|
| 241 |
+{
|
|
| 242 |
+ function sha1( $string, $raw_output = false ) |
|
| 243 |
+ {
|
|
| 244 |
+ $library = new Sha1Lib(); |
|
| 245 |
+ |
|
| 246 |
+ return $raw_output ? $library->str_sha1($string) : $library->hex_sha1($string); |
|
| 247 |
+ } |
|
| 248 |
+} |
|
| 249 |
+?> |
|
| 0 | 250 |
\ No newline at end of file |