mail/src/js/crypto/nacl-worker.js

32 lines
939 B
JavaScript
Raw Normal View History

2013-04-10 11:09:39 -04:00
(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,
2013-04-11 10:37:39 -04:00
output = null;
2013-04-10 11:09:39 -04:00
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);
};
}());