UNPKG

keygentoolshed

Version:

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

51 lines (42 loc) 1.83 kB
import { program } from 'commander'; import crypto from 'crypto'; // Function to generate a random polynomial of degree t-1 function generatePolynomial(secret, threshold) { const coefficients = [secret]; for (let i = 1; i < threshold; i++) { coefficients.push(crypto.randomInt(0, 256)); // Random coefficients } return coefficients; } // Function to evaluate the polynomial at a given x function evaluatePolynomial(coefficients, x) { return coefficients.reduce((acc, coeff, index) => { return (acc + coeff * Math.pow(x, index)) % 256; // Modulo 256 for simplicity }, 0); } // Function to generate shares function generateShares(secret, totalShares, threshold) { const coefficients = generatePolynomial(secret, threshold); const shares = []; for (let i = 1; i <= totalShares; i++) { const share = evaluatePolynomial(coefficients, i); shares.push({ share: share, index: i }); } return shares; } // Main function to handle command-line arguments function main() { program .option('--secret <number>', 'The secret to share (0-255)', parseInt) .option('--totalShares <number>', 'Total number of shares to generate', parseInt) .option('--threshold <number>', 'Threshold number of shares required to reconstruct the secret', parseInt); program.parse(process.argv); const options = program.opts(); if (options.secret === undefined || options.totalShares === undefined || options.threshold === undefined) { console.error('Please provide secret, totalShares, and threshold options.'); process.exit(1); } const shares = generateShares(options.secret, options.totalShares, options.threshold); console.log('Generated Shares:', shares); } main();