[Pkg-javascript-commits] [node-diffie-hellman] 22/88: same methods

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:13 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 c56df72c51ac83e06172f68d0066e9c2d6efa92f
Author: Calvin Metcalf <cmetcalf at appgeo.com>
Date:   Fri Nov 7 08:30:39 2014 -0500

    same methods
---
 dh.js     | 42 +++++++++++++++++++++++-------------------
 inject.js |  4 ++--
 test.js   | 12 +++++++++---
 3 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/dh.js b/dh.js
index f30786e..c76864e 100644
--- a/dh.js
+++ b/dh.js
@@ -1,20 +1,37 @@
 var BN = require('bn.js');
 
 module.exports = DH;
-
-function DH(prime, crypto) {
+function setPublicKey(pub, enc) {
+	enc = enc || 'utf8';
+	if (!Buffer.isBuffer(pub)) {
+		pub = new Buffer(pub, enc);
+	}
+	this._pub = new BN(pub);
+}
+function setPrivateKey(priv, enc) {
+	enc = enc || 'utf8';
+	if (!Buffer.isBuffer(priv)) {
+		priv = new Buffer(priv, enc);
+	}
+	this._priv = new BN(priv);
+}
+function DH(prime, crypto, malleable) {
 	this.setGenerator(new Buffer([2]));
 	this.__prime = new BN(prime);
 	this._prime = BN.mont(this.__prime);
 	this._pub = void 0;
 	this._priv = void 0;
+	if (malleable) {
+		this.setPublicKey = setPublicKey;
+		this.setPrivateKey = setPrivateKey;
+	}
 	this._makeNum = function makeNum() {
 		return crypto.randomBytes(192);
 	};
 }
 DH.prototype.generateKeys = function () {
 	if (!this._priv) {
-		this.setPrivateKey(this._makeNum());
+		this._priv = new BN(this._makeNum());
 	}
 	this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed();
 	return this.getPublicKey();
@@ -33,10 +50,10 @@ DH.prototype.computeSecret = function (other) {
 	}
 	return out;
 };
-DH.prototype.getPublicKey = function (enc) {
+DH.prototype.getPublicKey = function getPublicKey(enc) {
 	return returnValue(this._pub, enc);
 };
-DH.prototype.getPrivateKey = function (enc) {
+DH.prototype.getPrivateKey = function getPrivateKey(enc) {
 	return returnValue(this._priv, enc);
 };
 
@@ -53,20 +70,7 @@ DH.prototype.setGenerator = function (gen, enc) {
 	}
 	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 BN(pub);
-};
-DH.prototype.setPrivateKey = function (priv, enc) {
-	enc = enc || 'utf8';
-	if (!Buffer.isBuffer(priv)) {
-		priv = new Buffer(priv, enc);
-	}
-	this._priv = new BN(priv);
-};
+
 function returnValue(bn, enc) {
 	var buf = new Buffer(bn.toArray());
 	if (!enc) {
diff --git a/inject.js b/inject.js
index 8d27d8a..6261e38 100644
--- a/inject.js
+++ b/inject.js
@@ -7,12 +7,12 @@ module.exports = function (crypto, exports) {
 	};
 	exports.createDiffieHellman = function (prime, enc) {
 		if (typeof prime === 'number') {
-			return new DH(generatePrime(prime, crypto), crypto);
+			return new DH(generatePrime(prime, crypto), crypto, true);
 		}
 		enc = enc || 'utf8';
 		if (!Buffer.isBuffer(prime)) {
 			prime = new Buffer(prime, enc);
 		}
-		return new DH(prime, crypto);
+		return new DH(prime, crypto, true);
 	};
 };
\ No newline at end of file
diff --git a/test.js b/test.js
index faa2336..d8f0fef 100644
--- a/test.js
+++ b/test.js
@@ -12,11 +12,13 @@ var mods = [
 function run(i) {
 	mods.forEach(function (mod) {
 		test(mod + ' run ' + i, function (t){
-			t.plan(3);
+			t.plan(5);
 			var dh1 = nodeCrypto.getDiffieHellman(mod);
 			var p1 = dh1.getPrime().toString('hex');
 			dh1.generateKeys();
 			var dh2 = myCrypto.getDiffieHellman(mod);
+			t.equals(typeof dh1.setPublicKey, typeof dh2.setPublicKey, 'same methods');
+			t.equals(typeof dh1.setPrivateKey, typeof dh2.setPrivateKey, 'same methods');
 			var p2 = dh2.getPrime().toString('hex');
 			dh2.generateKeys();
 			t.equals(p1, p2, 'equal primes');
@@ -34,12 +36,14 @@ function run(i) {
 function bylen(t) {
 	return function (len){
 		t.test('' + len, function (t) {
-			t.plan(3);
+			t.plan(5);
 			var dh2 = myCrypto.createDiffieHellman(len);
 			var prime2 = dh2.getPrime();
 			var p2 = prime2.toString('hex');
 			var dh1 = nodeCrypto.createDiffieHellman(prime2);
 			var p1 = dh1.getPrime().toString('hex');
+			t.equals(typeof dh1.setPublicKey, typeof dh2.setPublicKey, 'same methods');
+			t.equals(typeof dh1.setPrivateKey, typeof dh2.setPrivateKey, 'same methods');
 			dh1.generateKeys();
 			dh2.generateKeys();
 			t.equals(p1, p2, 'equal primes');
@@ -55,7 +59,7 @@ function bylen(t) {
 function bylen2(t) {
 	return function (len){
 		t.test('' + len, function (t) {
-			t.plan(3);
+			t.plan(5);
 			var dh2 = nodeCrypto.createDiffieHellman(len);
 			var prime2 = dh2.getPrime();
 			var p2 = prime2.toString('hex');
@@ -63,6 +67,8 @@ function bylen2(t) {
 			var p1 = dh1.getPrime().toString('hex');
 			dh1.generateKeys();
 			dh2.generateKeys();
+			t.equals(typeof dh1.setPublicKey, typeof dh2.setPublicKey, 'same methods');
+			t.equals(typeof dh1.setPrivateKey, typeof dh2.setPrivateKey, 'same methods');
 			t.equals(p1, p2, 'equal primes');
 			var pubk1 = dh1.getPublicKey();
 			var pubk2 = dh2.getPublicKey();

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