Make WebCrypto optional with config.useWebCrypto
This commit is contained in:
parent
e6f66b9039
commit
72cb1cfc49
|
@ -134,12 +134,13 @@ function RSA() {
|
||||||
// Generate a new random private key B bits long, using public expt E
|
// Generate a new random private key B bits long, using public expt E
|
||||||
|
|
||||||
function generate(B, E, callback) {
|
function generate(B, E, callback) {
|
||||||
|
var webCrypto = util.getWebCrypto();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Native RSA keygen using Web Crypto
|
// 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 Euint32 = new Uint32Array([parseInt(E, 16)]); // get integer of exponent
|
||||||
var Euint8 = new Uint8Array(Euint32.buffer); // get bytes 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);
|
gen.then(exportKey).then(decodeKey).catch(onError);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -161,7 +162,7 @@ function RSA() {
|
||||||
function exportKey(key) {
|
function exportKey(key) {
|
||||||
// export the generated keys as JsonWebKey (JWK)
|
// export the generated keys as JsonWebKey (JWK)
|
||||||
// https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33
|
// 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) {
|
function decodeKey(jwk) {
|
||||||
|
|
|
@ -38,6 +38,7 @@ var armor = require('./encoding/armor.js'),
|
||||||
message = require('./message.js'),
|
message = require('./message.js'),
|
||||||
cleartext = require('./cleartext.js'),
|
cleartext = require('./cleartext.js'),
|
||||||
key = require('./key.js'),
|
key = require('./key.js'),
|
||||||
|
util = require('./util'),
|
||||||
AsyncProxy = require('./worker/async_proxy.js');
|
AsyncProxy = require('./worker/async_proxy.js');
|
||||||
|
|
||||||
var asyncProxy; // instance of the asyncproxy
|
var asyncProxy; // instance of the asyncproxy
|
||||||
|
@ -234,7 +235,7 @@ function generateKeyPair(options, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// use web worker if web crypto apis are not supported
|
// use web worker if web crypto apis are not supported
|
||||||
if (!useWebCrypto() && useWorker(callback)) {
|
if (!util.getWebCrypto() && useWorker(callback)) {
|
||||||
asyncProxy.generateKeyPair(options, callback);
|
asyncProxy.generateKeyPair(options, callback);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -273,13 +274,6 @@ function useWorker(callback) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check for WebCrypto support
|
|
||||||
*/
|
|
||||||
function useWebCrypto() {
|
|
||||||
return typeof window !== 'undefined' && window.crypto && window.crypto.subtle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command pattern that handles async calls gracefully
|
* Command pattern that handles async calls gracefully
|
||||||
*/
|
*/
|
||||||
|
|
11
src/util.js
11
src/util.js
|
@ -305,5 +305,16 @@ module.exports = {
|
||||||
return "SHA224";
|
return "SHA224";
|
||||||
}
|
}
|
||||||
return "unknown";
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user