I did some experimenting. It looks like we can get the last chunk read inside the onprogress event by utilizing the incomplete result on the reader object. It only appears to be accessible if we use reader.readAsArrayBuffer (Chrome only?) or reader.readAsBinaryString. The problem with strings is that if that if you want to take a chunk of it, you have to slice it which makes a copy (very slow).
ArrayBuffers have a .subarray method which creates a view into the buffer, without copying any data. This is exactly what we want. However, it doesn't appear to be available on the base class; and it's not clear from the documentation what happens when we construct a derived class (e.g. Uint8Array) using this buffer, but considering the original buffer is accessible via a readonly property, I'm assuming it's not copying.
Both sjcl and CryptoJS conveniently have .update methods which will take in this ArrayBufferView so that you can update your hash on the fly. Thus, I have come up with the following solution (using jQuery, underscore and sjcl):
$(document).on('drop', function(dropEvent) {
dropEvent.preventDefault();
_.each(dropEvent.originalEvent.dataTransfer.files, function(file) {
var reader = new FileReader();
var pos = 0;
var hash = new sjcl.hash.sha256();
reader.onprogress = function(progress) {
var chunk = new Uint8Array(reader.result, pos, progress.loaded - pos);
pos = progress.loaded;
hash.update(chunk);
if(progress.lengthComputable) {
console.log((progress.loaded/progress.total*100).toFixed(1)+'%');
}
};
reader.onload = function() {
var chunk = new Uint8Array(reader.result, pos);
if(chunk.length > 0) hash.update(chunk);
console.log(sjcl.codec.hex.fromBits(hash.finalize()));
};
reader.readAsArrayBuffer(file);
});
});
Note that this solution presently only works in Chrome and is fairly slow. I think sjcl isn't just hashing the file, but it's key-strengthening it, which really isn't what I want. Will investigate more later.