I didn't find any Hashing Algo for Javascript in the wiki,
but here is an implementation:
Code: Select all
function OpenSubtitlesHash(uploadInput, callback){
function getHash(arrayBuffer) {
var tmp;
var fsize = arrayBuffer.byteLength;
var arr = new Uint16Array(arrayBuffer, 0, 8192 * 4);
var hash = {
3 : 0,
2 : 0,
1 : (fsize >> 16) & 0xFFFF,
0 : fsize & 0xFFFF
};
var offset = fsize >= 65536 ? (fsize - 65536) : 0;
for (var i = 0; i < 8192; i++)
{
hash = AddUINT64(hash, arr.subarray(i*4, i * 4 + 4));
}
arr = new Uint8Array(arrayBuffer, offset, 8192 * 8);
for (i = 0; i < 8192; i++)
{
tmp = arr.subarray(i * 8, i * 8 + 8);
hash = AddUINT64(hash, {
0: (tmp[1] << 8) + tmp[0],
1: (tmp[3] << 8) + tmp[2],
2: (tmp[5] << 8) + tmp[4],
3: (tmp[7] << 8) + tmp[6]
});
}
return UINT64FormatHex(hash);
}
function AddUINT64(a, b) {
var o = {
0 : 0,
1 : 0,
2 : 0,
3 : 0
};
var carry = 0;
for (var i = 0; i < 4; i++)
{
if ((a[i] + b[i] + carry) > 0xffff )
{
o[i] += (a[i] + b[i] + carry) & 0xffff;
carry = 1;
}
else
{
o[i] += (a[i] + b[i] + carry);
carry = 0;
}
}
return o;
}
function UINT64FormatHex(n) {
return parseInt(n[3], 10).toString(16) + parseInt(n[2], 10).toString(16) + parseInt(n[1], 10).toString(16) + parseInt(n[0], 10).toString(16);
}
uploadInput.onchange = function (e) {
e.preventDefault();
var file = uploadInput.files[0],
reader = new FileReader();
reader.onload = function (event) {
var hash;
try {
hash = getHash(event.target.result);
} catch(e) {
hash = 'error';
}
callback( hash );
};
reader.readAsArrayBuffer(file);
return false;
};
}
Is there any other implementation for Javascript which uses the HTML 5 FileReader API but with the .slice() method instead?