From 1fd2c0f6f4001533df9f2acff15606533aa8d532 Mon Sep 17 00:00:00 2001 From: Msjoinder Date: Sat, 7 Jun 2014 14:11:53 -0500 Subject: [PATCH] API accepts single public key string instead of array --- src/openpgp.js | 12 ++++++++++++ src/worker/async_proxy.js | 9 +++++++++ src/worker/worker.js | 9 +++++++++ test/worker/api.js | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/openpgp.js b/src/openpgp.js index e826620e..b6a4b7d9 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -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; diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 6580b0bc..1d9af298 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -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(); }); diff --git a/src/worker/worker.js b/src/worker/worker.js index b93ee4d5..844072da 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -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); diff --git a/test/worker/api.js b/test/worker/api.js index 3c87cfa2..43efaea3 100644 --- a/test/worker/api.js +++ b/test/worker/api.js @@ -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;