-
-
Save TemaSM/1a43d620388d3fe1e66d683e325e8844 to your computer and use it in GitHub Desktop.
[nodejs][javascript] Diffie-Hellman key exchange by nodejs-0.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node.js 0.5 Diffie-Hellman example | |
var assert = require("assert"); | |
var crypto = require("crypto"); | |
// the prime is shared by everyone | |
var server = crypto.createDiffieHellman(512); | |
var prime = server.getPrime(); | |
// sharing secret key on a pair | |
var alice = crypto.createDiffieHellman(prime); | |
alice.generateKeys(); | |
var alicePub = alice.getPublicKey(); | |
var bob = crypto.createDiffieHellman(prime); | |
bob.generateKeys(); | |
var bobPub = bob.getPublicKey(); | |
var bobAliceSecret = bob.computeSecret(alicePub); | |
var aliceBobSecret = alice.computeSecret(bobPub); | |
// public keys are different, but secret is common. | |
assert.notEqual(bobPub, alicePub); | |
assert.equal(bobAliceSecret, aliceBobSecret); | |
// use common secret as shared encryption key... | |
// shared secret with 3rd person | |
var carol = crypto.createDiffieHellman(prime); | |
carol.generateKeys(); | |
var carolPub = carol.getPublicKey(); | |
var carolAliceSecret = carol.computeSecret(alicePub); | |
var aliceCarolSecret = alice.computeSecret(carolPub); | |
assert.notEqual(carolPub, alicePub); | |
assert.equal(carolAliceSecret, aliceCarolSecret); | |
// secret depends on pairs | |
assert.notEqual(aliceBobSecret, aliceCarolSecret); | |
var carolBobSecret = carol.computeSecret(bobPub); | |
assert.notEqual(carolAliceSecret, carolBobSecret); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment