I'm using sjcl to hash files client-side so that I can check if they exist on the server before commencing a full upload.
However, it seems a bit slow. It takes about 15 seconds to hash an 8 MB file. I'm not sure if that's because the library is slow, JavaScript is slow, or the algorithm is inherently slow. It's using sha256
which is probably a bit overkill for what I need. Speed is key -- cryptographic security and collisions aren't particularly important.
Is there a faster way to do this?
$(document).on('drop', function(dropEvent) {
dropEvent.preventDefault();
_.each(dropEvent.originalEvent.dataTransfer.files, function(file) {
var reader = new FileReader();
var pos = 0;
var startTime = +new Date();
var hashObj = new sjcl.hash.sha256();
reader.onprogress = function(progress) {
var chunk = new Uint8Array(reader.result).subarray(pos, progress.loaded);
hashObj.update(chunk);
pos = progress.loaded;
if(progress.lengthComputable) {
console.log((progress.loaded/progress.total*100).toFixed(1)+'%');
}
};
reader.onload = function() {
var endTime = +new Date();
console.log('hashed',file.name,'in',endTime-startTime,'ms');
var chunk = new Uint8Array(reader.result, pos);
if(chunk.length > 0) hashObj.update(chunk);
console.log(sjcl.codec.hex.fromBits(hashObj.finalize()));
};
reader.readAsArrayBuffer(file);
});
});
Edit: Just discovered SparkMD5 as per this answer. Initial tests have it running in under a second for the same 8 MB file, but it's still slower than I'd like.