Hash.js, minor formatting changes
This commit is contained in:
parent
44ec0d1ecb
commit
def7c88639
1 changed files with 22 additions and 19 deletions
|
@ -1,10 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
/*@
|
||||
Ox.oshash <f> compute oshahs for given File or Blob object (async)
|
||||
Ox.oshash <f> Calculates oshash for a given file or blob object. Async.
|
||||
@*/
|
||||
Ox.oshash = function(file, callback) {
|
||||
//needs to go via string to work for files > 2GB
|
||||
|
||||
// Needs to go via string to work for files > 2GB
|
||||
var hash = fromString(file.size.toString());
|
||||
|
||||
read(0);
|
||||
|
@ -18,7 +19,7 @@ Ox.oshash = function(file, callback) {
|
|||
c &= 0xffff;
|
||||
a = A[0] + B[0] + (b >> 16);
|
||||
b &= 0xffff;
|
||||
//cut of overflow
|
||||
// Cut off overflow
|
||||
a &= 0xffff;
|
||||
return [a, b, c ,d];
|
||||
}
|
||||
|
@ -26,10 +27,10 @@ Ox.oshash = function(file, callback) {
|
|||
function fromData(s, offset) {
|
||||
offset = offset || 0;
|
||||
return [
|
||||
s.charCodeAt(offset+6) + (s.charCodeAt(offset+7) << 8),
|
||||
s.charCodeAt(offset+4) + (s.charCodeAt(offset+5) << 8),
|
||||
s.charCodeAt(offset+2) + (s.charCodeAt(offset+3) << 8),
|
||||
s.charCodeAt(offset+0) + (s.charCodeAt(offset+1) << 8)
|
||||
s.charCodeAt(offset + 6) + (s.charCodeAt(offset + 7) << 8),
|
||||
s.charCodeAt(offset + 4) + (s.charCodeAt(offset + 5) << 8),
|
||||
s.charCodeAt(offset + 2) + (s.charCodeAt(offset + 3) << 8),
|
||||
s.charCodeAt(offset + 0) + (s.charCodeAt(offset + 1) << 8)
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -40,13 +41,13 @@ Ox.oshash = function(file, callback) {
|
|||
num,
|
||||
pos,
|
||||
r = [0, 0, 0, 0];
|
||||
for(pos=0;pos<str.length;pos++) {
|
||||
for (pos = 0; pos < str.length; pos++) {
|
||||
num = parseInt(str.charAt(pos), base);
|
||||
i = 0;
|
||||
do {
|
||||
while (i < blen) {
|
||||
num += r[3-i] * base;
|
||||
r[3-i++] = (num & 0xffff);
|
||||
num += r[3 - i] * base;
|
||||
r[3 - i++] = (num & 0xffff);
|
||||
num >>>= 16;
|
||||
}
|
||||
if (num) {
|
||||
|
@ -58,10 +59,12 @@ Ox.oshash = function(file, callback) {
|
|||
}
|
||||
|
||||
function hex(h) {
|
||||
return (Ox.pad(h[0].toString(16), 4)
|
||||
return (
|
||||
Ox.pad(h[0].toString(16), 4)
|
||||
+ Ox.pad(h[1].toString(16), 4)
|
||||
+ Ox.pad(h[2].toString(16), 4)
|
||||
+ Ox.pad(h[3].toString(16), 4)).toLowerCase();
|
||||
+ Ox.pad(h[3].toString(16), 4)
|
||||
).toLowerCase();
|
||||
}
|
||||
|
||||
function read(offset, last) {
|
||||
|
@ -73,21 +76,21 @@ Ox.oshash = function(file, callback) {
|
|||
var s = data.target.result,
|
||||
s_length = s.length - length,
|
||||
i;
|
||||
for(i=0;i<=s_length; i+=length) {
|
||||
for (i = 0; i <= s_length; i += length) {
|
||||
hash = add(hash, fromData(s, i));
|
||||
}
|
||||
if(file.size < block || last) {
|
||||
if (file.size < block || last) {
|
||||
callback(hex(hash));
|
||||
} else {
|
||||
read(file.size - block, true);
|
||||
}
|
||||
};
|
||||
if(file.mozSlice) {
|
||||
blob = file.mozSlice(offset, offset+block);
|
||||
} else if(file.webkitSlice) {
|
||||
blob = file.webkitSlice(offset, offset+block);
|
||||
if (file.mozSlice) {
|
||||
blob = file.mozSlice(offset, offset + block);
|
||||
} else if (file.webkitSlice) {
|
||||
blob = file.webkitSlice(offset, offset + block);
|
||||
} else {
|
||||
blob = file.slice(offset, offset+block);
|
||||
blob = file.slice(offset, offset + block);
|
||||
}
|
||||
reader.readAsBinaryString(blob);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue