1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * PHP implementation of SHA-256 hash function |
---|
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.hash |
---|
11 | * @author revulo <revulon@gmail.com> |
---|
12 | * @require PHP 4.0.0 |
---|
13 | */ |
---|
14 | function php_compat_sha256($str, $raw_output = false) |
---|
15 | { |
---|
16 | $h0 = (int)0x6a09e667; |
---|
17 | $h1 = (int)0xbb67ae85; |
---|
18 | $h2 = (int)0x3c6ef372; |
---|
19 | $h3 = (int)0xa54ff53a; |
---|
20 | $h4 = (int)0x510e527f; |
---|
21 | $h5 = (int)0x9b05688c; |
---|
22 | $h6 = (int)0x1f83d9ab; |
---|
23 | $h7 = (int)0x5be0cd19; |
---|
24 | |
---|
25 | $k = array( |
---|
26 | (int)0x428a2f98, (int)0x71374491, (int)0xb5c0fbcf, (int)0xe9b5dba5, |
---|
27 | (int)0x3956c25b, (int)0x59f111f1, (int)0x923f82a4, (int)0xab1c5ed5, |
---|
28 | (int)0xd807aa98, (int)0x12835b01, (int)0x243185be, (int)0x550c7dc3, |
---|
29 | (int)0x72be5d74, (int)0x80deb1fe, (int)0x9bdc06a7, (int)0xc19bf174, |
---|
30 | (int)0xe49b69c1, (int)0xefbe4786, (int)0x0fc19dc6, (int)0x240ca1cc, |
---|
31 | (int)0x2de92c6f, (int)0x4a7484aa, (int)0x5cb0a9dc, (int)0x76f988da, |
---|
32 | (int)0x983e5152, (int)0xa831c66d, (int)0xb00327c8, (int)0xbf597fc7, |
---|
33 | (int)0xc6e00bf3, (int)0xd5a79147, (int)0x06ca6351, (int)0x14292967, |
---|
34 | (int)0x27b70a85, (int)0x2e1b2138, (int)0x4d2c6dfc, (int)0x53380d13, |
---|
35 | (int)0x650a7354, (int)0x766a0abb, (int)0x81c2c92e, (int)0x92722c85, |
---|
36 | (int)0xa2bfe8a1, (int)0xa81a664b, (int)0xc24b8b70, (int)0xc76c51a3, |
---|
37 | (int)0xd192e819, (int)0xd6990624, (int)0xf40e3585, (int)0x106aa070, |
---|
38 | (int)0x19a4c116, (int)0x1e376c08, (int)0x2748774c, (int)0x34b0bcb5, |
---|
39 | (int)0x391c0cb3, (int)0x4ed8aa4a, (int)0x5b9cca4f, (int)0x682e6ff3, |
---|
40 | (int)0x748f82ee, (int)0x78a5636f, (int)0x84c87814, (int)0x8cc70208, |
---|
41 | (int)0x90befffa, (int)0xa4506ceb, (int)0xbef9a3f7, (int)0xc67178f2 |
---|
42 | ); |
---|
43 | |
---|
44 | $len = strlen($str); |
---|
45 | |
---|
46 | $str .= "\x80"; |
---|
47 | $str .= str_repeat("\0", 63 - ($len + 8) % 64); |
---|
48 | $str .= pack('N2', $len >> 29, $len << 3); |
---|
49 | |
---|
50 | for ($i = 0; $i < strlen($str); $i += 64) { |
---|
51 | |
---|
52 | $w = array(); |
---|
53 | for ($j = 0; $j < 16; ++$j) { |
---|
54 | $index = $i + $j * 4; |
---|
55 | $w[$j] = ord($str[$index]) << 24 |
---|
56 | | ord($str[$index + 1]) << 16 |
---|
57 | | ord($str[$index + 2]) << 8 |
---|
58 | | ord($str[$index + 3]); |
---|
59 | } |
---|
60 | for ($j = 16; $j < 64; ++$j) { |
---|
61 | $s0 = php_compat_sha256_rotr_helper($w[$j - 15], 7) |
---|
62 | ^ php_compat_sha256_rotr_helper($w[$j - 15], 18) |
---|
63 | ^ php_compat_sha256_shr_helper ($w[$j - 15], 3); |
---|
64 | |
---|
65 | $s1 = php_compat_sha256_rotr_helper($w[$j - 2], 17) |
---|
66 | ^ php_compat_sha256_rotr_helper($w[$j - 2], 19) |
---|
67 | ^ php_compat_sha256_shr_helper ($w[$j - 2], 10); |
---|
68 | |
---|
69 | $w[$j] = php_compat_sha256_add32_helper( |
---|
70 | php_compat_sha256_add32_helper( |
---|
71 | php_compat_sha256_add32_helper($w[$j - 16], $s0), $w[$j - 7]), $s1); |
---|
72 | } |
---|
73 | |
---|
74 | $a = $h0; |
---|
75 | $b = $h1; |
---|
76 | $c = $h2; |
---|
77 | $d = $h3; |
---|
78 | $e = $h4; |
---|
79 | $f = $h5; |
---|
80 | $g = $h6; |
---|
81 | $h = $h7; |
---|
82 | |
---|
83 | for ($j = 0; $j < 64; ++$j) { |
---|
84 | $s1 = php_compat_sha256_rotr_helper($e, 6) |
---|
85 | ^ php_compat_sha256_rotr_helper($e, 11) |
---|
86 | ^ php_compat_sha256_rotr_helper($e, 25); |
---|
87 | |
---|
88 | $ch = ($e & $f) ^ (~$e & $g); |
---|
89 | |
---|
90 | $s0 = php_compat_sha256_rotr_helper($a, 2) |
---|
91 | ^ php_compat_sha256_rotr_helper($a, 13) |
---|
92 | ^ php_compat_sha256_rotr_helper($a, 22); |
---|
93 | |
---|
94 | $maj = ($a & $b) ^ ($a & $c) ^ ($b & $c); |
---|
95 | |
---|
96 | $t1 = php_compat_sha256_add32_helper( |
---|
97 | php_compat_sha256_add32_helper( |
---|
98 | php_compat_sha256_add32_helper( |
---|
99 | php_compat_sha256_add32_helper($h, $s1), $ch), $k[$j]), $w[$j]); |
---|
100 | |
---|
101 | $t2 = php_compat_sha256_add32_helper($s0, $maj); |
---|
102 | |
---|
103 | $h = $g; |
---|
104 | $g = $f; |
---|
105 | $f = $e; |
---|
106 | $e = php_compat_sha256_add32_helper($d, $t1); |
---|
107 | $d = $c; |
---|
108 | $c = $b; |
---|
109 | $b = $a; |
---|
110 | $a = php_compat_sha256_add32_helper($t1, $t2); |
---|
111 | } |
---|
112 | |
---|
113 | $h0 = php_compat_sha256_add32_helper($h0, $a); |
---|
114 | $h1 = php_compat_sha256_add32_helper($h1, $b); |
---|
115 | $h2 = php_compat_sha256_add32_helper($h2, $c); |
---|
116 | $h3 = php_compat_sha256_add32_helper($h3, $d); |
---|
117 | $h4 = php_compat_sha256_add32_helper($h4, $e); |
---|
118 | $h5 = php_compat_sha256_add32_helper($h5, $f); |
---|
119 | $h6 = php_compat_sha256_add32_helper($h6, $g); |
---|
120 | $h7 = php_compat_sha256_add32_helper($h7, $h); |
---|
121 | } |
---|
122 | |
---|
123 | $h0 &= (int)0xffffffff; |
---|
124 | $h1 &= (int)0xffffffff; |
---|
125 | $h2 &= (int)0xffffffff; |
---|
126 | $h3 &= (int)0xffffffff; |
---|
127 | $h4 &= (int)0xffffffff; |
---|
128 | $h5 &= (int)0xffffffff; |
---|
129 | $h6 &= (int)0xffffffff; |
---|
130 | $h7 &= (int)0xffffffff; |
---|
131 | |
---|
132 | $hash = sprintf('%08x%08x%08x%08x%08x%08x%08x%08x', $h0, $h1, $h2, $h3, $h4, $h5, $h6, $h7); |
---|
133 | |
---|
134 | if ($raw_output) { |
---|
135 | return pack('H*', $hash); |
---|
136 | } else { |
---|
137 | return $hash; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | function php_compat_sha256_add32_helper($x, $y) |
---|
142 | { |
---|
143 | $lsw = ($x & 0xffff) + ($y & 0xffff); |
---|
144 | $msw = ($x >> 16) + ($y >> 16) + ($lsw >> 16); |
---|
145 | return ($msw << 16) | ($lsw & 0xffff); |
---|
146 | } |
---|
147 | |
---|
148 | function php_compat_sha256_shr_helper($x, $n) |
---|
149 | { |
---|
150 | return ($x >> $n) & (0x7fffffff >> ($n - 1)); |
---|
151 | } |
---|
152 | |
---|
153 | function php_compat_sha256_rotr_helper($x, $n) |
---|
154 | { |
---|
155 | return ($x << (32 - $n)) | ($x >> $n) & (0x7fffffff >> ($n - 1)); |
---|
156 | } |
---|