1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-16 22:25:08 -05:00
mail/src/js/crypto/crypto-batch-worker.js

93 lines
2.1 KiB
JavaScript
Raw Normal View History

(function() {
'use strict';
// import web worker dependencies
2013-06-10 18:55:53 -04:00
importScripts('../../lib/require.js');
/**
* 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-06-10 18:55:53 -04:00
require(['cryptoLib/crypto-batch'], function(batch) {
2013-08-30 10:05:33 -04:00
var output;
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-08-30 10:05:33 -04:00
function doOperation(batch, i) {
var output;
//
// 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);
} 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);
}
//
// Symmetric encryption
//
else if (i.type === 'symEncrypt' && i.list) {
// start encryption
output = batch.authEncryptList(i.list);
2013-08-30 10:05:33 -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-30 10:05:33 -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;
}
}());