I created a page which allows to transfer files using RTCPeerConnection
and datachannels
.
I need somehow to make sure that received and transferred file are exact the same, to be specific that none of packages has been lost nor bad guys in the middle sent evil packages.
I split files into chunks, send those chunks piece by piece without any identification. When a new webrtc messages comes on other side I push its data to buffer w/o any check. When the transferred size is equal to received size I assemble the file.
This while process looks like:
sender
function sliceFile(offset)
var reader = new window.FileReader();
reader.onload = (function () {
return function (e) {
sendChannel.send(e.target.result);
if (self.file.size > offset + e.target.result.byteLength) {
window.setTimeout(self.sliceFile, 0, offset + CHUNK_SIZE);
}
};
})(self.file);
}
receiver
var receiveBuffer = [];
var receivedSize = 0;
onwebrtcmessage = function(event) {
receiveBuffer.push(event.data);
receivedSize += event.data.byteLength;
if (receivedSize == expectedSize) {
assembleFile(receiveBuffer);
}
}
My question is: What is the best way to verify that received file is correct?
I tried to calc file's md5 checksum on sender and on receiver. Since FileApi
doesn't produce any known by me function that calculates checksum, and javascript's implementation of md5
kills the tab for files over 15MB. This makes me unable to check checksum for huge files.