1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Replace sha1() |
---|
5 | * |
---|
6 | * @category PHP |
---|
7 | * @package PHP_Compat |
---|
8 | * @license LGPL - http://www.gnu.org/licenses/lgpl.html |
---|
9 | * @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net> |
---|
10 | * @link http://php.net/function.sha1 |
---|
11 | * @author revulo <revulon@gmail.com> |
---|
12 | * @since PHP 4.3.0 |
---|
13 | * @require PHP 4.0.0 |
---|
14 | */ |
---|
15 | function php_compat_sha1($str, $raw_output = false) |
---|
16 | { |
---|
17 | $h0 = (int)0x67452301; |
---|
18 | $h1 = (int)0xefcdab89; |
---|
19 | $h2 = (int)0x98badcfe; |
---|
20 | $h3 = (int)0x10325476; |
---|
21 | $h4 = (int)0xc3d2e1f0; |
---|
22 | |
---|
23 | $len = strlen($str); |
---|
24 | |
---|
25 | $str .= "\x80"; |
---|
26 | $str .= str_repeat("\0", 63 - ($len + 8) % 64); |
---|
27 | $str .= pack('N2', $len >> 29, $len << 3); |
---|
28 | |
---|
29 | for ($i = 0; $i < strlen($str); $i += 64) { |
---|
30 | |
---|
31 | $w = array(); |
---|
32 | for ($j = 0; $j < 16; ++$j) { |
---|
33 | $index = $i + $j * 4; |
---|
34 | $w[$j] = ord($str[$index]) << 24 |
---|
35 | | ord($str[$index + 1]) << 16 |
---|
36 | | ord($str[$index + 2]) << 8 |
---|
37 | | ord($str[$index + 3]); |
---|
38 | } |
---|
39 | for ($j = 16; $j < 80; ++$j) { |
---|
40 | $w[$j] = php_compat_sha1_rotl_helper($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1); |
---|
41 | } |
---|
42 | |
---|
43 | $a = $h0; |
---|
44 | $b = $h1; |
---|
45 | $c = $h2; |
---|
46 | $d = $h3; |
---|
47 | $e = $h4; |
---|
48 | |
---|
49 | for ($j = 0; $j < 80; ++$j) { |
---|
50 | if ($j < 20) { |
---|
51 | $f = ($b & $c) | (~$b & $d); |
---|
52 | $k = (int)0x5a827999; |
---|
53 | } else if ($j < 40) { |
---|
54 | $f = $b ^ $c ^ $d; |
---|
55 | $k = (int)0x6ed9eba1; |
---|
56 | } else if ($j < 60) { |
---|
57 | $f = ($b & $c) | ($b & $d) | ($c & $d); |
---|
58 | $k = (int)0x8f1bbcdc; |
---|
59 | } else { |
---|
60 | $f = $b ^ $c ^ $d; |
---|
61 | $k = (int)0xca62c1d6; |
---|
62 | } |
---|
63 | |
---|
64 | $t = php_compat_sha1_add32_helper( |
---|
65 | php_compat_sha1_add32_helper( |
---|
66 | php_compat_sha1_add32_helper( |
---|
67 | php_compat_sha1_add32_helper( |
---|
68 | php_compat_sha1_rotl_helper($a, 5), $f), $e), $k), $w[$j]); |
---|
69 | |
---|
70 | $e = $d; |
---|
71 | $d = $c; |
---|
72 | $c = php_compat_sha1_rotl_helper($b, 30); |
---|
73 | $b = $a; |
---|
74 | $a = $t; |
---|
75 | } |
---|
76 | |
---|
77 | $h0 = php_compat_sha1_add32_helper($h0, $a); |
---|
78 | $h1 = php_compat_sha1_add32_helper($h1, $b); |
---|
79 | $h2 = php_compat_sha1_add32_helper($h2, $c); |
---|
80 | $h3 = php_compat_sha1_add32_helper($h3, $d); |
---|
81 | $h4 = php_compat_sha1_add32_helper($h4, $e); |
---|
82 | } |
---|
83 | |
---|
84 | $h0 &= (int)0xffffffff; |
---|
85 | $h1 &= (int)0xffffffff; |
---|
86 | $h2 &= (int)0xffffffff; |
---|
87 | $h3 &= (int)0xffffffff; |
---|
88 | $h4 &= (int)0xffffffff; |
---|
89 | |
---|
90 | $hash = sprintf('%08x%08x%08x%08x%08x', $h0, $h1, $h2, $h3, $h4); |
---|
91 | |
---|
92 | if ($raw_output) { |
---|
93 | return pack('H*', $hash); |
---|
94 | } else { |
---|
95 | return $hash; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | function php_compat_sha1_add32_helper($x, $y) |
---|
100 | { |
---|
101 | $lsw = ($x & 0xffff) + ($y & 0xffff); |
---|
102 | $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16); |
---|
103 | return ($msw << 16) | ($lsw & 0xffff); |
---|
104 | } |
---|
105 | |
---|
106 | function php_compat_sha1_rotl_helper($x, $n) |
---|
107 | { |
---|
108 | return ($x << $n) | ($x >> (32 - $n)) & (0x7fffffff >> (31 - $n)); |
---|
109 | } |
---|
110 | |
---|
111 | // Define |
---|
112 | if (!function_exists('sha1')) { |
---|
113 | function sha1($str, $raw_output = false) |
---|
114 | { |
---|
115 | return php_compat_sha1($str, $raw_output); |
---|
116 | } |
---|
117 | } |
---|