From eb7f854afe3c13fb827d9d8ff8ffe5ef5fc136ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Mon, 15 Dec 2014 14:30:22 +0100 Subject: [PATCH 1/4] Add useWebCrypto to config and set to true --- src/config/config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config/config.js b/src/config/config.js index 2aa6f182..17e2298b 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -37,6 +37,7 @@ module.exports = { compression: enums.compression.zip, integrity_protect: true, rsa_blinding: true, + useWebCrypto: true, show_version: true, show_comment: true, From c9b4f7532e2e89b2f65e24687894fae3265c278d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Mon, 15 Dec 2014 14:33:13 +0100 Subject: [PATCH 2/4] Add getter method for worker to high level API. Initialization options for AsyncProxy: path and worker. --- src/openpgp.js | 54 +++++++++++++++++++-------------------- src/worker/async_proxy.js | 8 ++++-- test/worker/api.js | 25 +++++++++++++++--- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index f0ca5863..955e9147 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -46,14 +46,29 @@ if (typeof Promise === 'undefined') { require('es6-promise').polyfill(); } -var asyncProxy; // instance of the asyncproxy +var asyncProxy = null; // instance of the asyncproxy /** * Set the path for the web worker script and create an instance of the async proxy - * @param {String} path relative path to the worker scripts + * @param {Object} [options.path=String] relative path to the worker scripts, default: 'openpgp.worker.js' + * [options.worker=module:async_proxy~AsyncProxy] initialized AsyncProxy object to be used as a worker + * @return {Boolean} true if worker created successfully */ -function initWorker(path) { - asyncProxy = new AsyncProxy(path); +function initWorker(options) { + if (!options.worker && + (typeof window === 'undefined' || !window.Worker)) { + return false; + } + asyncProxy = new AsyncProxy(options); + return true; +} + +/** + * Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker() + * @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized + */ +function getWorker() { + return asyncProxy; } /** @@ -68,7 +83,7 @@ function encryptMessage(keys, text) { keys = [keys]; } - if (useWorker()) { + if (asyncProxy) { return asyncProxy.encryptMessage(keys, text); } @@ -95,7 +110,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) { publicKeys = [publicKeys]; } - if (useWorker()) { + if (asyncProxy) { return asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text); } @@ -119,7 +134,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) { * @static */ function decryptMessage(privateKey, msg) { - if (useWorker()) { + if (asyncProxy) { return asyncProxy.decryptMessage(privateKey, msg); } @@ -145,7 +160,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg) { publicKeys = [publicKeys]; } - if (useWorker()) { + if (asyncProxy) { return asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg); } @@ -174,7 +189,7 @@ function signClearMessage(privateKeys, text) { privateKeys = [privateKeys]; } - if (useWorker()) { + if (asyncProxy) { return asyncProxy.signClearMessage(privateKeys, text); } @@ -199,7 +214,7 @@ function verifyClearSignedMessage(publicKeys, msg) { publicKeys = [publicKeys]; } - if (useWorker()) { + if (asyncProxy) { return asyncProxy.verifyClearSignedMessage(publicKeys, msg); } @@ -229,7 +244,7 @@ function verifyClearSignedMessage(publicKeys, msg) { */ function generateKeyPair(options) { // use web worker if web crypto apis are not supported - if (!util.getWebCrypto() && useWorker()) { + if (!util.getWebCrypto() && asyncProxy) { return asyncProxy.generateKeyPair(options); } @@ -259,22 +274,6 @@ function generateKeyPair(options) { // helper functions // -/** - * Are we in a browser and do we support worker? - */ -function useWorker() { - if (typeof window === 'undefined' || !window.Worker) { - return false; - } - - if (!asyncProxy) { - console.log('You need to set the worker path!'); - return false; - } - - return true; -} - /** * Command pattern that wraps synchronous code into a promise * @param {function} cmd The synchronous function with a return value @@ -307,6 +306,7 @@ function onError(message, error) { } exports.initWorker = initWorker; +exports.getWorker = getWorker; exports.encryptMessage = encryptMessage; exports.signAndEncryptMessage = signAndEncryptMessage; exports.decryptMessage = decryptMessage; diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 63a10e30..7a6c9e33 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -39,8 +39,12 @@ var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker * @constructor * @param {String} path The path to the worker or 'openpgp.worker.js' by default */ -function AsyncProxy(path) { - this.worker = new Worker(path || 'openpgp.worker.js'); +function AsyncProxy(options) { + if (options && options.worker) { + this.worker = options.worker; + } else { + this.worker = new Worker(options && options.path || 'openpgp.worker.js'); + } this.worker.onmessage = this.onMessage.bind(this); this.worker.onerror = function(e) { throw new Error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')'); diff --git a/test/worker/api.js b/test/worker/api.js index 502ced3b..413d29d5 100644 --- a/test/worker/api.js +++ b/test/worker/api.js @@ -172,12 +172,31 @@ var priv_key_de = expect(privKeyDE).to.exist; } +describe('Init Worker', function() { + + this.timeout(0); + + it('openpgp.getWorker method', function (done) { + expect(openpgp.getWorker()).to.be.null; + var workerAvailable = openpgp.initWorker({path: '../dist/openpgp.worker.js'}); + expect(workerAvailable).to.be.true; + expect(openpgp.getWorker()).to.exist; + privKeyRSA = openpgp.key.readArmored(priv_key_rsa).keys[0]; + expect(privKeyRSA.primaryKey.isDecrypted).to.be.false; + openpgp.getWorker().decryptKeyPacket(privKeyRSA, [privKeyRSA.primaryKey.getKeyId()], 'hello world').then(function(key) { + expect(key.primaryKey.isDecrypted).to.be.true; + done(); + }).catch(done); + }); + +}); + describe('High level API', function() { this.timeout(0); before(function() { - openpgp.initWorker('../dist/openpgp.worker.js'); + openpgp.initWorker({path: '../dist/openpgp.worker.js'}); initKeys(); }); @@ -353,7 +372,7 @@ describe('High level API', function() { }); it('Depleted random buffer in worker gives error', function (done) { - var wProxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); + var wProxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); wProxy.worker = new Worker('../dist/openpgp.worker.js'); wProxy.worker.onmessage = wProxy.onMessage.bind(wProxy); wProxy.seedRandom(10); @@ -385,7 +404,7 @@ describe('High level API', function() { var msg, proxy; beforeEach(function() { - proxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); + proxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); initKeys(); msg = openpgp.message.fromText(plaintext).encrypt([pubKeyRSA]); }); From de0a6d12594619f836f949cbb9b83353cca115bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Mon, 15 Dec 2014 14:34:02 +0100 Subject: [PATCH 3/4] Helper method: get Keyid object from hex keyid. --- src/type/keyid.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/type/keyid.js b/src/type/keyid.js index 438a124d..2cbf123b 100644 --- a/src/type/keyid.js +++ b/src/type/keyid.js @@ -71,3 +71,9 @@ module.exports.fromClone = function (clone) { keyid.bytes = clone.bytes; return keyid; }; + +module.exports.fromId = function (hex) { + var keyid = new Keyid(); + keyid.read(util.hex2bin(hex)); + return keyid; +}; From 712f807e503ffd1aa0a22618824bec5bfd92a84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Sat, 24 Jan 2015 14:44:06 +0100 Subject: [PATCH 4/4] Restore path parameter of initWorker method --- src/openpgp.js | 16 +++++++++------- src/worker/async_proxy.js | 6 ++++-- test/worker/api.js | 8 ++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index 955e9147..f70d9f9c 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -50,17 +50,19 @@ var asyncProxy = null; // instance of the asyncproxy /** * Set the path for the web worker script and create an instance of the async proxy - * @param {Object} [options.path=String] relative path to the worker scripts, default: 'openpgp.worker.js' - * [options.worker=module:async_proxy~AsyncProxy] initialized AsyncProxy object to be used as a worker + * @param {String} path relative path to the worker scripts, default: 'openpgp.worker.js' + * @param {Object} [options.worker=Object] alternative to path parameter: + * web worker initialized with 'openpgp.worker.js' * @return {Boolean} true if worker created successfully */ -function initWorker(options) { - if (!options.worker && - (typeof window === 'undefined' || !window.Worker)) { +function initWorker(path, options) { + if (options && options.worker || + typeof window !== 'undefined' && window.Worker) { + asyncProxy = new AsyncProxy(path, options); + return true; + } else { return false; } - asyncProxy = new AsyncProxy(options); - return true; } /** diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 7a6c9e33..6d08b07a 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -38,12 +38,14 @@ var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker * Initializes a new proxy and loads the web worker * @constructor * @param {String} path The path to the worker or 'openpgp.worker.js' by default + * @param {Object} [options.worker=Object] alternative to path parameter: + * web worker initialized with 'openpgp.worker.js' */ -function AsyncProxy(options) { +function AsyncProxy(path, options) { if (options && options.worker) { this.worker = options.worker; } else { - this.worker = new Worker(options && options.path || 'openpgp.worker.js'); + this.worker = new Worker(path || 'openpgp.worker.js'); } this.worker.onmessage = this.onMessage.bind(this); this.worker.onerror = function(e) { diff --git a/test/worker/api.js b/test/worker/api.js index 413d29d5..1fea6b07 100644 --- a/test/worker/api.js +++ b/test/worker/api.js @@ -178,7 +178,7 @@ describe('Init Worker', function() { it('openpgp.getWorker method', function (done) { expect(openpgp.getWorker()).to.be.null; - var workerAvailable = openpgp.initWorker({path: '../dist/openpgp.worker.js'}); + var workerAvailable = openpgp.initWorker('../dist/openpgp.worker.js'); expect(workerAvailable).to.be.true; expect(openpgp.getWorker()).to.exist; privKeyRSA = openpgp.key.readArmored(priv_key_rsa).keys[0]; @@ -196,7 +196,7 @@ describe('High level API', function() { this.timeout(0); before(function() { - openpgp.initWorker({path: '../dist/openpgp.worker.js'}); + openpgp.initWorker('../dist/openpgp.worker.js'); initKeys(); }); @@ -372,7 +372,7 @@ describe('High level API', function() { }); it('Depleted random buffer in worker gives error', function (done) { - var wProxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); + var wProxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); wProxy.worker = new Worker('../dist/openpgp.worker.js'); wProxy.worker.onmessage = wProxy.onMessage.bind(wProxy); wProxy.seedRandom(10); @@ -404,7 +404,7 @@ describe('High level API', function() { var msg, proxy; beforeEach(function() { - proxy = new openpgp.AsyncProxy({path: '../dist/openpgp.worker.js'}); + proxy = new openpgp.AsyncProxy('../dist/openpgp.worker.js'); initKeys(); msg = openpgp.message.fromText(plaintext).encrypt([pubKeyRSA]); });