UNPKG

crypto-keygen-suite

Version:

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

98 lines (84 loc) 3.06 kB
import { program } from 'commander'; import crypto from 'crypto'; import fs from 'fs'; function generateRandomKey(lengthBits) { const lengthBytes = lengthBits / 8; return crypto.randomBytes(lengthBytes); } function formatKey(keyBuffer, format) { if (format === 'raw') { return keyBuffer; } if (format === 'base64') { return keyBuffer.toString('base64'); } // default to hex return keyBuffer.toString('hex'); } function saveKeyAndSubkeysToFile(key, subkeys, filePath, format) { if (format === 'raw') { // For raw binary, write the key followed by all subkeys concatenated const buffers = [key, ...subkeys]; const combined = Buffer.concat(buffers); fs.writeFileSync(filePath, combined); console.log(`Key and subkeys saved in raw binary to ${filePath}`); } else { // For hex or base64, write formatted text let content = `Key: ${formatKey(key, format)}\n\n`; subkeys.forEach((subkey, i) => { content += `Derived Subkey #${i + 1}: ${formatKey(subkey, format)}\n\n`; }); fs.writeFileSync(filePath, content.trim(), 'utf8'); console.log(`Key and subkeys saved to ${filePath} as ${format}`); } } function deriveSubkey(primaryKey, index) { const hash = crypto.createHash('sha256'); hash.update(primaryKey); hash.update(Buffer.from(index.toString())); return hash.digest(); } function generateKey(length, generateSubkeys, saveToFile, filePath, format) { const validLengths = [128, 192, 256]; if (!validLengths.includes(length)) { console.error('Invalid key length. Choose one of: 128, 192, or 256 bits.'); process.exit(1); } if (!['raw', 'hex', 'base64'].includes(format)) { console.error('Invalid format. Choose one of: raw, hex, base64.'); process.exit(1); } const key = generateRandomKey(length); console.log(`Generated LES Key (${length} bits):`, formatKey(key, format)); let subkeys = []; if (generateSubkeys) { const numSubkeys = 5; for (let i = 1; i <= numSubkeys; i++) { const subkey = deriveSubkey(key, i); subkeys.push(subkey); console.log(`Derived Subkey #${i}:`, formatKey(subkey, format)); } } if (saveToFile) { if (!filePath) { console.error('You must specify --filePath to save the key.'); process.exit(1); } saveKeyAndSubkeysToFile(key, subkeys, filePath, format); } } program .option('--length <number>', 'Key length in bits (128, 192, or 256)', parseInt) .option('--subkeys <boolean>', 'Generate subkeys (true/false)', val => val === 'true') .option('--saveFile <boolean>', 'Save key to file (true/false)', val => val === 'true') .option('--filePath <path>', 'File path to save the key') .option('--format <type>', 'Output format (raw, hex, base64)', 'hex'); program.parse(process.argv); const options = program.opts(); generateKey( options.length || 128, options.subkeys || false, options.saveFile || false, options.filePath, options.format );