-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtth.php
235 lines (219 loc) · 5.91 KB
/
tth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @author Alexey Kupershtokh <alexey.kupershtokh@gmail.com>
* @url http://kupershtokh.blogspot.com/2007/12/on-phpclub.html
*/
class TTH {
private static $BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
private static $tiger_hash = null;
private static $tiger_mhash = null;
/**
* Generates TigerTree Head in breadthfirst format
*
* @param string $filename
* @param integer $maxlevel
* @return binary tree
*/
/*
First 24 bytes is binary encoded TTH.
*/
public static function getTigerTree($filename, $maxlevel) {
$fp = fopen($filename, "rb");
$top = 1;
$tree = NULL;
if($fp) {
$i = 1;
$hashes = array();
while(!feof($fp)) {
$buf = fread($fp, 1024);
$step = 1;
if ($buf || ($i == 1)) {
$hashes[$i] = self::tiger("\0".$buf);
if (isset($tree[$step])) {
$tree[$step] = $tree[$step].$hashes[$i];
} else {
$tree[$step] = $hashes[$i];
}
$j = 1;
while($i % ($j * 2) == 0) {
$step = $step + 1;
$hashes[$i] = self::tiger("\1".$hashes[$i - $j].$hashes[$i]);
/*Building tree*/
if ($top < $step) $top = $step;
if ($top - $step <= $maxlevel) {
if (isset($hashes[$i])) {
if (isset($tree[$step])) {
$tree[$step] = $tree[$step].$hashes[$i];
} else {
$tree[$step] = $hashes[$i];
}
}
}elseif (isset($tree[$step])){
unset($tree[$step]);
}
/*end*/
unset($hashes[$i - $j]);
$j = round($j * 2);
}
$i++;
}
}
$k = 1;
while($i > $k) {
$k = round($k * 2);
}
for(; $i <= $k && $i > 2; $i++) {
$j = 1;
$step = 2;
while($i % ($j * 2) == 0) {
if(isset($hashes[$i])) {
$hashes[$i] = self::tiger("\1".$hashes[$i - $j].$hashes[$i]);
} elseif(isset($hashes[$i - $j])) {
$hashes[$i] = $hashes[$i - $j];
}
/*Building tree*/
if ($top < $step) $top = $step;
if ($top - $step <= $maxlevel) {
if (isset($hashes[$i])) {
if (isset($tree[$step])) {
$tree[$step] = $tree[$step].$hashes[$i];
} else {
$tree[$step] = $hashes[$i];
}
}
}elseif (isset($tree[$step])){
unset($tree[$step]);
}
/*end*/
unset($hashes[$i - $j]);
$step = $step + 1;
$j = round($j * 2);
}
}
fclose($fp);
$fulltree = NULL;
for($level = 0; $level < $maxlevel; $level++){
if (!isset($tree[$top - $level])) break;
$fulltree = $fulltree.$tree[$top - $level];
}
return $fulltree;
}
}
/**
* Generates DC-compatible TTH of a file.
*
* @param string $filename
* @return string
*/
/*
public static function getTTH($filename) {
$fp = fopen($filename, "rb");
if($fp) {
$i = 1;
$hashes = array();
while(!feof($fp)) {
$buf = fread($fp, 1024);
if ($buf || ($i == 1)) {
$hashes[$i] = self::tiger("\0".$buf);
$j = 1;
while($i % ($j * 2) == 0) {
$hashes[$i] = self::tiger("\1".$hashes[$i - $j].$hashes[$i]);
unset($hashes[$i - $j]);
$j = round($j * 2);
}
$i++;
}
}
$k = 1;
while($i > $k) {
$k = round($k * 2);
}
for(; $i <= $k; $i++) {
$j = 1;
while($i % ($j * 2) == 0) {
if(isset($hashes[$i])) {
$hashes[$i] = self::tiger("\1".$hashes[$i - $j].$hashes[$i]);
} elseif(isset($hashes[$i - $j])) {
$hashes[$i] = $hashes[$i - $j];
}
unset($hashes[$i - $j]);
$j = round($j * 2);
}
}
fclose($fp);
return self::base32encode($hashes[$i-1]);
}
}
*/
/**
* Generates a DC-compatible tiger hash (not TTH).
* Automatically chooses between hash() and mhash().
*
* @param string $string
* @return string
*/
private static function tiger($string) {
if (is_null(self::$tiger_hash)) {
self::$tiger_hash = function_exists("hash_algos") && in_array("tiger192,3", hash_algos());
}
if (self::$tiger_hash) {
return self::tigerfix(hash("tiger192,3", $string, 1));
}
if (is_null(self::$tiger_mhash)) {
self::$tiger_mhash = function_exists("mhash");
}
if(self::$tiger_mhash) {
return self::tigerfix(mhash(MHASH_TIGER, $string));
}
trigger_error(E_USER_ERROR, "Neither tiger hash function is available.");
}
/**
* Repairs tiger hash for compatibility with DC.
*
* @url http://www.php.net/manual/en/ref.mhash.php#55737
* @param string $binary_hash
* @return string
*/
private static function tigerfix($binary_hash) {
$my_split = str_split($binary_hash,8);
$my_tiger ="";
foreach($my_split as $key => $value) {
$my_split[$key] = strrev($value);
$my_tiger .= $my_split[$key];
}
return $my_tiger;
}
/**
* Just a base32encode function :)
*
* @url http://www.php.net/manual/en/function.sha1-file.php#61741
* @param string $input
* @return string
*/
public static function base32encode($input) {
$output = '';
$position = 0;
$storedData = 0;
$storedBitCount = 0;
$index = 0;
while ($index < strlen($input)) {
$storedData <<= 8;
$storedData += ord($input[$index]);
$storedBitCount += 8;
$index += 1;
//take as much data as possible out of storedData
while ($storedBitCount >= 5) {
$storedBitCount -= 5;
$output .= self::$BASE32_ALPHABET[$storedData >> $storedBitCount];
$storedData &= ((1 << $storedBitCount) - 1);
}
} //while
//deal with leftover data
if ($storedBitCount > 0) {
$storedData <<= (5-$storedBitCount);
$output .= self::$BASE32_ALPHABET[$storedData];
}
return $output;
}
}
?>