1

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.

deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • 1
    By default, Datachannels are reliable (encrypted channel, data retransmission and ordered data). I dont know if it is rly useful to check the integrity of the file. – Antonin M. Jul 18 '16 at 13:43
  • Calculating MD5 sounds like a reasonable approach. Cut the work up into small pieces that can be executed without blocking the tab, or use a Web worker. Should finish well before the transfer. – jib Jul 18 '16 at 14:55

0 Answers0