diff --git a/www/file/file-crypto.js b/www/file/file-crypto.js index de80fd336..cde49759c 100644 --- a/www/file/file-crypto.js +++ b/www/file/file-crypto.js @@ -8,14 +8,9 @@ define([ var cypherChunkLength = 131088; var computeEncryptedSize = function (bytes, meta) { - var metasize = Nacl.util.decodeUTF8(meta).length + 18; + var metasize = Nacl.util.decodeUTF8(JSON.stringify(meta)).length; var chunks = Math.ceil(bytes / plainChunkLength); - console.log({ - metasize: metasize, - chunks: chunks, - bytes: bytes, - }); - return metasize + (chunks * 16) + bytes; + return metasize + 18 + (chunks * 16) + bytes; }; var encodePrefix = function (p) { @@ -142,23 +137,15 @@ define([ var i = 0; - /* - 0: metadata - 1: u8 - 2: done - */ - var state = 0; - var next = function (cb) { + if (state === 2) { return void cb(); } + var start; var end; var part; var box; - // DONE - if (state === 2) { return void cb(); } - if (state === 0) { // metadata... part = new Uint8Array(plaintext); box = Nacl.secretbox(part, nonce, key); @@ -171,10 +158,6 @@ define([ .concat(slice(box))); state++; - // TODO verify that each box is the expected size - - console.log(part.length, prefixed.length); - return void cb(void 0, prefixed); } @@ -187,8 +170,6 @@ define([ increment(nonce); i++; - console.log(part.length, box.length); - // regular data is done if (i * plainChunkLength >= u8.length) { state = 2; } diff --git a/www/file/main.js b/www/file/main.js index 32bcaafe6..2f553cf20 100644 --- a/www/file/main.js +++ b/www/file/main.js @@ -36,6 +36,7 @@ define([ var key = Nacl.randomBytes(32); var next = FileCrypto.encrypt(u8, metadata, key); + var estimate = FileCrypto.computeEncryptedSize(blob.byteLength, metadata); var chunks = []; var sendChunk = function (box, cb) { @@ -47,15 +48,21 @@ define([ }); }; + var actual = 0; var again = function (err, box) { if (err) { throw new Error(err); } if (box) { + actual += box.length; return void sendChunk(box, function (e) { if (e) { return console.error(e); } next(again); }); } + if (actual !== estimate) { + console.error('Estimated size does not match actual size'); + } + // if not box then done Cryptpad.rpc.send('UPLOAD_COMPLETE', '', function (e, res) { if (e) { return void console.error(e); }