Skip to content

Commit 5814e21

Browse files
davidlehndlongley
authored andcommitted
Remove multiple forge instances feature.
- Remove abilty to create a new forge container by calling the top-level container with new options. Doing this correctly would add too much implementation complexity compared to its usefulness. - Move global options to the forge.options object. - Change disableNativeCode option to usePureJavaScript.
1 parent 79aef5e commit 5814e21

File tree

6 files changed

+18
-49
lines changed

6 files changed

+18
-49
lines changed

README.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,36 +234,24 @@ Documentation
234234

235235
If at any time you wish to disable the use of native code, where available,
236236
for particular forge features like its secure random number generator, you
237-
may set the ```disableNativeCode``` flag on ```forge``` to ```true```. It
238-
is not recommended that you set this flag as native code is typically more
237+
may set the ```forge.options.usePureJavaScript``` flag to ```true```. It is
238+
not recommended that you set this flag as native code is typically more
239239
performant and may have stronger security properties. It may be useful to
240240
set this flag to test certain features that you plan to run in environments
241241
that are different from your testing environment.
242242

243243
To disable native code when including forge in the browser:
244244

245245
```js
246-
forge = {disableNativeCode: true};
247-
// now include forge script file(s)
248-
// Note: with this approach, script files *must*
249-
// be included after initializing the global forge var
250-
251-
// alternatively, include script files first and then call
252-
forge = forge({disableNativeCode: true});
253-
254-
// Note: forge will be permanently reconfigured now;
255-
// to avoid this but use the same "forge" var name,
256-
// you can wrap your code in a function to shadow the
257-
// global var, eg:
258-
(function(forge) {
259-
// ...
260-
})(forge({disableNativeCode: true}));
246+
// run this *after* including the forge script
247+
forge.options.usePureJavaScript = true;
261248
```
262249

263250
To disable native code when using node.js:
264251

265252
```js
266-
var forge = require('node-forge')({disableNativeCode: true});
253+
var forge = require('node-forge');
254+
forge.options.usePureJavaScript = true;
267255
```
268256

269257
Transports

lib/forge.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,11 @@
33
*
44
* @author Dave Longley
55
*
6-
* Copyright 2011-2014 Digital Bazaar, Inc.
6+
* Copyright 2011-2016 Digital Bazaar, Inc.
77
*/
8-
// default options
9-
var options;
10-
if(typeof forge !== 'undefined') {
11-
options = forge;
12-
} else {
13-
options = {
14-
disableNativeCode: false
15-
};
16-
}
17-
function makeForge(options) {
18-
var self = this;
19-
// create new forge container function
20-
// can be called with new options to create independent copy
21-
var forge = function() {
22-
return makeForge(options);
23-
};
24-
function set(value, key) {
25-
forge[key] = value;
8+
module.exports = {
9+
// default options
10+
options: {
11+
usePureJavaScript: false
2612
}
27-
// copy old properties
28-
Object.keys(self).forEach(set);
29-
// set options
30-
Object.keys(options).forEach(set);
31-
return forge;
32-
}
33-
module.exports = makeForge(options);
13+
};

lib/pbkdf2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var pkcs5 = forge.pkcs5 = forge.pkcs5 || {};
1717
var _nodejs = (
1818
typeof process !== 'undefined' && process.versions && process.versions.node);
1919
var crypto;
20-
if(_nodejs && !forge.disableNativeCode) {
20+
if(_nodejs && !forge.options.usePureJavaScript) {
2121
crypto = require('crypto');
2222
}
2323

@@ -46,7 +46,7 @@ module.exports = forge.pbkdf2 = pkcs5.pbkdf2 = function(
4646

4747
// use native implementation if possible and not disabled, note that
4848
// some node versions only support SHA-1, others allow digest to be changed
49-
if(_nodejs && !forge.disableNativeCode && crypto.pbkdf2 &&
49+
if(_nodejs && !forge.options.usePureJavaScript && crypto.pbkdf2 &&
5050
(md === null || typeof md !== 'object') &&
5151
(crypto.pbkdf2Sync.length > 4 || (!md || md === 'sha1'))) {
5252
if(typeof md !== 'string') {

lib/prng.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ require('./util')
1616
var _nodejs = (
1717
typeof process !== 'undefined' && process.versions && process.versions.node);
1818
var _crypto = null;
19-
if(!forge.disableNativeCode && _nodejs && !process.versions['node-webkit']) {
19+
if(_nodejs && !forge.options.usePureJavaScript &&
20+
!process.versions['node-webkit']) {
2021
_crypto = require('crypto');
2122
}
2223

lib/random.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ if(typeof window !== 'undefined') {
125125
};
126126
}
127127
}
128-
if(forge.disableNativeCode || (!_nodejs && !getRandomValues)) {
128+
if(forge.options.usePureJavaScript || (!_nodejs && !getRandomValues)) {
129129
// if this is a web worker, do not use weak entropy, instead register to
130130
// receive strong entropy asynchronously from the main thread
131131
if(typeof window === 'undefined' || window.document === undefined) {

lib/rsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ pki.rsa.generateKeyPair = function(bits, e, options, callback) {
885885

886886
// if native code is permitted and a callback is given, use native
887887
// key generation code if available and if parameters are acceptable
888-
if(!forge.disableNativeCode && callback &&
888+
if(!forge.options.usePureJavaScript && callback &&
889889
bits >= 256 && bits <= 16384 && (e === 0x10001 || e === 3)) {
890890
if(_detectSubtleCrypto('generateKey') && _detectSubtleCrypto('exportKey')) {
891891
// use standard native generateKey

0 commit comments

Comments
 (0)