-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
337 lines (300 loc) · 9.04 KB
/
index.html
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
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<script src="private/rusha.js" type="text/javascript" charset="utf-8"></script>
<script src="src/chunksha.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function toUTF8Array(str) {
var utf8 = [];
for (var i=0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10)
| (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >>18),
0x80 | ((charcode>>12) & 0x3f),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
return utf8;
}
function textToArrayBuffer(str) {
var data = toUTF8Array(str);
var arrayBuffer = new ArrayBuffer(data.length);
var view = new Int8Array(arrayBuffer);
for (var i=0;i<data.length;i++){
view[i] = data[i];
}
return arrayBuffer;
}
//var r = new Rusha();
//var s = "любовь"
// var a = toUTF8Array(s)
// console.log(r.digest(a));
// var r = new Rusha();
// var s = "abc0000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000"
// s+= "abc0000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000000000"
// s+= "0000"
// s+= "abc"
// var a = toUTF8Array(s)
// console.log(a)
// console.log(r.digest(a));
</script>
<style>
.container {
margin: 40px;
float: left;
}
.method-switcher{
float: left;
padding:20px;
}
.file-drop-area{
width: 400px;
border: 2px red dashed;
padding: 40px;
float: left;
}
</style>
<body>
<div class="container">
<h1>SHA1</h1>
<div class="method-switcher">
<label>Native</label><input type="radio" name="method" value="native"/>
<br/>
<label>Rusha</label><input type="radio" name="method" value="rusha"/>
<br/>
<label>Chunksha</label><input type="radio" name="method" value="chunksha"/>
<br/>
<label>Chunksha worker</label><input type="radio" name="method" value="chunkshaworker" checked/>
</div>
<div id="dropbox" class="file-drop-area">
<input type="file" id="input" onchange="handleFiles(this.files)">
<br/>
Reading file progress [%]<span id="fileProgress"></span>
<br/>
Computing hash progress [%]<span id="hashProgress"></span>
<br/>
Hash <span id="hash"></span>
<br/>
Execution time [s]: <span id="time"></span>
<br/>
Error <span id="error"></span>
</div>
</div>
<div class="container">
<h1>HMAC SHA1</h1>
<label>Key</label>
<input type="text" id="hmackey" value="abc"/>
<input type="file" id="hmacfile" onchange="handleHmacFiles(this.files)">
</div>
</body>
<script type="text/javascript">
function dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
resetResults();
handleFiles(files);
}
function handleFiles(files) {
var method = getSelectedRadioValue(methods);
switch (method) {
case 'chunksha':
handleFile(files[0]);
break;
case 'chunkshaworker':
handleFileInWorker(files[0]);
break;
case 'rusha':
handleFileRusha(files[0]);
break;
case 'native':
handleFilesNativeAPI(files[0]);
break;
default:
alert('Unsupported method')
}
}
function handleHmacFiles(files) {
var key = document.getElementById('hmackey').value;
var keyArrayBuffer = textToArrayBuffer(key);
handleFileInWorker(files[0], keyArrayBuffer);
}
function getSelectedRadioValue(radios) {
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
return radios[i].value
}
}
return null;
}
var dropbox = document.getElementById("dropbox");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);
var fileProgress = document.getElementById('fileProgress');
var hashProgress = document.getElementById('hashProgress');
var hashEl = document.getElementById('hash');
var timeEl = document.getElementById('time');
var errorEl = document.getElementById('error');
var methods = document.getElementsByName('method');
function resetResults () {
fileProgress.innerHTML = '';
hashProgress.innerHTML = '';
hashEl.innerHTML = '';
timeEl.innerHTML = '';
errorEl.innerHTML = '';
}
//================================
function read(chunkSha, file, n, chunkSize, totalParts, callback) {
var start = n * chunkSize;
var end = start + chunkSize;
if (n === totalParts - 1) {
end = file.size;
}
var reader = new FileReader();
reader.onload = function(e) {
chunkSha.update(e.target.result);
fileProgress.innerHTML = end/file.size*100;
if (n === totalParts - 1) {
callback(chunkSha.getHex());
return;
}
read(chunkSha, file, n+1, chunkSize, totalParts, callback);
};
var blob = file.slice(start, end);
reader.readAsArrayBuffer(blob);
}
function handleFile (file) {
var start = new Date().getTime();
var chunkSize = 1 * 1024 * 1024;
var chunkSha = new ChunkSha();
var totalParts = Math.ceil(file.size/chunkSize);
read(chunkSha, file, 0, chunkSize, totalParts, function (hash) {
var stop = new Date().getTime();
hashEl.innerHTML = hash;
timeEl.innerHTML = (stop-start)/1000;
});
}
//================================
function handleFileInWorker(file, keyArrayBuffer) {
var worker = new Worker('src/chunksha.js');
worker.onmessage = function(e) {
if (e.data.name === 'progress') {
hashProgress.innerHTML = e.data.data;
} else if (e.data.name === 'hash') {
hashEl.innerHTML = e.data.data;
} else if (e.data.name === 'totalTime') {
timeEl.innerHTML = e.data.data;
} else {
console.log(e.data.name);
console.log(e.data.data);
}
};
worker.onerror = function (event) {
console.log(event.message, event);
};
worker.postMessage({file: file, key: keyArrayBuffer});
}
//================================
function arrayBufferToHex(arrayBuffer) {
var i, x, hex_tab = '0123456789abcdef', res = [], binarray = new Uint8Array(arrayBuffer);
for (i = 0; i < binarray.length; i++) {
x = binarray[i];
res[i] = hex_tab.charAt(x >> 4 & 15) + hex_tab.charAt(x >> 0 & 15);
}
return res.join('');
}
function handleFilesNativeAPI(file) {
var start = new Date().getTime();
var reader = new FileReader();
reader.onload = function(event) {
var crypto = window.crypto || window.msCrypto;
if(crypto.subtle) {
crypto.subtle.digest({name: "SHA-1"}, event.target.result)
.then(function(hash){
var hex = arrayBufferToHex(hash);
hashEl.innerHTML = hex;
var stop = new Date().getTime();
timeEl.innerHTML = (stop-start)/1000;
})
.catch(function(err){
console.error(err);
});
} else {
console.log("Crypto not supported")
}
};
reader.onprogress = function (event) {
var p = event.loaded/event.total * 100;
fileProgress.innerHTML = p.toFixed(2) + '%';
}
reader.onerror = function(event) {
console.error("File could not be read! Code " + event.target.error.code);
};
reader.readAsArrayBuffer(file)
}
//================================
function handleFileRusha (file) {
var startTime = new Date().getTime();
var reader = new FileReader();
reader.onload = function(event) {
var worker = new Worker('private/rusha.js');
worker.addEventListener('message', function (e) {
if (e.data.progress) {
hashProgress.innerHTML = e.data.progress * 100;
} else if (e.data.hash) {
var stopTime = new Date().getTime();
timeEl.innerHTML = (stopTime - startTime)/1000;
hash.innerHTML = e.data.hash;
} else {
error.innerHTML = e.data.error
}
}, false);
worker.postMessage({id: file.name, data: event.target.result});
};
reader.onprogress = function(event) {
fileProgress.innerHTML = (event.loaded/event.total * 100)
};
reader.readAsArrayBuffer(file);
}
</script>
</html>