UNPKG

crypto-keygen-suite

Version:

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

92 lines (74 loc) 2.89 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import fs from 'fs'; class COW { constructor(keyLength = 128) { this.keyLength = keyLength; } generateKey() { return Array.from({ length: this.keyLength }, () => Math.random() > 0.5 ? 1 : 0); } generateBases() { return Array.from({ length: this.keyLength }, () => Math.random() > 0.5 ? 1 : 0); } encodeQubits(key, bases) { return key.map((bit, i) => (bit + bases[i]) % 2); } measureQubits(qubits, bases) { return qubits.map((bit, i) => (bit + bases[i]) % 2); } siftKey(aliceBases, bobBases, key) { return key.filter((bit, 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 runCOWQKD(keyLength, format, secure) { const cow = new COW(keyLength); const aliceKey = cow.generateKey(); const aliceBases = cow.generateBases(); const encodedQubits = cow.encodeQubits(aliceKey, aliceBases); const bobBases = cow.generateBases(); const bobKey = cow.measureQubits(encodedQubits, bobBases); const siftedKey = cow.siftKey(aliceBases, bobBases, bobKey); const formattedSiftedKey = formatOutput(siftedKey, format); console.log(`✅ COW-QKD Simulation Completed!`); console.log(`🔑 Alice's original key: ${formatOutput(aliceKey, format)}`); console.log(`📡 Alice's bases: ${formatOutput(aliceBases, format)}`); console.log(`📡 Bob's bases: ${formatOutput(bobBases, format)}`); console.log(`📡 Bob's measured key: ${formatOutput(bobKey, format)}`); console.log(`💎 Sifted key: ${formattedSiftedKey}`); // Secure key storage if (secure) { console.log("🔒 Secure Mode Enabled: Saving keys to files."); fs.writeFileSync(`cowqkd_sifted_key.${format}`, formattedSiftedKey); } return { publicKey: formatOutput(aliceKey, format), privateKey: formattedSiftedKey }; } 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; runCOWQKD(argv.length, argv.format, argv.secure); } console.log("🚀 Debug: Running main function..."); main();