UNPKG

keygentoolshed

Version:

Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3

71 lines (60 loc) 2.56 kB
import { program } from 'commander'; import crypto from 'crypto'; import fs from 'fs'; // Function to generate a random key function generateRandomKey(length) { const bytes = length / 8; // Convert bits to bytes return crypto.randomBytes(bytes); } // Function to save the key to a file function saveKeyToFile(key, filePath) { fs.writeFileSync(filePath, key.toString('hex'), 'utf8'); console.log(`Key saved to ${filePath}`); } // Function to derive a subkey from the primary key function deriveSubkey(primaryKey, subkeyIndex) { const hash = crypto.createHash('sha256'); hash.update(primaryKey); hash.update(Buffer.from(subkeyIndex.toString())); return hash.digest(); } // Main function to handle key generation function generateKey(mode, length, subkey, saveFile, filePath) { const validModes = ['onefish', 'twofish', 'threefish']; if (!validModes.includes(mode)) { console.error('Invalid mode. Please choose one of: onefish, twofish, threefish.'); process.exit(1); } if (![128, 192, 256].includes(length)) { console.error('Invalid key length. Please choose 128, 192, or 256 bits.'); process.exit(1); } const key = generateRandomKey(length); console.log(`Generated ${mode} Key:`, key.toString('hex')); if (subkey) { const subkeys = []; const numberOfSubkeys = 5; // Example: Generate 5 subkeys for (let i = 1; i <= numberOfSubkeys; i++) { const subkey = deriveSubkey(key, i); subkeys.push(subkey.toString('hex')); console.log(`Derived Subkey (Index ${i}):`, subkey.toString('hex')); } } if (saveFile) { if (!filePath) { console.error('File path must be specified when saveFile is true.'); process.exit(1); } saveKeyToFile(key, filePath); } } // Command-line argument parsing program .option('--mode <mode>', 'Encryption mode (onefish, twofish, or threefish)') .option('--length <number>', 'Key length (128, 192, or 256 bits)', parseInt) .option('--subkey <boolean>', 'Generate subkeys (true/false)', (value) => value === 'true') .option('--saveFile <boolean>', 'Save key to file (true/false)', (value) => value === 'true') .option('--filePath <path>', 'File path to save the key'); program.parse(process.argv); const options = program.opts(); generateKey(options.mode, options.length, options.subkey, options.saveFile, options.filePath);