From 91d35ff99c542e6ef54c6e8aa9fa50ec5d05a327 Mon Sep 17 00:00:00 2001 From: Bart Butler Date: Thu, 26 Mar 2015 21:27:42 -0700 Subject: [PATCH] initial modifications --- src/openpgp.js | 46 +++++++++++++++++++++++++++++++-------- src/worker/async_proxy.js | 16 +++++++++----- src/worker/worker.js | 4 ++-- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index e4ce81f9..022b1ef2 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -74,22 +74,43 @@ function getWorker() { /** * Encrypts message text/data with keys or passwords * @param {(Array|module:key~Key)} keys array of keys or single key, used to encrypt the message - * @param {String} text text message as native JavaScript string + * @param {String} data text/data message as native JavaScript string/binary string * @param {(Array|String)} passwords passwords for the message - * @return {Promise} encrypted ASCII armored message + * @param {Object} params parameter object with optional properties binary {Boolean}, + * filename {String}, and packets {Boolean} + * @return {Promise or Promise} encrypted ASCII armored message, or Packetlist if params.packets is true * @static */ -function encryptMessage(keys, text, passwords) { +function encryptMessage(keys, data, passwords, params) { if (asyncProxy) { - return asyncProxy.encryptMessage(keys, text, passwords); + return asyncProxy.encryptMessage(keys, data, passwords, params); + } + + var filename, binary, packets; + if(params) { + filename = params.filename; + binary = params.binary; + packets = params.packets; } return execute(function() { var msg, armored; - msg = message.fromText(text); + if(binary) { + msg = message.fromBinary(data, filename); + } + else { + msg = message.fromText(data, filename); + } msg = msg.encrypt(keys, passwords); - return armor.encode(enums.armor.message, msg.packets.write()); + + if(packets) { + return msg.packets; + } + else { + return armor.encode(enums.armor.message, msg.packets.write()); + } + }, 'Error encrypting message!'); } @@ -125,18 +146,25 @@ function signAndEncryptMessage(publicKeys, privateKey, text) { * Decrypts message * @param {module:key~Key|String} privateKey private key with decrypted secret key data or string password * @param {module:message~Message} msg the message object with the encrypted data + * @param {Boolean} binary if true, return literal data binaryString instead of converting from UTF-8 * @return {Promise<(String|null)>} decrypted message as as native JavaScript string * or null if no literal data found * @static */ -function decryptMessage(privateKey, msg) { +function decryptMessage(privateKey, msg, binary) { if (asyncProxy) { - return asyncProxy.decryptMessage(privateKey, msg); + return asyncProxy.decryptMessage(privateKey, msg, binary); } return execute(function() { msg = msg.decrypt(privateKey); - return msg.getText(); + if(binary) { + return msg.getLiteralData(); + } + else { + return msg.getText(); + } + }, 'Error decrypting message!'); } diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 3fb63081..5749d5f0 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -132,10 +132,12 @@ AsyncProxy.prototype.terminate = function() { /** * Encrypts message text/data with keys or passwords * @param {(Array|module:key~Key)} keys array of keys or single key, used to encrypt the message - * @param {String} text text message as native JavaScript string/binary string + * @param {String} data text/data message as native JavaScript string/binary string * @param {(Array|String)} passwords passwords for the message + * @param {Object} params parameter object with optional properties binary {Boolean}, + * filename {String}, and packets {Boolean} */ -AsyncProxy.prototype.encryptMessage = function(keys, text, passwords) { +AsyncProxy.prototype.encryptMessage = function(keys, data, passwords, params) { var self = this; return self.execute(function() { @@ -150,8 +152,9 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, passwords) { self.worker.postMessage({ event: 'encrypt-message', keys: keys, - text: text, - passwords: passwords + data: data, + passwords: passwords, + params: params }); }); }; @@ -188,7 +191,7 @@ AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, te * @param {module:message~Message} msg the message object with the encrypted data * @param {Boolean} binary if true, return literal data binaryString instead of converting from UTF-8 */ -AsyncProxy.prototype.decryptMessage = function(privateKey, message) { +AsyncProxy.prototype.decryptMessage = function(privateKey, message, binary) { var self = this; return self.execute(function() { @@ -199,7 +202,8 @@ AsyncProxy.prototype.decryptMessage = function(privateKey, message) { self.worker.postMessage({ event: 'decrypt-message', privateKey: privateKey, - message: message + message: message, + binary: binary }); }); }; diff --git a/src/worker/worker.js b/src/worker/worker.js index a7fc8ab4..ca3fa27c 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -68,7 +68,7 @@ self.onmessage = function (event) { if(msg.keys) { msg.keys = msg.keys.map(packetlistCloneToKey); } - window.openpgp.encryptMessage(msg.keys, msg.text, msg.passwords).then(function(data) { + window.openpgp.encryptMessage(msg.keys, msg.data, msg.passwords, msg.params).then(function(data) { response({event: 'method-return', data: data}); }).catch(function(e) { response({event: 'method-return', err: e.message}); @@ -91,7 +91,7 @@ self.onmessage = function (event) { msg.privateKey = packetlistCloneToKey(msg.privateKey); } msg.message = packetlistCloneToMessage(msg.message.packets); - window.openpgp.decryptMessage(msg.privateKey, msg.message).then(function(data) { + window.openpgp.decryptMessage(msg.privateKey, msg.message, msg.binary).then(function(data) { response({event: 'method-return', data: data}); }).catch(function(e) { response({event: 'method-return', err: e.message});