Merge pull request #185 from mozilla/events128

added loading, hashing, and encrypting events for uploader; decryptin…
This commit is contained in:
Danny Coates 2017-07-11 13:54:43 -07:00 committed by GitHub
commit dad6132342
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
.download()
.catch(() => {

View File

@ -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) => {

View File

@ -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);
}
});

View File

@ -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();
});