Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Last active August 31, 2016 12:04
Show Gist options
  • Save ikouchiha47/2e1c3973ef356da4df57b59e62a95f97 to your computer and use it in GitHub Desktop.
Save ikouchiha47/2e1c3973ef356da4df57b59e62a95f97 to your computer and use it in GitHub Desktop.
trying dfh in js
const crypto = require('crypto');
const assert = require('assert');
function DeffMan(prime, generator) {
this.user = prime && generator ? crypto.createDiffieHellman(prime, generator) : crypto.createDiffieHellman(120)
this.sharedPrime = this.user.getPrime()
this.generator = this.user.getGenerator()
}
DeffMan.prototype.getPublicKey = function() {
this.user.generateKeys()
return this.user.getPublicKey()
}
DeffMan.prototype.computeSecret = function(public_key) {
return this.user.computeSecret(public_key)
}
let alice = new DeffMan();
let bob = new DeffMan(alice.sharedPrime, alice.generator);
let alice_key = alice.getPublicKey();
let bob_key = bob.getPublicKey();
console.log(alice.computeSecret(bob_key).toString('hex'),
bob.computeSecret(alice_key).toString('hex'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment