From 395c38b644d9605485a561f01ccd63e1351efeef Mon Sep 17 00:00:00 2001 From: Abhinav Adduri Date: Tue, 11 Jul 2017 13:30:25 -0700 Subject: [PATCH] added loading, hashing, and encrypting events for uploader; decrypting and hashing events for the downloader --- frontend/src/download.js | 18 ++++++++++++++++++ frontend/src/fileReceiver.js | 10 +++++++++- frontend/src/fileSender.js | 15 +++++++++++++-- frontend/src/upload.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/frontend/src/download.js b/frontend/src/download.js index 52e71f91..00208085 100644 --- a/frontend/src/download.js +++ b/frontend/src/download.js @@ -34,6 +34,24 @@ $(document).ready(function() { } }); + fileReceiver.on('decrypting', isStillDecrypting => { + // The file is being decrypted + if (isStillDecrypting) { + console.log('Decrypting') + } else { + console.log('Done decrypting') + } + }) + + fileReceiver.on('hashing', isStillHashing => { + // The file is being hashed to make sure a malicious user hasn't tampered with it + if (isStillHashing) { + console.log('Checking file integrity') + } else { + console.log('Integrity check done') + } + }) + fileReceiver .download() .catch(() => { diff --git a/frontend/src/fileReceiver.js b/frontend/src/fileReceiver.js index 241d8aac..0404bda8 100644 --- a/frontend/src/fileReceiver.js +++ b/frontend/src/fileReceiver.js @@ -62,6 +62,7 @@ class FileReceiver extends EventEmitter { ['encrypt', 'decrypt'] ) ]).then(([fdata, key]) => { + this.emit('decrypting', true); return Promise.all([ window.crypto.subtle.decrypt( { @@ -71,7 +72,12 @@ class FileReceiver extends EventEmitter { }, key, fdata.data - ), + ).then(decrypted => { + this.emit('decrypting', false); + return new Promise((resolve, reject) => { + resolve(decrypted); + }) + }), new Promise((resolve, reject) => { resolve(fdata.filename); }), @@ -80,7 +86,9 @@ class FileReceiver extends EventEmitter { }) ]); }).then(([decrypted, fname, proposedHash]) => { + this.emit('hashing', true); return window.crypto.subtle.digest('SHA-256', decrypted).then(calculatedHash => { + this.emit('hashing', false); const integrity = new Uint8Array(calculatedHash).toString() === proposedHash.toString(); if (!integrity) { return new Promise((resolve, reject) => { diff --git a/frontend/src/fileSender.js b/frontend/src/fileSender.js index 8be5f4ae..47b63e94 100644 --- a/frontend/src/fileSender.js +++ b/frontend/src/fileSender.js @@ -36,6 +36,8 @@ class FileSender extends EventEmitter { } upload() { + const self = this; + self.emit('loading', true); return Promise.all([ window.crypto.subtle .generateKey( @@ -53,8 +55,12 @@ class FileSender extends EventEmitter { const reader = new FileReader(); reader.readAsArrayBuffer(this.file); reader.onload = function(event) { + self.emit('loading', false); + self.emit('hashing', true); const plaintext = new Uint8Array(this.result); window.crypto.subtle.digest('SHA-256', plaintext).then(hash => { + self.emit('hashing', false); + self.emit('encrypting', true); resolve({plaintext: plaintext, hash: new Uint8Array(hash)}); }) }; @@ -75,7 +81,12 @@ class FileSender extends EventEmitter { }, secretKey, file.plaintext - ), + ).then(encrypted => { + self.emit('encrypting', false); + return new Promise((resolve, reject) => { + resolve(encrypted); + }) + }), window.crypto.subtle.exportKey('jwk', secretKey), new Promise((resolve, reject) => { resolve(file.hash) }) ]); @@ -94,7 +105,7 @@ class FileSender extends EventEmitter { xhr.upload.addEventListener('progress', e => { if (e.lengthComputable) { const percentComplete = Math.floor(e.loaded / e.total * 100); - this.emit('progress', percentComplete); + self.emit('progress', percentComplete); } }); diff --git a/frontend/src/upload.js b/frontend/src/upload.js index ec0bfb8a..f91c09b6 100644 --- a/frontend/src/upload.js +++ b/frontend/src/upload.js @@ -84,6 +84,34 @@ $(document).ready(function() { .style.setProperty('--progress', percentComplete + '%'); $('#progress-text').html(`${percentComplete}%`); }); + + fileSender.on('loading', isStillLoading => { + // The file is loading into Firefox at this stage + if (isStillLoading) { + console.log('Processing') + } else { + console.log('Finished processing') + } + }) + + fileSender.on('hashing', isStillHashing => { + // The file is being hashed + if (isStillHashing) { + console.log('Hashing'); + } else { + console.log('Finished hashing') + } + }) + + fileSender.on('encrypting', isStillEncrypting => { + // The file is being encrypted + if (isStillEncrypting) { + console.log('Encrypting'); + } else { + console.log('Finished encrypting') + } + }) + fileSender .upload() .then(info => { @@ -111,6 +139,7 @@ $(document).ready(function() { }) .catch(err => { Raven.captureException(err); + console.log(err); $('#page-one').hide(); $('#upload-error').show(); });