UNPKG

crypto-keygen-suite

Version:

Key generation utilities for cryptographic operations. YES I RENAMED IT OMG BASES AND PROTOCOLS!!! See its folder for all <3

78 lines (63 loc) 2.51 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; class E91 { constructor(keyLength = 128) { this.keyLength = keyLength; } generateEntangledPairs() { return Array.from({ length: this.keyLength }, () => [Math.random() > 0.5 ? 1 : 0, Math.random() > 0.5 ? 1 : 0]); } generateBases() { return Array.from({ length: this.keyLength }, () => Math.floor(Math.random() * 3)); // Bases {0, 1, 2} } measureQubits(qubits, bases) { return qubits.map((bit, i) => (bit + bases[i]) % 2); } siftKey(aliceBases, bobBases, key) { return key.filter((_, i) => aliceBases[i] === bobBases[i]); } } function formatOutput(data, format) { const bufferData = Buffer.from(data); switch (format) { case 'hex': return bufferData.toString('hex'); case 'base64': return bufferData.toString('base64'); default: return data.join(''); } } function runE91QKD(keyLength, format) { const e91 = new E91(keyLength); const entangledPairs = e91.generateEntangledPairs(); const aliceBases = e91.generateBases(); const bobBases = e91.generateBases(); const aliceKey = e91.measureQubits(entangledPairs.map(p => p[0]), aliceBases); const bobKey = e91.measureQubits(entangledPairs.map(p => p[1]), bobBases); const siftedKey = e91.siftKey(aliceBases, bobBases, aliceKey); console.log(`✅ E91-QKD Simulation Completed!`); console.log(`🔑 Alice's measured key: ${formatOutput(aliceKey, format)}`); console.log(`📡 Alice's bases: ${formatOutput(aliceBases, format)}`); console.log(`📡 Bob's measured key: ${formatOutput(bobKey, format)}`); console.log(`📡 Bob's bases: ${formatOutput(bobBases, format)}`); console.log(`💎 Sifted key: ${formatOutput(siftedKey, format)}`); } async function main() { const argv = yargs(hideBin(process.argv)) .option('length', { alias: 'l', type: 'number', default: 128, describe: 'Length of the key', }) .option('format', { alias: 'f', type: 'string', default: 'raw', choices: ['raw', 'hex', 'base64'], describe: 'Choose output format (raw, hex, base64)', }) .help() .argv; runE91QKD(argv.length, argv.format); } console.log("🚀 Debug: Running main function..."); main();