|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file provides compatibility functions such as the sha1() function, |
|
5
|
|
|
* missing extensions, etc |
|
6
|
|
|
* It is only included for when the respective extension or function cannot be found. |
|
7
|
|
|
* |
|
8
|
|
|
* @name ElkArte Forum |
|
9
|
|
|
* @copyright ElkArte Forum contributors |
|
10
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
|
11
|
|
|
* |
|
12
|
|
|
* This file contains code covered by: |
|
13
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
|
14
|
|
|
* license: BSD, See included LICENSE.TXT for terms and conditions. |
|
15
|
|
|
* |
|
16
|
|
|
* @version 1.1 |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Define the old SMF sha1 function. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $str the string |
|
24
|
|
|
*/ |
|
25
|
|
|
function sha1_smf($str) |
|
26
|
|
|
{ |
|
27
|
|
|
// If we have mhash loaded in, use it instead! |
|
28
|
|
|
if (function_exists('mhash') && defined('MHASH_SHA1')) |
|
29
|
|
|
return bin2hex(mhash(MHASH_SHA1, $str)); |
|
30
|
|
|
|
|
31
|
|
|
$nblk = (strlen($str) + 8 >> 6) + 1; |
|
32
|
|
|
$blks = array_pad(array(), $nblk * 16, 0); |
|
33
|
|
|
|
|
34
|
|
|
$str_len = strlen($str); |
|
35
|
|
|
for ($i = 0; $i < $str_len; $i++) |
|
36
|
|
|
$blks[$i >> 2] |= ord($str[$i]) << (24 - ($i % 4) * 8); |
|
37
|
|
|
|
|
38
|
|
|
$blks[$i >> 2] |= 0x80 << (24 - ($i % 4) * 8); |
|
39
|
|
|
|
|
40
|
|
|
return sha1_core($blks, $str_len * 8); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This is the core SHA-1 calculation routine, used by sha1(). |
|
45
|
|
|
* |
|
46
|
|
|
* @param int[] $x |
|
47
|
|
|
* @param int $len |
|
48
|
|
|
*/ |
|
49
|
|
|
function sha1_core($x, $len) |
|
50
|
|
|
{ |
|
51
|
|
|
@$x[$len >> 5] |= 0x80 << (24 - $len % 32); |
|
52
|
|
|
$x[(($len + 64 >> 9) << 4) + 15] = $len; |
|
53
|
|
|
|
|
54
|
|
|
$w = array(); |
|
55
|
|
|
$a = 1732584193; |
|
56
|
|
|
$b = -271733879; |
|
57
|
|
|
$c = -1732584194; |
|
58
|
|
|
$d = 271733878; |
|
59
|
|
|
$e = -1009589776; |
|
60
|
|
|
|
|
61
|
|
|
for ($i = 0, $n = count($x); $i < $n; $i += 16) |
|
62
|
|
|
{ |
|
63
|
|
|
$olda = $a; |
|
64
|
|
|
$oldb = $b; |
|
65
|
|
|
$oldc = $c; |
|
66
|
|
|
$oldd = $d; |
|
67
|
|
|
$olde = $e; |
|
68
|
|
|
|
|
69
|
|
|
for ($j = 0; $j < 80; $j++) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($j < 16) |
|
72
|
|
|
$w[$j] = isset($x[$i + $j]) ? $x[$i + $j] : 0; |
|
73
|
|
|
else |
|
74
|
|
|
$w[$j] = sha1_rol($w[$j - 3] ^ $w[$j - 8] ^ $w[$j - 14] ^ $w[$j - 16], 1); |
|
75
|
|
|
|
|
76
|
|
|
$t = sha1_rol($a, 5) + sha1_ft($j, $b, $c, $d) + $e + $w[$j] + sha1_kt($j); |
|
77
|
|
|
$e = $d; |
|
78
|
|
|
$d = $c; |
|
79
|
|
|
$c = sha1_rol($b, 30); |
|
80
|
|
|
$b = $a; |
|
81
|
|
|
$a = $t; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$a += $olda; |
|
85
|
|
|
$b += $oldb; |
|
86
|
|
|
$c += $oldc; |
|
87
|
|
|
$d += $oldd; |
|
88
|
|
|
$e += $olde; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return sprintf('%08x%08x%08x%08x%08x', $a, $b, $c, $d, $e); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Helper function for the core SHA-1 calculation |
|
96
|
|
|
* |
|
97
|
|
|
* @param int $t |
|
98
|
|
|
* @param int $b |
|
99
|
|
|
* @param int $c |
|
100
|
|
|
* @param int $d |
|
101
|
|
|
*/ |
|
102
|
|
|
function sha1_ft($t, $b, $c, $d) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($t < 20) |
|
105
|
|
|
return ($b & $c) | ((~$b) & $d); |
|
106
|
|
|
if ($t < 40) |
|
107
|
|
|
return $b ^ $c ^ $d; |
|
108
|
|
|
if ($t < 60) |
|
109
|
|
|
return ($b & $c) | ($b & $d) | ($c & $d); |
|
110
|
|
|
|
|
111
|
|
|
return $b ^ $c ^ $d; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Helper function for the core SHA-1 calculation |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $t |
|
118
|
|
|
*/ |
|
119
|
|
|
function sha1_kt($t) |
|
120
|
|
|
{ |
|
121
|
|
|
return $t < 20 ? 1518500249 : ($t < 40 ? 1859775393 : ($t < 60 ? -1894007588 : -899497514)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Helper function for the core SHA-1 calculation |
|
126
|
|
|
* |
|
127
|
|
|
* @param int $num |
|
128
|
|
|
* @param int $cnt |
|
129
|
|
|
*/ |
|
130
|
|
|
function sha1_rol($num, $cnt) |
|
131
|
|
|
{ |
|
132
|
|
|
// Unfortunately, PHP uses unsigned 32-bit longs only. So we have to kludge it a bit. |
|
133
|
|
|
if ($num & 0x80000000) |
|
134
|
|
|
$a = ($num >> 1 & 0x7fffffff) >> (31 - $cnt); |
|
135
|
|
|
else |
|
136
|
|
|
$a = $num >> (32 - $cnt); |
|
137
|
|
|
|
|
138
|
|
|
return ($num << $cnt) | $a; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Compatibility function. |
|
143
|
|
|
*/ |
|
144
|
|
|
if (!function_exists('crc32_compat')) |
|
145
|
|
|
{ |
|
146
|
|
|
/** |
|
147
|
|
|
* crc32 doesn't work as expected on 64-bit functions - make our own. |
|
148
|
|
|
* http://www.php.net/crc32#79567 |
|
149
|
|
|
* |
|
150
|
|
|
* @param int $number |
|
151
|
|
|
*/ |
|
152
|
|
|
function crc32_compat($number) |
|
153
|
|
|
{ |
|
154
|
|
|
$crc = crc32($number); |
|
155
|
|
|
|
|
156
|
|
|
if ($crc & 0x80000000) |
|
157
|
|
|
{ |
|
158
|
|
|
$crc ^= 0xffffffff; |
|
159
|
|
|
$crc += 1; |
|
160
|
|
|
$crc = -$crc; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $crc; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|