crypto-keygen-suite
Version:
Key generation utilities for cryptographic operations. YES I RENAMED IT. SIX STATE PROTOCOL!!! See its folder for all <3
91 lines (77 loc) ⢠2.72 kB
JavaScript
import pkg from 'sphincs';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
import fs from 'fs';
const Sphincs = pkg.Sphincs || pkg.default?.Sphincs || pkg;
async function generateSphincsKeys(securityLevel, hash, format, saveToFile) {
if (!Sphincs || typeof Sphincs.keyPair !== 'function') {
throw new Error("Sphincs module missing `keyPair()` method.");
}
const { publicKey, privateKey } = await Sphincs.keyPair(securityLevel, hash);
let pubKeyFormatted, privKeyFormatted;
switch (format) {
case 'hex':
pubKeyFormatted = Buffer.from(publicKey).toString('hex');
privKeyFormatted = Buffer.from(privateKey).toString('hex');
break;
case 'base64':
pubKeyFormatted = Buffer.from(publicKey).toString('base64');
privKeyFormatted = Buffer.from(privateKey).toString('base64');
break;
case 'raw':
default:
pubKeyFormatted = publicKey;
privKeyFormatted = privateKey;
break;
}
console.log(`\nā
SuperSPHINCS+ Keys Generated`);
console.log(`Security Level: ${securityLevel}`);
console.log(`Hash: ${hash}`);
console.log(`Format: ${format}\n`);
console.log(`Public Key (${format}):`);
console.log(pubKeyFormatted);
console.log(`\nPrivate Key (${format}):`);
console.log(privKeyFormatted);
if (saveToFile) {
const pubFilename = `supersphincs_pubkey.${format}`;
const privFilename = `supersphincs_privkey.${format}`;
fs.writeFileSync(pubFilename, pubKeyFormatted instanceof Uint8Array ? Buffer.from(pubKeyFormatted) : pubKeyFormatted);
fs.writeFileSync(privFilename, privKeyFormatted instanceof Uint8Array ? Buffer.from(privKeyFormatted) : privKeyFormatted);
console.log(`\nš Keys saved to files: ${pubFilename}, ${privFilename}`);
}
}
async function main() {
const argv = yargs(hideBin(process.argv))
.option('securityLevel', {
alias: 's',
choices: ['fast', 'small', 'robust'],
default: 'fast',
describe: 'SPHINCS+ security mode'
})
.option('hash', {
alias: 'h',
choices: ['SHA256', 'SHAKE256'],
default: 'SHA256',
describe: 'Hash function'
})
.option('format', {
alias: 'f',
choices: ['raw', 'hex', 'base64'],
default: 'raw',
describe: 'Key output format'
})
.option('save', {
alias: 'p',
type: 'boolean',
default: false,
describe: 'Save keys to files'
})
.help()
.argv;
try {
await generateSphincsKeys(argv.securityLevel, argv.hash, argv.format, argv.save);
} catch (err) {
console.error('ā Error:', err.message);
}
}
main();