[Pkg-javascript-commits] [node-diffie-hellman] 04/88: works

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:12 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 a4ec953802fdc5944a0318331bfadea364942cde
Author: Calvin Metcalf <calvin.metcalf at state.ma.us>
Date:   Sun Nov 2 20:03:53 2014 -0500

    works
---
 dh.js        | 24 ++++++++++--------------
 index.js     |  2 +-
 inject.js    |  3 ++-
 package.json |  6 +++++-
 readme.md    |  2 +-
 test.js      | 29 +++++++++++++++++++++++++++++
 6 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/dh.js b/dh.js
index 0ee4136..7cafdcd 100644
--- a/dh.js
+++ b/dh.js
@@ -4,7 +4,7 @@ module.exports = DH;
 
 function DH(prime, crypto) {
 	this.setGenerator(new Buffer([2]));
-	this.__prime = new BN(prime.toString('hex'), 16);
+	this.__prime = new BN(prime);
 	this._prime = BN.mont(this.__prime);
 	this._pub = void 0;
 	this._priv = void 0;
@@ -19,15 +19,15 @@ DH.prototype.generateKeys = function () {
 };
 
 DH.prototype.computeSecret = function (other) {
-	other = new BN(other.toString('hex'), 16);
+	other = new BN(other);
 	other = other.toRed(this._prime);
 	var secret = other.redPow(this._priv).fromRed();
-	return new Buffer(secret.toString('hex'), 'hex');
+	return new Buffer(secret.toArray());
 }
-DH.prototype.getPublicKey = function () {
+DH.prototype.getPublicKey = function (enc) {
 	return returnValue(this._pub, enc);
 };
-DH.prototype.getPrivateKey = function () {
+DH.prototype.getPrivateKey = function (enc) {
 	return returnValue(this._priv, enc);
 };
 
@@ -42,31 +42,27 @@ DH.prototype.setGenerator = function (gen, enc) {
 	if (!Buffer.isBuffer(gen)) {
 		gen = new Buffer(gen, enc);
 	}
-	this._gen = new BH(gen.toString('hex'), 16);
+	this._gen = new BN(gen);
 }
 DH.prototype.setPublicKey = function (pub, enc) {
 	enc = enc || 'utf8';
 	if (!Buffer.isBuffer(pub)) {
 		pub = new Buffer(pub, enc);
 	}
-	this._pub = new BH(pub.toString('hex'), 16);
+	this._pub = new BN(pub);
 }
 DH.prototype.setPrivateKey = function (priv, enc) {
 	enc = enc || 'utf8';
 	if (!Buffer.isBuffer(priv)) {
 		priv = new Buffer(priv, enc);
 	}
-	this._priv = new BH(priv.toString('hex'), 16);
+	this._priv = new BN(priv);
 }
 function returnValue(bn, enc) {
-	var hex = bn.toString(16);
-	if (enc === 'hex') {
-		return hex;
-	}
-	var buf = new Buffer(hex, 'hex');
+	var buf = new Buffer(bn.toArray());
 	if (!enc) {
 		return buf;
 	} else {
-		return buf.toString(end);
+		return buf.toString(enc);
 	}
 }
\ No newline at end of file
diff --git a/index.js b/index.js
index 17326b3..93c33ea 100644
--- a/index.js
+++ b/index.js
@@ -1,3 +1,3 @@
-exports.__browserify = require('inject');
+exports.__browserify = require('./inject');
 
 exports.__browserify(require('crypto'), exports);
\ No newline at end of file
diff --git a/inject.js b/inject.js
index ebda2a3..536fadd 100644
--- a/inject.js
+++ b/inject.js
@@ -1,7 +1,8 @@
 var primes = require('./primes.json');
+var DH = require('./dh');
 module.exports = function (crypto, exports) {
 	exports.getDiffieHellman = function (mod) {
-		return new DH(primes[mod], crypto);
+		return new DH(new Buffer(primes[mod].prime, 'hex'), crypto);
 	}
 	exports.createDiffieHellman = function (prime, enc) {
 		if (typeof prime === 'number') {
diff --git a/package.json b/package.json
index fbaaee7..6ae6655 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
   "description": "pure js diffie-hellman",
   "main": "index.js",
   "scripts": {
-    "test": "node test.js"
+    "test": "node test.js | tspec"
   },
   "repository": {
     "type": "git",
@@ -24,5 +24,9 @@
   "homepage": "https://github.com/calvinmetcalf/diffie-hellman",
   "dependencies": {
     "bn.js": "^0.15.0"
+  },
+  "devDependencies": {
+    "tap-spec": "^1.0.1",
+    "tape": "^3.0.1"
   }
 }
diff --git a/readme.md b/readme.md
index 2909dfa..79d1e43 100644
--- a/readme.md
+++ b/readme.md
@@ -1,4 +1,4 @@
 diffie hellman
 ====
 
-pure js diffie-hellman
\ No newline at end of file
+pure js diffie-hellman, same api as node, 'modp17', 'modp18' don't work at the moment
\ No newline at end of file
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..f072fc0
--- /dev/null
+++ b/test.js
@@ -0,0 +1,29 @@
+var test = require('tape');
+var nodeCrypto = require('crypto');
+var myCrypto = require('./');
+
+var mods = [
+   'modp1', 'modp2', 'modp5', 'modp14', 'modp15', 'modp16'/*, 'modp17', 'modp18'*/
+];
+
+function run(i) {
+	mods.forEach(function (mod) {
+		test(mod + ' run ' + i, function (t){
+			t.plan(2);
+			var dh1 = nodeCrypto.getDiffieHellman(mod);
+			var p1 = dh1.getPrime().toString('hex');
+			dh1.generateKeys();
+			var dh2 = myCrypto.getDiffieHellman(mod);
+			var p2 = dh2.getPrime().toString('hex');
+			dh2.generateKeys();
+			t.equals(p1, p2, 'equal primes')
+			var pub1 = dh1.computeSecret(dh2.getPublicKey()).toString('hex');
+			var pub2 = dh2.computeSecret(dh1.getPublicKey()).toString('hex');
+			t.equals(pub1, pub2, 'equal secrets');
+		});
+	});
+}
+var i = 0;
+while (++i < 3) {
+	run(i);
+}
\ No newline at end of file

-- 
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