-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhtml5hash.js
344 lines (271 loc) · 11.6 KB
/
html5hash.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
$().ready(function () {
/*
* Helpers
*/
getUnique = function () {
var uniquecnt = 0;
function getUnique() {
return (uniquecnt++);
}
return getUnique;
}();
function decimalToHexString(number) {
if (number < 0) {
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16);
}
function digits(number, dig) {
var shift = Math.pow(10, dig);
return Math.floor(number * shift) / shift;
}
function escapeHtml(text) {
return $('<div/>').text(text).html();
}
function swapendian32(val) {
return (((val & 0xFF) << 24)
| ((val & 0xFF00) << 8)
| ((val >> 8) & 0xFF00)
| ((val >> 24) & 0xFF)) >>> 0;
}
/*
* Horribly inefficient but unless I find another library or
* (re)write stuff this is the way that just makes it work (TM).
*
* Note: Since CryptoJS 3.1 this functionality is in the framework.
* However it's currently slower than this juding from my tests.
* See CryptoJS components/lib-typedarrays.js
*/
function arrayBufferToWordArray(arrayBuffer) {
var fullWords = Math.floor(arrayBuffer.byteLength / 4);
var bytesLeft = arrayBuffer.byteLength % 4;
var u32 = new Uint32Array(arrayBuffer, 0, fullWords);
var u8 = new Uint8Array(arrayBuffer);
var cp = [];
for (var i = 0; i < fullWords; ++i) {
cp.push(swapendian32(u32[i]));
}
if (bytesLeft) {
var pad = 0;
for (var i = bytesLeft; i > 0; --i) {
pad = pad << 8;
pad += u8[u8.byteLength - i];
}
for (var i = 0; i < 4 - bytesLeft; ++i) {
pad = pad << 8;
}
cp.push(pad);
}
return CryptoJS.lib.WordArray.create(cp, arrayBuffer.byteLength);
};
function bytes2si(bytes, outputdigits) {
if (bytes < 1024) { // Bytes
return digits(bytes, outputdigits) + " b";
}
else if (bytes < 1048576) { // KiB
return digits(bytes / 1024, outputdigits) + " KiB";
}
return digits(bytes / 1048576, outputdigits) + " MiB";
}
function bytes2si2(bytes1, bytes2, outputdigits) {
var big = Math.max(bytes1, bytes2);
if (big < 1024) { // Bytes
return bytes1 + "/" + bytes2 + " b";
}
else if (big < 1048576) { // KiB
return digits(bytes1 / 1024, outputdigits) + "/" +
digits(bytes2 / 1024, outputdigits) + " KiB";
}
return digits(bytes1 / 1048576, outputdigits) + "/" +
digits(bytes2 / 1048576, outputdigits) + " MiB";
}
function progressiveRead(file, work, done) {
var chunkSize = 20480; // 20KiB at a time
var pos = 0;
var reader = new FileReader();
function progressiveReadNext() {
var end = Math.min(pos + chunkSize, file.size);
reader.onload = function (e) {
pos = end;
work(e.target.result, pos, file);
if (pos < file.size) {
setTimeout(progressiveReadNext, 0);
}
else {
// Done
done(file);
}
}
if (file.slice) {
var blob = file.slice(pos, end);
}
else if (file.webkitSlice) {
var blob = file.webkitSlice(pos, end);
}
reader.readAsArrayBuffer(blob);
}
setTimeout(progressiveReadNext, 0);
};
// List all CryptoJS based supported algorithms so we can handle them
// in one codepath. We could also use this to dynamically add these
// options to the UI but this might prevent search engines from properly
// picking them up.
var algorithms = [
{ name: "MD5", type: CryptoJS.algo.MD5 },
{ name: "SHA1", type: CryptoJS.algo.SHA1 },
{ name: "SHA256", type: CryptoJS.algo.SHA256 },
{ name: "SHA512", type: CryptoJS.algo.SHA512 },
{ name: "SHA3-224", type: CryptoJS.algo.SHA3, param: { outputLength: 224 } },
{ name: "SHA3-256", type: CryptoJS.algo.SHA3, param: { outputLength: 256 } },
{ name: "SHA3-384", type: CryptoJS.algo.SHA3, param: { outputLength: 384 } },
{ name: "SHA3-512", type: CryptoJS.algo.SHA3, param: { outputLength: 512 } },
{ name: "RIPEMD-160", type: CryptoJS.algo.RIPEMD160 }
];
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
if (evt.target.files) {
var files = evt.target.files;
}
else {
var files = evt.dataTransfer.files; // FileList object.
}
for (var i = 0, f; f = files[i]; i++) {
(function () {
var start = (new Date).getTime();
var lastprogress = 0;
var enabledAlgorithms = [];
for (var j = 0; j < algorithms.length; j++) {
var current = algorithms[j];
if ($('[name="' + current.name + '-switch"]').prop("checked")) {
// Param will not exist for some but that's ok
var algoInst = { name: current.name, instance: current.type.create(current.param) };
enabledAlgorithms.push(algoInst);
}
}
// Special case CRC32 as it's not part of CryptoJS and takes another input format.
var doCRC32 = $('[name="crc32switch"]').prop("checked");
if (doCRC32) var crc32intermediate = 0;
var uid = "filehash" + getUnique();
$("#list").append('<li id="' + uid + '" class="entrystyle">'
+ '<b>' + escapeHtml(f.name) + ' <span class="progresstext"></span></b>'
+ '<div class="progress"></div>'
+ '</li>');
progressiveRead(f,
function (data, pos, file) {
// Work
if (enabledAlgorithms.length > 0) {
// Easiest way to get this up and running ;-) Obvious optimization potential there.
var wordArray = arrayBufferToWordArray(data);
}
for (var j = 0; j < enabledAlgorithms.length ; j++) {
enabledAlgorithms[j].instance.update(wordArray);
}
if (doCRC32) crc32intermediate = crc32(new Uint8Array(data), crc32intermediate);
// Update progress display
var progress = Math.floor((pos / file.size) * 100);
if (progress > lastprogress) {
var took = ((new Date).getTime() - start) / 1000;
if (took > 0.1) // Only show progressbar after 100ms so it won't show for very small files
$("#" + uid + " .progress").progressbar({ value: progress });
$("#" + uid + " .progresstext").html('('
+ bytes2si2(pos, file.size, 2) + ' @ ' + bytes2si(pos / took, 2) + '/s )');
lastprogress = progress;
}
},
function (file) {
// Done
var took = ((new Date).getTime() - start) / 1000;
var results = '<div class="resultdiv"><table>';
if (doCRC32) results += '<tr><td>CRC-32:</td><td>' + decimalToHexString(crc32intermediate) + '</td></tr>';
for (var j = 0; j < enabledAlgorithms.length ; j++) {
results += '<tr><td class="algoname">' + enabledAlgorithms[j].name + ':</td><td class="algoresult">' + enabledAlgorithms[j].instance.finalize() + '</td></tr>';
}
results += '</table></div>';
results += '<span class="resulttaken">Time taken: ' + digits(took, 2) + 's @ ' + bytes2si(file.size / took, 2) + '/s</span><br />';
$("#" + uid).append(results);
$("#" + uid + " .progress")
.hide('slow');
$("#" + uid)
.css('background-color', '#F0FFF0');
});
})();
};
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function triggerFileSelection(node) {
$("#hiddenFilesSelector").click();
}
function compatible() {
try {
// Check for FileApi
if (typeof FileReader == "undefined") return false;
// Check for Blob and slice api
if (typeof Blob == "undefined") return false;
var blob = new Blob();
if (!blob.slice && !blob.webkitSlice) return false;
// Check for Drag-and-drop
if (!('draggable' in document.createElement('span'))) return false;
} catch (e) {
return false;
}
return true;
}
(function () {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
if (!compatible()) {
$("#nojavascript").hide();
// Fade in incompatibility note
$("#overlay")
.css('opacity', 0)
.animate({ opacity: 0.8 }, 2000)
$("#overlaytextbox")
.css('opacity', 0)
.animate({ opacity: 1.0 }, 2000)
$("#missingfeatures").html(
"HTML5HASH does not work with this browser. Consider using one of these: \
<div id=\"browserads\"> \
<div class=\"browserad\"> \
<a href=\"http://affiliates.mozilla.org/link/banner/21269\"> \
<img src=\"http://affiliates.mozilla.org/media/uploads/banners/f5eeeddc214ed8ef15e48bc80e1f53b0da4f0574.png\" alt=\"Download: Fast, Fun, Awesome\" /> \
</a> \
</div> \
<div class=\"browserad\"> \
<a href=\"http://www.google.com/chrome/\"> \
<img src=\"img/chrome_logo.png\" alt=\"Google Chrome\" /> \
</a> \
</div> \
</div>");
return; // Nevermind initialising the handlers
}
// Hide incompatibility warning
$("#overlay").hide();
$("#overlaytextbox").hide();
// Hide the additional algorithms
$(".additionalalgos").hide();
// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
// Setup browse listener
var fileSelector = document.getElementById('hiddenFilesSelector');
fileSelector.addEventListener('change', handleFileSelect, false);
$("#placeholder").click(triggerFileSelection);
// Setup more/less buttons for hiding showing additional algorithm options
var algoshidden = true;
$("#algosshow").click(function (node) {
$(".additionalalgos").show();
$("#algosshow").hide();
});
$("#algoshide").click(function (node) {
$(".additionalalgos").hide();
$("#algosshow").show();
});
});