[Pkg-javascript-commits] [node-diffie-hellman] 43/88: modularize
Bastien Roucariès
rouca at moszumanska.debian.org
Thu May 4 10:19:15 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-diffie-hellman.
commit c87808991e5b9b723423cb7389fa68f3ab15d948
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date: Tue Jan 20 16:44:55 2015 -0500
modularize
---
browser.js | 35 +++++++++++++++++++++++++++++++++++
dh.js | 6 +++---
generatePrime.js | 6 +++---
index.js | 11 +++++++++--
inject.js | 36 ------------------------------------
package.json | 10 ++++++----
readme.md | 4 ++--
test.js | 4 ++--
8 files changed, 60 insertions(+), 52 deletions(-)
diff --git a/browser.js b/browser.js
new file mode 100644
index 0000000..e3457c5
--- /dev/null
+++ b/browser.js
@@ -0,0 +1,35 @@
+var primes = require('./primes.json');
+var DH = require('./dh');
+var generatePrime = require('./generatePrime');
+
+exports.DiffieHellmanGroup =
+ exports.createDiffieHellmanGroup =
+ exports.getDiffieHellman = DiffieHellmanGroup;
+function DiffieHellmanGroup(mod) {
+ return new DH(new Buffer(primes[mod].prime, 'hex'),
+ new Buffer(primes[mod].gen, 'hex'));
+}
+exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
+function DiffieHellman(prime, enc, generator, genc) {
+
+ if (Buffer.isBuffer(enc) ||
+ (typeof enc === 'string' && ['hex', 'binary', 'base64'].indexOf(enc) === -1)) {
+ genc = generator;
+ generator = enc;
+ enc = void 0;
+ }
+ enc = enc || 'binary';
+ genc = genc || 'binary';
+ generator = generator || new Buffer([2]);
+ if (!Buffer.isBuffer(generator)) {
+ generator = new Buffer(generator, genc);
+ }
+ if (typeof prime === 'number') {
+ return new DH(generatePrime(prime, generator), generator, true);
+ }
+ if (!Buffer.isBuffer(prime)) {
+ prime = new Buffer(prime, enc);
+ }
+
+ return new DH(prime, generator, true);
+}
diff --git a/dh.js b/dh.js
index 4b9c35b..ae28d5e 100644
--- a/dh.js
+++ b/dh.js
@@ -7,6 +7,7 @@ var TEN = new BN(10);
var THREE = new BN(3);
var SEVEN = new BN(7);
var primes = require('./generatePrime');
+var randomBytes = require('randombytes');
module.exports = DH;
function setPublicKey(pub, enc) {
enc = enc || 'utf8';
@@ -54,7 +55,6 @@ function checkPrime(prime, generator) {
//not a safe prime
error += 2;
}
- var gen = generator.toString('hex');
var rem;
switch (gen) {
case '02':
@@ -87,7 +87,7 @@ function defineError (self, error) {
self.verifyError = error;
}
}
-function DH(prime, generator,crypto, malleable) {
+function DH(prime, generator, malleable) {
this.setGenerator(generator);
this.__prime = new BN(prime);
this._prime = BN.mont(this.__prime);
@@ -102,7 +102,7 @@ function DH(prime, generator,crypto, malleable) {
defineError(this, 8);
}
this._makeNum = function makeNum() {
- return crypto.randomBytes(prime.length);
+ return randomBytes(prime.length);
};
}
DH.prototype.generateKeys = function () {
diff --git a/generatePrime.js b/generatePrime.js
index ff9a1d7..5f6300b 100644
--- a/generatePrime.js
+++ b/generatePrime.js
@@ -1,4 +1,4 @@
-
+var randomBytes = require('randombytes');
module.exports = findPrime;
findPrime.simpleSieve = simpleSieve;
findPrime.fermatTest = fermatTest;
@@ -58,7 +58,7 @@ function fermatTest(p) {
var red = BN.mont(p);
return TWO.toRed(red).redPow(p.subn(1)).fromRed().cmpn(1) === 0;
}
-function findPrime(bits, gen ,crypto) {
+function findPrime(bits, gen) {
if (bits < 16) {
// this is what openssl does
if (gen === 2 || gen === 5) {
@@ -71,7 +71,7 @@ function findPrime(bits, gen ,crypto) {
var runs, comp;
function generateRandom(bits) {
runs = -1;
- var out = new BN(crypto.randomBytes(Math.ceil(bits / 8)));
+ var out = new BN(randomBytes(Math.ceil(bits / 8)));
while (out.bitLength() > bits) {
out.ishrn(1);
}
diff --git a/index.js b/index.js
index 93c33ea..6a2b03d 100644
--- a/index.js
+++ b/index.js
@@ -1,3 +1,10 @@
-exports.__browserify = require('./inject');
+var crypto = require('crypto');
-exports.__browserify(require('crypto'), exports);
\ No newline at end of file
+//getDiffieHellman
+exports.DiffieHellmanGroup = crypto.DiffieHellmanGroup;
+exports.createDiffieHellmanGroup = crypto.createDiffieHellmanGroup;
+exports.getDiffieHellman = crypto.getDiffieHellman;
+
+//createDiffieHellman
+exports.createDiffieHellman = crypto.createDiffieHellman;
+exports.DiffieHellman = crypto.DiffieHellman;
\ No newline at end of file
diff --git a/inject.js b/inject.js
deleted file mode 100644
index 85fdcf7..0000000
--- a/inject.js
+++ /dev/null
@@ -1,36 +0,0 @@
-var primes = require('./primes.json');
-var DH = require('./dh');
-var generatePrime = require('./generatePrime');
-module.exports = function (crypto, exports) {
- exports.DiffieHellmanGroup =
- exports.createDiffieHellmanGroup =
- exports.getDiffieHellman = DiffieHellmanGroup;
- function DiffieHellmanGroup(mod) {
- return new DH(new Buffer(primes[mod].prime, 'hex'),
- new Buffer(primes[mod].gen, 'hex'), crypto);
- }
- exports.createDiffieHellman = exports.DiffieHellman = DiffieHellman;
- function DiffieHellman(prime, enc, generator, genc) {
-
- if (Buffer.isBuffer(enc) ||
- (typeof enc === 'string' && ['hex', 'binary', 'base64'].indexOf(enc) === -1)) {
- genc = generator;
- generator = enc
- enc = void 0;
- }
- enc = enc || 'binary';
- genc = genc || 'binary';
- generator = generator || new Buffer([2]);
- if (!Buffer.isBuffer(generator)) {
- generator = new Buffer(generator, genc);
- }
- if (typeof prime === 'number') {
- return new DH(generatePrime(prime, generator, crypto), generator, crypto, true);
- }
- if (!Buffer.isBuffer(prime)) {
- prime = new Buffer(prime, enc);
- }
-
- return new DH(prime, generator, crypto, true);
- };
-}
\ No newline at end of file
diff --git a/package.json b/package.json
index bebf365..84c6687 100644
--- a/package.json
+++ b/package.json
@@ -3,12 +3,13 @@
"version": "2.2.3",
"description": "pure js diffie-hellman",
"main": "index.js",
+ "browser": "browser.js",
"scripts": {
"test": "node test.js | tspec"
},
"repository": {
"type": "git",
- "url": "https://github.com/calvinmetcalf/diffie-hellman.git"
+ "url": "https://github.com/crypto-browserify/diffie-hellman.git"
},
"keywords": [
"diffie",
@@ -19,12 +20,13 @@
"author": "Calvin Metcalf",
"license": "MIT",
"bugs": {
- "url": "https://github.com/calvinmetcalf/diffie-hellman/issues"
+ "url": "https://github.com/crypto-browserify/diffie-hellman/issues"
},
- "homepage": "https://github.com/calvinmetcalf/diffie-hellman",
+ "homepage": "https://github.com/crypto-browserify/diffie-hellman",
"dependencies": {
"bn.js": "^1.0.0",
- "miller-rabin": "^1.1.2"
+ "miller-rabin": "^1.1.2",
+ "randombytes": "^2.0.0"
},
"devDependencies": {
"tap-spec": "^1.0.1",
diff --git a/readme.md b/readme.md
index 0080c5b..2912d1a 100644
--- a/readme.md
+++ b/readme.md
@@ -1,4 +1,4 @@
-diffie hellman [![Build Status](https://travis-ci.org/calvinmetcalf/diffie-hellman.svg)](https://travis-ci.org/calvinmetcalf/diffie-hellman)
+diffie hellman [![Build Status](https://travis-ci.org/crypto-browserify/diffie-hellman.svg)](https://travis-ci.org/crypto-browserify/diffie-hellman)
====
-pure js diffie-hellman, same api as node, most hard parts thanks to [bn.js](https://www.npmjs.org/package/bn.js) by [@indutny](https://github.com/indutny).
\ No newline at end of file
+pure js diffie-hellman, same api as node, most hard parts thanks to [bn.js](https://www.npmjs.org/package/bn.js) by [@indutny](https://github.com/indutny). In node just returns an object with `crypto.getDiffieHellman` and `crypto.getDiffieHellman` in the browser returns a shim. To require the pure JavaScript one in node `require('diffie-hellman/browser');`;
\ No newline at end of file
diff --git a/test.js b/test.js
index 59d9b01..2ef1326 100644
--- a/test.js
+++ b/test.js
@@ -1,6 +1,6 @@
var test = require('tape');
-var nodeCrypto = require('crypto');
-var myCrypto = require('./');
+var nodeCrypto = require('./');
+var myCrypto = require('./browser');
var mods = [
'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16', 'modp17', 'modp18'
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-diffie-hellman.git
More information about the Pkg-javascript-commits
mailing list