diffie-hellman
Advanced tools
Comparing version 1.1.1 to 1.1.2
42
dh.js
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])); | ||
@@ -11,2 +24,6 @@ this.__prime = new BN(prime); | ||
this._priv = void 0; | ||
if (malleable) { | ||
this.setPublicKey = setPublicKey; | ||
this.setPrivateKey = setPrivateKey; | ||
} | ||
this._makeNum = function makeNum() { | ||
@@ -18,3 +35,3 @@ return crypto.randomBytes(192); | ||
if (!this._priv) { | ||
this.setPrivateKey(this._makeNum()); | ||
this._priv = new BN(this._makeNum()); | ||
} | ||
@@ -38,6 +55,6 @@ this._pub = this._gen.toRed(this._prime).redPow(this._priv).fromRed(); | ||
}; | ||
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); | ||
@@ -59,16 +76,3 @@ }; | ||
}; | ||
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) { | ||
@@ -75,0 +79,0 @@ var buf = new Buffer(bn.toArray()); |
@@ -10,3 +10,3 @@ var primes = require('./primes.json'); | ||
if (typeof prime === 'number') { | ||
return new DH(generatePrime(prime, crypto), crypto); | ||
return new DH(generatePrime(prime, crypto), crypto, true); | ||
} | ||
@@ -17,4 +17,4 @@ enc = enc || 'utf8'; | ||
} | ||
return new DH(prime, crypto); | ||
return new DH(prime, crypto, true); | ||
}; | ||
}; |
{ | ||
"name": "diffie-hellman", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "pure js diffie-hellman", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
12
test.js
@@ -15,3 +15,3 @@ var test = require('tape'); | ||
test(mod + ' run ' + i, function (t){ | ||
t.plan(3); | ||
t.plan(5); | ||
var dh1 = nodeCrypto.getDiffieHellman(mod); | ||
@@ -21,2 +21,4 @@ var p1 = dh1.getPrime().toString('hex'); | ||
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'); | ||
@@ -39,3 +41,3 @@ dh2.generateKeys(); | ||
t.test('' + len, function (t) { | ||
t.plan(3); | ||
t.plan(5); | ||
var dh2 = myCrypto.createDiffieHellman(len); | ||
@@ -46,2 +48,4 @@ var prime2 = dh2.getPrime(); | ||
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(); | ||
@@ -62,3 +66,3 @@ dh2.generateKeys(); | ||
t.test('' + len, function (t) { | ||
t.plan(3); | ||
t.plan(5); | ||
var dh2 = nodeCrypto.createDiffieHellman(len); | ||
@@ -71,2 +75,4 @@ var prime2 = dh2.getPrime(); | ||
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'); | ||
@@ -73,0 +79,0 @@ var pubk1 = dh1.getPublicKey(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17910
370