added loading, hashing, and encrypting events for uploader; decrypting and hashing events for the downloader

This commit is contained in:
Abhinav Adduri 2017-07-11 13:30:25 -07:00
parent 6181ea6463
commit 395c38b644
4 changed files with 69 additions and 3 deletions

View File

@ -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 fileReceiver
.download() .download()
.catch(() => { .catch(() => {

View File

@ -62,6 +62,7 @@ class FileReceiver extends EventEmitter {
['encrypt', 'decrypt'] ['encrypt', 'decrypt']
) )
]).then(([fdata, key]) => { ]).then(([fdata, key]) => {
this.emit('decrypting', true);
return Promise.all([ return Promise.all([
window.crypto.subtle.decrypt( window.crypto.subtle.decrypt(
{ {
@ -71,7 +72,12 @@ class FileReceiver extends EventEmitter {
}, },
key, key,
fdata.data fdata.data
), ).then(decrypted => {
this.emit('decrypting', false);
return new Promise((resolve, reject) => {
resolve(decrypted);
})
}),
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
resolve(fdata.filename); resolve(fdata.filename);
}), }),
@ -80,7 +86,9 @@ class FileReceiver extends EventEmitter {
}) })
]); ]);
}).then(([decrypted, fname, proposedHash]) => { }).then(([decrypted, fname, proposedHash]) => {
this.emit('hashing', true);
return window.crypto.subtle.digest('SHA-256', decrypted).then(calculatedHash => { return window.crypto.subtle.digest('SHA-256', decrypted).then(calculatedHash => {
this.emit('hashing', false);
const integrity = new Uint8Array(calculatedHash).toString() === proposedHash.toString(); const integrity = new Uint8Array(calculatedHash).toString() === proposedHash.toString();
if (!integrity) { if (!integrity) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -36,6 +36,8 @@ class FileSender extends EventEmitter {
} }
upload() { upload() {
const self = this;
self.emit('loading', true);
return Promise.all([ return Promise.all([
window.crypto.subtle window.crypto.subtle
.generateKey( .generateKey(
@ -53,8 +55,12 @@ class FileSender extends EventEmitter {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsArrayBuffer(this.file); reader.readAsArrayBuffer(this.file);
reader.onload = function(event) { reader.onload = function(event) {
self.emit('loading', false);
self.emit('hashing', true);
const plaintext = new Uint8Array(this.result); const plaintext = new Uint8Array(this.result);
window.crypto.subtle.digest('SHA-256', plaintext).then(hash => { window.crypto.subtle.digest('SHA-256', plaintext).then(hash => {
self.emit('hashing', false);
self.emit('encrypting', true);
resolve({plaintext: plaintext, hash: new Uint8Array(hash)}); resolve({plaintext: plaintext, hash: new Uint8Array(hash)});
}) })
}; };
@ -75,7 +81,12 @@ class FileSender extends EventEmitter {
}, },
secretKey, secretKey,
file.plaintext file.plaintext
), ).then(encrypted => {
self.emit('encrypting', false);
return new Promise((resolve, reject) => {
resolve(encrypted);
})
}),
window.crypto.subtle.exportKey('jwk', secretKey), window.crypto.subtle.exportKey('jwk', secretKey),
new Promise((resolve, reject) => { resolve(file.hash) }) new Promise((resolve, reject) => { resolve(file.hash) })
]); ]);
@ -94,7 +105,7 @@ class FileSender extends EventEmitter {
xhr.upload.addEventListener('progress', e => { xhr.upload.addEventListener('progress', e => {
if (e.lengthComputable) { if (e.lengthComputable) {
const percentComplete = Math.floor(e.loaded / e.total * 100); const percentComplete = Math.floor(e.loaded / e.total * 100);
this.emit('progress', percentComplete); self.emit('progress', percentComplete);
} }
}); });

View File

@ -84,6 +84,34 @@ $(document).ready(function() {
.style.setProperty('--progress', percentComplete + '%'); .style.setProperty('--progress', percentComplete + '%');
$('#progress-text').html(`${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 fileSender
.upload() .upload()
.then(info => { .then(info => {
@ -111,6 +139,7 @@ $(document).ready(function() {
}) })
.catch(err => { .catch(err => {
Raven.captureException(err); Raven.captureException(err);
console.log(err);
$('#page-one').hide(); $('#page-one').hide();
$('#upload-error').show(); $('#upload-error').show();
}); });