UNPKG

crypto-keygen-suite

Version:

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

77 lines (66 loc) 2.08 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import readline from 'readline'; import crypto from 'crypto'; function generateTOTP(secret, window = 30, digits = 6) { const key = Buffer.from(secret, 'hex'); const epoch = Math.floor(Date.now() / 1000); const counter = Math.floor(epoch / window); const buf = Buffer.alloc(8); buf.writeBigUInt64BE(BigInt(counter)); const hmac = crypto.createHmac('sha1', key); hmac.update(buf); const digest = hmac.digest(); const offset = digest[digest.length - 1] & 0xf; const code = ((digest[offset] & 0x7f) << 24) | ((digest[offset + 1] & 0xff) << 16) | ((digest[offset + 2] & 0xff) << 8) | (digest[offset + 3] & 0xff); const otp = (code % 10 ** digits).toString().padStart(digits, '0'); return otp; } async function prompt(question) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); return new Promise(resolve => { rl.question(question, answer => { rl.close(); resolve(answer.trim().toLowerCase()); }); }); } async function main() { const argv = yargs(hideBin(process.argv)) .option('secret', { alias: 'k', type: 'string', description: 'Hex encoded secret key', demandOption: true, }) .option('window', { alias: 'w', type: 'number', default: 30, description: 'TOTP time step window in seconds', }) .option('digits', { alias: 'd', type: 'number', default: 6, description: 'Digits in OTP code', }) .argv; console.log('🚀 Generating push OTP...'); const otp = generateTOTP(argv.secret, argv.window, argv.digits); console.log(`📩 Push OTP code generated: ${otp}`); const answer = await prompt('Approve push? (yes/no): '); if (answer === 'yes' || answer === 'y') { console.log('✅ Push approved! Access granted.'); } else { console.log('❌ Push denied! Access rejected.'); } } main();