UNPKG

crypto-keygen-suite

Version:

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

64 lines (55 loc) 1.9 kB
import { authenticator } from 'otplib'; import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import fs from 'fs'; import base32 from 'base32.js'; const { Decoder } = base32; // Function to generate TOTP secret function generateTOTPSecret(outputFormat, secure) { try { const secretBase32 = authenticator.generateSecret(); const decoder = new Decoder(); // RFC4648 by default const decoded = decoder.write(secretBase32).finalize(); let secret; switch (outputFormat) { case 'hex': secret = Buffer.from(decoded).toString('hex'); break; case 'raw': secret = decoded; break; case 'base32': default: secret = secretBase32; break; } console.log(`✅ TOTP Secret (${outputFormat.toUpperCase()}):`, secret); if (secure) { console.log("🔒 Secure Mode Enabled: Saving secret to file."); fs.writeFileSync(`totp_secret.${outputFormat}`, secret.toString()); } return secret; } catch (error) { console.error(`❌ Secret Generation Failed: ${error.message}`); } } async function main() { const argv = yargs(hideBin(process.argv)) .option('format', { alias: 'f', type: 'string', default: 'base32', choices: ['base32', 'hex', 'raw'], describe: 'Output format for the TOTP secret' }) .option('secure', { alias: 's', type: 'boolean', default: false, describe: 'Save secret as file' }) .argv; generateTOTPSecret(argv.format, argv.secure); } console.log("🚀 Debug: Running main function..."); main();