crypto-keygen-suite
Version:
Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3
26 lines (19 loc) ⢠1.11 kB
JavaScript
import crypto from 'crypto';
const args = process.argv.slice(2);
const bitsArgIndex = args.indexOf('--bits');
const bits = (bitsArgIndex !== -1 && args[bitsArgIndex + 1]) ? parseInt(args[bitsArgIndex + 1]) : 2048;
const alice = crypto.createDiffieHellman(bits);
const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());
const alicePublicKey = alice.generateKeys();
const bobPublicKey = bob.generateKeys();
const aliceSecret = alice.computeSecret(bobPublicKey);
const bobSecret = bob.computeSecret(alicePublicKey);
const toHex = (buffer) => buffer.toString('hex').match(/.{1,2}/g).join(':');
console.log('š Diffie-Hellman Key Exchange');
console.log('----------------------------------------');
console.log(`Bits: ${bits}`);
console.log('\nAlice\'s Public Key:\n', toHex(alice.getPublicKey()));
console.log('\nBob\'s Public Key:\n', toHex(bob.getPublicKey()));
console.log('\nAlice\'s Shared Secret:\n', toHex(aliceSecret));
console.log('\nBob\'s Shared Secret:\n', toHex(bobSecret));
console.log('\nā
Secrets Match:', aliceSecret.equals(bobSecret));