crypto-keygen-suite
Version:
Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3
87 lines (73 loc) • 3.17 kB
JavaScript
import pkg from 'kyber-crystals';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
const Kyber = pkg.kyber || pkg.default?.kyber || pkg; // Ensure correct reference
async function generateKyberKeys(length, outputFormat, secure) {
try {
if (!Kyber || typeof Kyber.keyPair !== 'function') {
throw new Error("❌ Kyber module does not expose `keyPair()` correctly. Check the package structure.");
}
console.log("🚀 Debug: Kyber module structure:");
console.log(Kyber);
// Call `keyPair()` directly instead of instantiating Kyber
const { publicKey, privateKey } = await Kyber.keyPair();
console.log(`✅ Kyber-${length} Key Pair Generated`);
// Choose output format
let formattedPubKey, formattedPrivKey;
switch (outputFormat) {
case 'hex':
formattedPubKey = Buffer.from(publicKey).toString('hex');
formattedPrivKey = Buffer.from(privateKey).toString('hex');
break;
case 'base64':
formattedPubKey = Buffer.from(publicKey).toString('base64');
formattedPrivKey = Buffer.from(privateKey).toString('base64');
break;
case 'raw':
default:
formattedPubKey = publicKey;
formattedPrivKey = privateKey;
break;
}
console.log(`Public Key (${outputFormat.toUpperCase()}):`, formattedPubKey);
console.log(`Private Key (${outputFormat.toUpperCase()}):`, formattedPrivKey);
if (secure) {
console.log("🔒 Secure Mode Enabled: Saving keys to files.");
fs.writeFileSync(`kyber_${length}_pubkey.${outputFormat}`, formattedPubKey);
fs.writeFileSync(`kyber_${length}_privkey.${outputFormat}`, formattedPrivKey);
}
return { publicKey: formattedPubKey, privateKey: formattedPrivKey };
} catch (error) {
console.error(`❌ Key Generation Failed: ${error.message}`);
}
}
async function main() {
const argv = yargs(hideBin(process.argv))
.option('length', {
alias: 'l',
type: 'number',
demandOption: true,
describe: 'The length of the key (512, 768, 1024)'
})
.option('format', {
alias: 'f',
type: 'string',
default: 'raw',
choices: ['raw', 'hex', 'base64'],
describe: 'Output format for the keys (raw, hex, base64)'
})
.option('secure', {
alias: 's',
type: 'boolean',
default: false,
describe: 'Save keys as files'
})
.argv;
await generateKyberKeys(argv.length, argv.format, argv.secure);
}
console.log("🚀 Debug: Package Structure:");
console.log(pkg); // Logs entire package content
console.log("Available keys:", Object.keys(pkg)); // Logs all available exported functions
console.log("🚀 Debug: Running main function...");
main();