initial modifications

This commit is contained in:
Bart Butler 2015-03-26 21:27:42 -07:00 committed by Tankred Hase
parent 2e4e9387a0
commit 91d35ff99c
3 changed files with 49 additions and 17 deletions

View File

@ -74,22 +74,43 @@ function getWorker() {
/**
* Encrypts message text/data with keys or passwords
* @param {(Array<module:key~Key>|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>|String)} passwords passwords for the message
* @return {Promise<String>} encrypted ASCII armored message
* @param {Object} params parameter object with optional properties binary {Boolean},
* filename {String}, and packets {Boolean}
* @return {Promise<String> or Promise<Packetlist>} 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);
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);
if(binary) {
return msg.getLiteralData();
}
else {
return msg.getText();
}
}, 'Error decrypting message!');
}

View File

@ -132,10 +132,12 @@ AsyncProxy.prototype.terminate = function() {
/**
* Encrypts message text/data with keys or passwords
* @param {(Array<module:key~Key>|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>|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
});
});
};

View File

@ -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});