Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require minimal digest algorithm dependencies. #471

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ Forge ChangeLog
- Improve PhantomJS support.
- Move Bower/bundle support to
[forge-dist](https://github.com/digitalbazaar/forge-dist).
- **BREAKING**: Require minimal digest algorithm dependencies from individual
modules.

### Added

- webpack bundler support via `npm run build`:
- Builds .js, .min.js, and basic sourcemaps.
- Basic build: forge.js.
- Build with extra utils and networking support: forge.all.js.
- Build WebWorker support: prime.worker.js.
- Builds `.js`, `.min.js`, and basic sourcemaps.
- Basic build: `forge.js`.
- Build with extra utils and networking support: `forge.all.js`.
- Build WebWorker support: `prime.worker.js`.
- Browserify support in package.json.
- Karma browser testing.
- `forge.options` field.
Expand All @@ -38,6 +40,7 @@ Forge ChangeLog
- Add pbkdf2 usePureJavaScript test.
- Add rsa.generateKeyPair async and usePureJavaScript tests.
- Add .editorconfig support.
- Add `md.all.js` which includes all digest algorithms.

### Removed

Expand Down Expand Up @@ -68,6 +71,13 @@ Forge ChangeLog
also now in a different directory.
- (all) The library should now directly support building custom bundles with
webpack, browserify, or similar.
- (all) If building a custom bundle ensure the correct dependencies are
included. In particular, note there is now a `md.all.js` file to include all
digest algorithms. Individual files limit what they include by default to
allow smaller custom builds. For instance, `pbdkf2.js` has a `sha1` default
but does not include any algorithm files by default. This allows the
possibility to include only `sha256` without the overhead of `sha1` and
`sha512`.

### Notes

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require('./des');
require('./hmac');
require('./kem');
require('./log');
require('./md');
require('./md.all');
require('./mgf1');
require('./pbkdf2');
require('./pem');
Expand Down
13 changes: 13 additions & 0 deletions lib/md.all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Node.js module for all known Forge message digests.
*
* @author Dave Longley
*
* Copyright 2011-2017 Digital Bazaar, Inc.
*/
module.exports = require('./md');

require('./md5');
require('./sha1');
require('./sha256');
require('./sha512');
5 changes: 0 additions & 5 deletions lib/md.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@ var forge = require('./forge');

module.exports = forge.md = forge.md || {};
forge.md.algorithms = forge.md.algorithms || {};

require('./md5');
require('./sha1');
require('./sha256');
require('./sha512');
3 changes: 1 addition & 2 deletions lib/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* Copyright (c) 2010-2014 Digital Bazaar, Inc.
*/
var forge = require('./forge');
require('./md');
require('./util');

var md5 = module.exports = forge.md5 = forge.md5 || {};
forge.md = forge.md || {};
forge.md.algorithms = forge.md.algorithms || {};
forge.md.md5 = forge.md.algorithms.md5 = md5;

/**
Expand Down
11 changes: 10 additions & 1 deletion lib/pbe.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ require('./asn1');
require('./des');
require('./md');
require('./oids');
require('./pem');
require('./pbkdf2');
require('./pem');
require('./random');
require('./rc2');
require('./rsa');
Expand Down Expand Up @@ -643,6 +643,9 @@ pki.pbe.generatePkcs12Key = function(password, salt, id, iter, n, md) {
var j, l;

if(typeof md === 'undefined' || md === null) {
if(!('sha1' in forge.md)) {
throw new Error('"sha1" hash algorithm unavailable.');
}
md = forge.md.sha1.create();
}

Expand Down Expand Up @@ -929,6 +932,9 @@ pki.pbe.getCipherForPKCS12PBE = function(oid, params, password) {
*/
pki.pbe.opensslDeriveBytes = function(password, salt, dkLen, md) {
if(typeof md === 'undefined' || md === null) {
if(!('md5' in forge.md)) {
throw new Error('"md5" hash algorithm unavailable.');
}
md = forge.md.md5.create();
}
if(salt === null) {
Expand Down Expand Up @@ -983,6 +989,9 @@ function prfAlgorithmToMessageDigest(prfAlgorithm) {
'hmacWithSHA512'];
throw error;
}
if(!factory || !(prfAlgorithm in factory)) {
throw new Error('Unknown hash algorithm: ' + prfAlgorithm);
}
return factory[prfAlgorithm].create();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = forge.pbkdf2 = pkcs5.pbkdf2 = function(

if(typeof md === 'undefined' || md === null) {
// default prf to SHA-1
md = forge.md.sha1.create();
md = 'sha1';
}
if(typeof md === 'string') {
if(!(md in forge.md.algorithms)) {
Expand Down
1 change: 0 additions & 1 deletion lib/prng.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Copyright (c) 2010-2014 Digital Bazaar, Inc.
*/
var forge = require('./forge');
require('./md');
require('./util');

var _crypto = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
var forge = require('./forge');
require('./aes');
require('./md');
require('./sha256');
require('./prng');
require('./util');

Expand Down
3 changes: 1 addition & 2 deletions lib/sha1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* Copyright (c) 2010-2015 Digital Bazaar, Inc.
*/
var forge = require('./forge');
require('./md');
require('./util');

var sha1 = module.exports = forge.sha1 = forge.sha1 || {};
forge.md = forge.md || {};
forge.md.algorithms = forge.md.algorithms || {};
forge.md.sha1 = forge.md.algorithms.sha1 = sha1;

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/sha256.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
* Copyright (c) 2010-2015 Digital Bazaar, Inc.
*/
var forge = require('./forge');
require('./md');
require('./util');

var sha256 = module.exports = forge.sha256 = forge.sha256 || {};
forge.md = forge.md || {};
forge.md.algorithms = forge.md.algorithms || {};
forge.md.sha256 = forge.md.algorithms.sha256 = sha256;

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/sha512.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
* Copyright (c) 2014-2015 Digital Bazaar, Inc.
*/
var forge = require('./forge');
require('./md');
require('./util');

var sha512 = module.exports = forge.sha512 = forge.sha512 || {};
forge.md = forge.md || {};
forge.md.algorithms = forge.md.algorithms || {};

// SHA-512
forge.md.sha512 = forge.md.algorithms.sha512 = sha512;
Expand Down
3 changes: 2 additions & 1 deletion lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,11 @@
var forge = require('./forge');
require('./asn1');
require('./hmac');
require('./md');
require('./md5');
require('./pem');
require('./pki');
require('./random');
require('./sha1');
require('./util');

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/kem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var ASSERT = require('assert');
var KEM = require('../../lib/kem');
var MD = require('../../lib/md');
var MD = require('../../lib/md.all');
var RSA = require('../../lib/rsa');
var UTIL = require('../../lib/util');
var JSBN = require('../../lib/jsbn');
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mgf1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var ASSERT = require('assert');
var MD = require('../../lib/md.all');
var MGF = require('../../lib/mgf');
var MD = require('../../lib/md');
var UTIL = require('../../lib/util');

(function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pbkdf2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var ASSERT = require('assert');
var FORGE = require('../../lib/forge');
var MD = require('../../lib/md.all');
var PBKDF2 = require('../../lib/pbkdf2');
var MD = require('../../lib/md');
var UTIL = require('../../lib/util');

(function() {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/pkcs1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var ASSERT = require('assert');
var PKI = require('../../lib/pki');
var PKCS1 = require('../../lib/pkcs1');
var MD = require('../../lib/md');
var JSBN = require('../../lib/jsbn');
var MD = require('../../lib/md.all');
var PKCS1 = require('../../lib/pkcs1');
var PKI = require('../../lib/pki');
var UTIL = require('../../lib/util');

(function() {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/rsa.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var ASSERT = require('assert');
var FORGE = require('../../lib/forge');
var PKI = require('../../lib/pki');
var RSA = require('../../lib/rsa');
var MD = require('../../lib/md');
var MD = require('../../lib/md.all');
var MGF = require('../../lib/mgf');
var PKI = require('../../lib/pki');
var PSS = require('../../lib/pss');
var RANDOM = require('../../lib/random');
var RSA = require('../../lib/rsa');
var UTIL = require('../../lib/util');

(function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/x509.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var ASSERT = require('assert');
var MD = require('../../lib/md.all');
var PKI = require('../../lib/pki');
var MD = require('../../lib/md');
var UTIL = require('../../lib/util');

(function() {
Expand Down
24 changes: 21 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,33 @@ const outputs = [
library: null,
libraryTarget: null
}
// custom builds can be created by specifying the high level files you need
// webpack will pull in dependencies as needed
// Note: if using UMD or similar, add forge.js *last* to properly export
// Custom builds can be created by specifying the high level files you need
// webpack will pull in dependencies as needed.
//
// Note: Some modules, such as pbkdf2, may require explicitly listing other
// modules they require, such as sha1 or sha256. This is to allow smaller
// builds when you don't require a default dependency.
//
// Note: If using UMD or similar, add forge.js *last* to properly export
// the top level forge namespace.
//
// Example: sha1 + ...
//{
// entry: ['./lib/sha1.js', ..., './lib/forge.js'],
// filenameBase: 'forge.custom'
// libraryTarget: 'umd'
//}
// Example: PBKDF2 + sha1, explicitly include sha1 default
//{
// entry: ['./lib/pbkdf2.js', './lib/sha1.js', './lib/forge.js'],
// filenameBase: 'forge.pbkdf2-sha1'
// libraryTarget: 'umd'
// Example: PBKDF2 + sha256, without the sha1 default
//{
// entry: ['./lib/pbkdf2.js', './lib/sha256.js', './lib/forge.js'],
// filenameBase: 'forge.pbkdf2-sha256'
// libraryTarget: 'umd'
//}
];

outputs.forEach((info) => {
Expand Down