[Pkg-javascript-commits] [node-diffie-hellman] 01/88: first

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:19:11 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 923fdc540603443b7da981094c8906f722ce1ef8
Author: Calvin Metcalf <calvin.metcalf at state.ma.us>
Date:   Sun Nov 2 17:51:59 2014 -0500

    first
---
 dh.js        | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 index.js     |  3 +++
 inject.js    | 16 ++++++++++++
 package.json | 25 ++++++++++++++++++
 primes.json  | 34 ++++++++++++++++++++++++
 readme.md    |  4 +++
 6 files changed, 166 insertions(+)

diff --git a/dh.js b/dh.js
new file mode 100644
index 0000000..4b8614f
--- /dev/null
+++ b/dh.js
@@ -0,0 +1,84 @@
+
+var BN = require('bn.js');
+
+dh.generateKeys();
+
+var priv = new BN(dh.getPrivateKey().toString('hex'), 16);
+console.log('priv', priv);
+var gen = new BN(dh.getGenerator().toString('hex'), 16);
+console.log('gen', gen);
+var prime = BN.mont(new BN(dh.getPrime().toString('hex'), 16));
+console.log('prime', prime);
+var publ = new BN(dh.getPublicKey().toString('hex'), 16);
+console.log('publ', publ);
+var calcpubl = gen.toRed(prime).redPow(priv).fromRed()
+console.log('compare', publ.toString(16) === calcpubl.toString(16));
+
+function DH(prime, crypto) {
+	this.setGenerator(new Buffer([2]));
+	this.__prime = new BN(prime.toString('hex'), 16);
+	this._prime = BN.mont(this.__prime);
+	this._pub = void 0;
+	this._priv = void 0;
+	this._makeNum = function makeNum() {
+		return crypto.randomBytes(192);
+	};
+}
+DH.prototype.generateKeys = function () {
+	this.setPrivateKey(this._makeNum());
+	this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed();
+	return this.getPublicKey();
+};
+
+DH.prototype.computeSecret = function (other) {
+	other = new BN(other.toString('hex'), 16);
+	other = other.toRed(this._prime);
+	var secret = other.redPow(this._priv).fromRed();
+	return new Buffer(secret.toString('hex'), 'hex');
+}
+DH.prototype.getPublicKey = function () {
+	return returnValue(this._pub, enc);
+};
+DH.prototype.getPrivateKey = function () {
+	return returnValue(this._priv, enc);
+};
+
+DH.prototype.getPrime = function (enc) {
+	return returnValue(this.__prime, enc);
+};
+DH.prototype.getGenerator = function (enc) {
+	return returnValue(this._gen, enc);
+};
+DH.prototype.setGenerator = function (gen, enc) {
+	enc = enc || 'utf8';
+	if (!Buffer.isBuffer(gen)) {
+		gen = new Buffer(gen, enc);
+	}
+	this._gen = new BH(gen.toString('hex'), 16);
+}
+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);
+}
+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);
+}
+function returnValue(bn, enc) {
+	var hex = bn.toString(16);
+	if (enc === 'hex') {
+		return hex;
+	}
+	var buf = new Buffer(hex, 'hex');
+	if (!enc) {
+		return buf;
+	} else {
+		return buf.toString(end);
+	}
+}
\ No newline at end of file
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..17326b3
--- /dev/null
+++ b/index.js
@@ -0,0 +1,3 @@
+exports.__browserify = require('inject');
+
+exports.__browserify(require('crypto'), exports);
\ No newline at end of file
diff --git a/inject.js b/inject.js
new file mode 100644
index 0000000..ebda2a3
--- /dev/null
+++ b/inject.js
@@ -0,0 +1,16 @@
+var primes = require('./primes.json');
+module.exports = function (crypto, exports) {
+	exports.getDiffieHellman = function (mod) {
+		return new DH(primes[mod], crypto);
+	}
+	exports.createDiffieHellman = function (prime, enc) {
+		if (typeof prime === 'number') {
+			throw new Error('generating primes not implimented');
+		}
+		enc = enc || 'utf8';
+		if (!Buffer.isBuffer(prime)) {
+			prime = new Buffer(prime, enc);
+		}
+		return new DH(prime, crypto);
+	}
+}
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..aac7286
--- /dev/null
+++ b/package.json
@@ -0,0 +1,25 @@
+{
+  "name": "diffie-hellman",
+  "version": "1.0.0",
+  "description": "pure js diffie-hellman",
+  "main": "index.js",
+  "scripts": {
+    "test": "node test.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/calvinmetcalf/diffie-hellman.git"
+  },
+  "keywords": [
+    "diffie",
+    "hellman",
+    "diffiehellman",
+    "dh"
+  ],
+  "author": "Calvin Metcalf",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/calvinmetcalf/diffie-hellman/issues"
+  },
+  "homepage": "https://github.com/calvinmetcalf/diffie-hellman"
+}
diff --git a/primes.json b/primes.json
new file mode 100644
index 0000000..9fef6c9
--- /dev/null
+++ b/primes.json
@@ -0,0 +1,34 @@
+{
+    "modp1": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"
+    },
+    "modp2": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff"
+    },
+    "modp5": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff"
+    },
+    "modp14": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898 [...]
+    },
+    "modp15": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898 [...]
+    },
+    "modp16": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898 [...]
+    },
+    "modp17": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898 [...]
+    },
+    "modp18": {
+        "gen": "02",
+        "prime": "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898 [...]
+    }
+}
\ No newline at end of file
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..2909dfa
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,4 @@
+diffie hellman
+====
+
+pure js diffie-hellman
\ 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