crypto-keygen-suite
Version:
Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3
50 lines (39 loc) • 1.62 kB
JavaScript
import { program } from 'commander';
import crypto from 'crypto';
import fs from 'fs';
function generateX25519KeyPair() {
return crypto.generateKeyPairSync('x25519');
}
function saveKeyToFile(keyBuffer, filePath) {
fs.writeFileSync(filePath, keyBuffer.toString('hex'), 'utf8');
console.log(`Saved key to ${filePath}`);
}
function main(savePriv, privPath, savePub, pubPath) {
const { privateKey, publicKey } = generateX25519KeyPair();
const privHex = privateKey.export({ type: 'pkcs8', format: 'der' }).toString('hex');
const pubHex = publicKey.export({ type: 'spki', format: 'der' }).toString('hex');
console.log('Private Key (PKCS8 DER hex):', privHex);
console.log('Public Key (SPKI DER hex):', pubHex);
if (savePriv) {
if (!privPath) {
console.error('Please specify --privPath to save private key');
process.exit(1);
}
saveKeyToFile(Buffer.from(privHex, 'hex'), privPath);
}
if (savePub) {
if (!pubPath) {
console.error('Please specify --pubPath to save public key');
process.exit(1);
}
saveKeyToFile(Buffer.from(pubHex, 'hex'), pubPath);
}
}
program
.option('--savePriv <boolean>', 'Save private key to file (true/false)', val => val === 'true')
.option('--privPath <path>', 'File path for private key')
.option('--savePub <boolean>', 'Save public key to file (true/false)', val => val === 'true')
.option('--pubPath <path>', 'File path for public key');
program.parse(process.argv);
const opts = program.opts();
main(opts.savePriv, opts.privPath, opts.savePub, opts.pubPath);