2013-04-01 18:12:15 -04:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// import web worker dependencies
|
2013-05-14 08:05:14 -04:00
|
|
|
importScripts('../../lib/forge/forge.rsa.bundle.js');
|
2013-04-01 18:12:15 -04:00
|
|
|
importScripts('../app-config.js');
|
2013-05-14 08:05:14 -04:00
|
|
|
importScripts('./aes-cbc.js');
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
/**
|
|
|
|
* In the web worker thread context, 'this' and 'self' can be used as a global
|
|
|
|
* variable namespace similar to the 'window' object in the main thread
|
|
|
|
*/
|
2013-04-10 11:14:19 -04:00
|
|
|
self.onmessage = function(e) {
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-15 07:11:08 -04:00
|
|
|
var i = e.data,
|
2013-03-13 11:58:46 -04:00
|
|
|
output = null,
|
2013-05-22 11:03:54 -04:00
|
|
|
aes = new cryptoLib.AesCBC(forge);
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-15 07:11:08 -04:00
|
|
|
if (i.type === 'encrypt' && i.plaintext && i.key && i.iv) {
|
2013-03-13 11:58:46 -04:00
|
|
|
// start encryption
|
2013-05-15 07:11:08 -04:00
|
|
|
output = aes.encrypt(i.plaintext, i.key, i.iv);
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-15 07:11:08 -04:00
|
|
|
} else if (i.type === 'decrypt' && i.ciphertext && i.key && i.iv) {
|
2013-03-13 11:58:46 -04:00
|
|
|
// start decryption
|
2013-05-15 07:11:08 -04:00
|
|
|
output = aes.decrypt(i.ciphertext, i.key, i.iv);
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
} else {
|
|
|
|
throw 'Not all arguments for web worker crypto are defined!';
|
|
|
|
}
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
// pass output back to main thread
|
|
|
|
self.postMessage(output);
|
2013-04-10 11:14:19 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
}());
|