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

79 lines (72 loc) 2.62 kB
import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import crypto from 'crypto'; // Function to derive a key using PBKDF2, HKDF, or scrypt async function deriveKey(secret, salt, algorithm, length, iterations) { let derivedKey; switch (algorithm) { case 'pbkdf2': derivedKey = crypto.pbkdf2Sync(secret, salt, iterations, length, 'sha256'); break; case 'hkdf': derivedKey = crypto.hkdfSync('sha256', secret, salt, '', length); break; case 'scrypt': derivedKey = await new Promise((resolve, reject) => { crypto.scrypt(secret, salt, length, (err, key) => { if (err) reject(err); resolve(key); }); }); break; default: throw new Error(`Unsupported KDF algorithm: ${algorithm}`); } return derivedKey.toString('hex'); // Default output format is HEX } // CLI Setup async function main() { const argv = yargs(hideBin(process.argv)) .option('secret', { alias: 's', type: 'string', demandOption: true, describe: 'Secret key or password to derive from', }) .option('salt', { alias: 't', type: 'string', default: crypto.randomBytes(16).toString('hex'), describe: 'Salt for key derivation (default: random)', }) .option('algorithm', { alias: 'a', type: 'string', choices: ['pbkdf2', 'hkdf', 'scrypt'], default: 'pbkdf2', describe: 'Select KDF algorithm', }) .option('length', { alias: 'l', type: 'number', default: 32, describe: 'Length of derived key (default: 32 bytes)', }) .option('iterations', { alias: 'i', type: 'number', default: 100000, describe: 'Number of iterations for PBKDF2 (ignored for HKDF & scrypt)', }) .help() .argv; try { const derivedKey = await deriveKey(argv.secret, argv.salt, argv.algorithm, argv.length, argv.iterations); console.log(`✅ Derived Key (${argv.algorithm.toUpperCase()}): ${derivedKey}`); console.log(`🔹 Salt Used: ${argv.salt}`); } catch (error) { console.error(`❌ Key Derivation Failed: ${error.message}`); } } console.log("🚀 Debug: Running main function..."); main();