UNPKG

crypto-keygen-suite

Version:

Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3

90 lines (80 loc) 2.9 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import crypto from 'crypto'; class PAKE { constructor(password) { this.password = password; this.salt = crypto.randomBytes(16); this.generator = BigInt(2); this.prime = BigInt( '0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + 'E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF' ); } deriveVerifier() { const hash = crypto.createHash('sha256').update(this.password + this.salt.toString('hex')).digest(); return BigInt(`0x${hash.toString('hex')}`) % this.prime; } modExp(base, exp, mod) { let result = BigInt(1); base = base % mod; while (exp > 0) { if (exp % BigInt(2) === BigInt(1)) { result = (result * base) % mod; } exp = exp >> BigInt(1); base = (base * base) % mod; } return result; } generatePublicKey() { const verifier = this.deriveVerifier(); return this.modExp(this.generator, verifier, this.prime); } computeSharedSecret(peerPublicKey) { const verifier = this.deriveVerifier(); return this.modExp(BigInt(peerPublicKey), verifier, this.prime); } } // CLI Setup async function main() { const argv = yargs(hideBin(process.argv)) .option('mode', { alias: 'm', type: 'string', choices: ['keygen', 'exchange'], describe: 'Generate keys or perform key exchange', }) .option('password', { alias: 'p', type: 'string', demandOption: true, describe: 'Shared password for authentication', }) .option('peerKey', { alias: 'k', type: 'string', describe: 'Peer public key for key exchange', }) .help() .argv; const pake = new PAKE(argv.password); if (argv.mode === 'keygen') { const publicKey = pake.generatePublicKey(); console.log(`✅ Generated PAKE Key Pair`); console.log(`📡 Public Key: ${publicKey.toString(16)}`); } else if (argv.mode === 'exchange') { if (!argv.peerKey) { console.error("❌ Key exchange requires a peer public key"); return; } const sharedSecret = pake.computeSharedSecret(BigInt(`0x${argv.peerKey}`)); console.log(`🔐 Shared Secret: ${sharedSecret.toString(16)}`); } else { console.error("❌ Invalid mode. Use --mode keygen | exchange"); } } console.log("🚀 Debug: Running main function..."); main();