keygentoolshed
Version:
Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3
65 lines (55 loc) • 1.98 kB
JavaScript
import fs from 'fs';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv))
.usage('Usage: $0 --expiration [time] --key [key]')
.option('expiration', {
alias: 'ex',
describe: 'Specify the expiration time (e.g., 1h, 30m)',
type: 'string',
demandOption: true
})
.option('key', {
alias: 'k',
describe: 'Specify the key to add expiration to',
type: 'string',
demandOption: true
})
.help()
.argv;
function parseExpirationTime(expiration) {
const regex = /(\d+)([d|h|m])/;
const match = expiration.match(regex);
if (!match) {
throw new Error('Invalid expiration format. Use "1h" for 1 hour or "30m" for 30 minutes.');
}
const value = parseInt(match[1], 10);
const unit = match[2];
if (unit === 'd') {
return value * 60 * 60 * 60 * 1000;
} else if (unit === 'h') {
return value * 60 * 60 * 1000;
} else if (unit === 'm') {
return value * 60 * 1000;
} else {
throw new Error('Invalid time unit. Use "d" for days, use "h" for hours or "m" for minutes.');
}
}
function addKeyExpiration(expiration, key) {
const expirationInMs = parseExpirationTime(expiration);
const expirationTime = Date.now() + expirationInMs; // Calculate expiration time
const expirationDate = new Date(expirationTime).toISOString();
const content = `Key: ${key}\nExpiration date: ${expirationDate}\n\n`;
fs.appendFile('keyExpirations', content, (err) => {
if (err) {
console.error(`Error writing to file: ${err.message}`);
} else {
console.log(`Successfully added expiration for key: ${key}, expires at: ${expirationDate}`);
}
});
}
function main() {
const { expiration, key } = argv;
addKeyExpiration(expiration, key);
}
main();