diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js index 0ffb8b4d..05da060e 100644 --- a/src/crypto/public_key/rsa.js +++ b/src/crypto/public_key/rsa.js @@ -134,12 +134,13 @@ function RSA() { // Generate a new random private key B bits long, using public expt E function generate(B, E, callback) { + var webCrypto = util.getWebCrypto(); // // Native RSA keygen using Web Crypto // - if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) { + if (webCrypto) { var Euint32 = new Uint32Array([parseInt(E, 16)]); // get integer of exponent var Euint8 = new Uint8Array(Euint32.buffer); // get bytes of exponent @@ -152,7 +153,7 @@ function RSA() { } }; - var gen = window.crypto.subtle.generateKey(keyGenOpt, true, ['sign', 'verify']); + var gen = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']); gen.then(exportKey).then(decodeKey).catch(onError); return; @@ -161,7 +162,7 @@ function RSA() { function exportKey(key) { // export the generated keys as JsonWebKey (JWK) // https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33 - return window.crypto.subtle.exportKey('jwk', key.privateKey); + return webCrypto.exportKey('jwk', key.privateKey); } function decodeKey(jwk) { diff --git a/src/openpgp.js b/src/openpgp.js index 9d3e4141..d71cfb92 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -38,6 +38,7 @@ var armor = require('./encoding/armor.js'), message = require('./message.js'), cleartext = require('./cleartext.js'), key = require('./key.js'), + util = require('./util'), AsyncProxy = require('./worker/async_proxy.js'); var asyncProxy; // instance of the asyncproxy @@ -234,7 +235,7 @@ function generateKeyPair(options, callback) { } // use web worker if web crypto apis are not supported - if (!useWebCrypto() && useWorker(callback)) { + if (!util.getWebCrypto() && useWorker(callback)) { asyncProxy.generateKeyPair(options, callback); return; } @@ -273,13 +274,6 @@ function useWorker(callback) { return true; } -/** - * Check for WebCrypto support - */ -function useWebCrypto() { - return typeof window !== 'undefined' && window.crypto && window.crypto.subtle; -} - /** * Command pattern that handles async calls gracefully */ diff --git a/src/util.js b/src/util.js index 8ec8a74f..8b16a133 100644 --- a/src/util.js +++ b/src/util.js @@ -1,16 +1,16 @@ // GPG4Browsers - An OpenPGP implementation in javascript // Copyright (C) 2011 Recurity Labs GmbH -// +// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3.0 of the License, or (at your option) any later version. -// +// // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. -// +// // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA @@ -193,7 +193,7 @@ module.exports = { }, /** - * Convert a Uint8Array to a string. This currently functions + * Convert a Uint8Array to a string. This currently functions * the same as bin2str. * @function module:util.Uint8Array2str * @param {Uint8Array} bin An array of (binary) integers to convert @@ -228,7 +228,7 @@ module.exports = { }, /** - * Helper function to print a debug message. Debug + * Helper function to print a debug message. Debug * messages are only printed if * @link module:config/config.debug is set to true. * @param {String} str String of the debug message @@ -240,7 +240,7 @@ module.exports = { }, /** - * Helper function to print a debug message. Debug + * Helper function to print a debug message. Debug * messages are only printed if * @link module:config/config.debug is set to true. * Different than print_debug because will call hexstrdump iff necessary. @@ -265,9 +265,9 @@ module.exports = { /** * Shifting a string to n bits right * @param {String} value The string to shift - * @param {Integer} bitcount Amount of bits to shift (MUST be smaller + * @param {Integer} bitcount Amount of bits to shift (MUST be smaller * than 9) - * @return {String} Resulting string. + * @return {String} Resulting string. */ shiftRight: function (value, bitcount) { var temp = util.str2bin(value); @@ -305,5 +305,16 @@ module.exports = { return "SHA224"; } return "unknown"; + }, + + getWebCrypto: function() { + if (config.useWebCrypto === false) { + // make web crypto optional + return; + } + + if (typeof window !== 'undefined' && window.crypto && window.crypto.subtle) { + return window.crypto.subtle; + } } };