API accepts single public key string instead of array

This commit is contained in:
Msjoinder 2014-06-07 14:11:53 -05:00
parent 896e90c1ee
commit 1fd2c0f6f4
4 changed files with 49 additions and 0 deletions

View File

@ -83,6 +83,10 @@ function encryptMessage(keys, text, callback) {
* @static
*/
function signAndEncryptMessage(publicKeys, privateKey, text, callback) {
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text, callback);
return;
@ -131,6 +135,10 @@ function decryptMessage(privateKey, msg, callback) {
* @static
*/
function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) {
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg, callback);
return;
@ -179,6 +187,10 @@ function signClearMessage(privateKeys, text, callback) {
* @static
*/
function verifyClearSignedMessage(publicKeys, msg, callback) {
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.verifyClearSignedMessage(publicKeys, msg, callback);
return;

View File

@ -120,6 +120,9 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) {
* @param {Function} callback receives encrypted ASCII armored message
*/
AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, text, callback) {
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});
@ -160,6 +163,9 @@ AsyncProxy.prototype.decryptMessage = function(privateKey, message, callback) {
*/
AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, message, callback) {
privateKey = privateKey.toPacketlist();
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});
@ -205,6 +211,9 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) {
* @param {Function} callback receives cleartext with status of verified signatures
*/
AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, callback) {
if (typeof publicKeys === 'string') {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});

View File

@ -47,6 +47,9 @@ onmessage = function (event) {
break;
case 'sign-and-encrypt-message':
try {
if (typeof msg.publicKeys === 'string') {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
msg.privateKey = packetlistCloneToKey(msg.privateKey);
data = window.openpgp.signAndEncryptMessage(msg.publicKeys, msg.privateKey, msg.text);
@ -68,6 +71,9 @@ onmessage = function (event) {
case 'decrypt-and-verify-message':
try {
msg.privateKey = packetlistCloneToKey(msg.privateKey);
if (typeof msg.publicKeys === 'string') {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
msg.message = packetlistCloneToMessage(msg.message.packets);
data = window.openpgp.decryptAndVerifyMessage(msg.privateKey, msg.publicKeys, msg.message);
@ -87,6 +93,9 @@ onmessage = function (event) {
break;
case 'verify-clear-signed-message':
try {
if (typeof msg.publicKeys === 'string') {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
var packetlist = window.openpgp.packet.List.fromStructuredClone(msg.message.packets);
msg.message = new window.openpgp.cleartext.CleartextMessage(msg.message.text, packetlist);

View File

@ -194,6 +194,17 @@ describe('High level API', function() {
});
});
it('RSA: encryptMessage one key async', function (done) {
openpgp.encryptMessage(pubKeyRSA, plaintext, function(err, data) {
expect(err).to.not.exist;
expect(data).to.exist;
expect(data).to.match(/^-----BEGIN PGP MESSAGE/);
var msg = openpgp.message.readArmored(data);
expect(msg).to.be.an.instanceof(openpgp.message.Message);
done();
});
});
it('RSA: encryptMessage sync', function () {
var msg = openpgp.encryptMessage([pubKeyRSA], plaintext);
expect(msg).to.exist;
@ -202,6 +213,14 @@ describe('High level API', function() {
expect(msg).to.be.an.instanceof(openpgp.message.Message);
});
it('RSA: encryptMessage one key sync', function () {
var msg = openpgp.encryptMessage(pubKeyRSA, plaintext);
expect(msg).to.exist;
expect(msg).to.match(/^-----BEGIN PGP MESSAGE/);
msg = openpgp.message.readArmored(msg);
expect(msg).to.be.an.instanceof(openpgp.message.Message);
});
it('ELG: encryptMessage async', function (done) {
openpgp.encryptMessage([pubKeyDE], plaintext, function(err, data) {
expect(err).to.not.exist;