2013-05-15 07:11:08 -04:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// import web worker dependencies
|
2013-06-10 18:55:53 -04:00
|
|
|
importScripts('../../lib/require.js');
|
2013-05-15 07:11:08 -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
|
|
|
|
*/
|
|
|
|
self.onmessage = function(e) {
|
2013-06-10 18:55:53 -04:00
|
|
|
// fetch dependencies via require.js
|
|
|
|
require(['../../require-config'], function() {
|
|
|
|
require.config({
|
|
|
|
baseUrl: '../../lib'
|
|
|
|
});
|
2013-05-15 07:11:08 -04:00
|
|
|
|
2013-06-10 18:55:53 -04:00
|
|
|
require(['cryptoLib/crypto-batch'], function(batch) {
|
2013-05-15 07:11:08 -04:00
|
|
|
|
2013-08-30 10:05:33 -04:00
|
|
|
var output;
|
2013-05-15 07:11:08 -04:00
|
|
|
|
2013-08-30 10:05:33 -04:00
|
|
|
try {
|
|
|
|
output = doOperation(batch, e.data);
|
|
|
|
} catch (e) {
|
|
|
|
output = {
|
|
|
|
err: {
|
|
|
|
errMsg: (e.message) ? e.message : e
|
|
|
|
}
|
|
|
|
};
|
2013-06-10 18:55:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass output back to main thread
|
|
|
|
self.postMessage(output);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
2013-05-15 07:11:08 -04:00
|
|
|
};
|
|
|
|
|
2013-08-30 10:05:33 -04:00
|
|
|
function doOperation(batch, i) {
|
|
|
|
var output;
|
|
|
|
|
2013-08-31 10:09:25 -04:00
|
|
|
//
|
|
|
|
// Asymmetric encryption
|
|
|
|
//
|
|
|
|
|
|
|
|
if (i.type === 'asymEncrypt' && i.receiverPubkeys && i.senderPrivkey && i.list) {
|
2013-08-30 10:05:33 -04:00
|
|
|
// start encryption
|
|
|
|
output = batch.encryptListForUser(i.list, i.receiverPubkeys, i.senderPrivkey);
|
|
|
|
|
2013-08-31 10:09:25 -04:00
|
|
|
} else if (i.type === 'asymDecrypt' && i.senderPubkeys && i.receiverPrivkey && i.list) {
|
2013-08-30 10:05:33 -04:00
|
|
|
// start decryption
|
|
|
|
output = batch.decryptListForUser(i.list, i.senderPubkeys, i.receiverPrivkey);
|
2013-08-31 10:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Symmetric encryption
|
|
|
|
//
|
|
|
|
else if (i.type === 'symEncrypt' && i.list) {
|
|
|
|
// start encryption
|
|
|
|
output = batch.authEncryptList(i.list);
|
2013-08-30 10:05:33 -04:00
|
|
|
|
2013-08-31 10:09:25 -04:00
|
|
|
} else if (i.type === 'symDecrypt' && i.list && i.keys) {
|
|
|
|
// start decryption
|
|
|
|
output = batch.authDecryptList(i.list, i.keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Reencryption of asymmetric items to symmetric items
|
|
|
|
//
|
|
|
|
else if (i.type === 'reencrypt' && i.senderPubkeys && i.receiverPrivkey && i.list && i.symKey) {
|
2013-08-30 10:05:33 -04:00
|
|
|
// start validation and re-encryption
|
|
|
|
output = batch.reencryptListKeysForUser(i.list, i.senderPubkeys, i.receiverPrivkey, i.symKey);
|
|
|
|
|
|
|
|
} else if (i.type === 'decryptItems' && i.symKey && i.list) {
|
|
|
|
// start decryption
|
|
|
|
output = batch.decryptKeysAndList(i.list, i.symKey);
|
2013-08-31 10:09:25 -04:00
|
|
|
}
|
2013-08-30 10:05:33 -04:00
|
|
|
|
2013-08-31 10:09:25 -04:00
|
|
|
//
|
|
|
|
// Error
|
|
|
|
//
|
|
|
|
else {
|
2013-08-30 10:05:33 -04:00
|
|
|
output = {
|
|
|
|
err: {
|
|
|
|
errMsg: 'Not all arguments for web worker crypto are defined!'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2013-05-15 07:11:08 -04:00
|
|
|
}());
|