1
0
mirror of https://github.com/moparisthebest/mail synced 2024-08-13 16:43:47 -04:00
mail/src/js/crypto/nacl-worker.js
2013-04-11 16:37:39 +02:00

32 lines
939 B
JavaScript

(function() {
'use strict';
// import web worker dependencies
importScripts('../../lib/nacl.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) {
var args = e.data,
output = null;
if (args.type === 'encrypt' && args.plaintext && args.nonce && args.recipientPk && args.senderSk) {
// start encryption
output = nacl.crypto_box(args.plaintext, args.nonce, args.recipientPk, args.senderSk);
} else if (args.type === 'decrypt' && args.ciphertext && args.nonce && args.senderPk && args.recipienSk) {
// start decryption
output = nacl.crypto_box_open(args.ciphertext, args.nonce, args.senderPk, args.recipienSk);
} else {
throw 'Not all arguments for web worker crypto are defined!';
}
// pass output back to main thread
self.postMessage(output);
};
}());