From: Justin Wind Date: Sat, 26 Jul 2025 20:06:32 +0000 (-0700) Subject: include simple encoded secret generator X-Git-Tag: v2.2.0~3 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=6d38391c38b9c1c750290ece5a02a16ba339ea76;p=squeep-mystery-box include simple encoded secret generator --- diff --git a/bin/create-secret.js b/bin/create-secret.js new file mode 100755 index 0000000..f1fac50 --- /dev/null +++ b/bin/create-secret.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node +'use strict'; + +/** + * Simple script to create a secret key for mysterybox configuration. + */ + +const { randomBytes } = require('node:crypto'); + +/** + * Get the value following a command line option. + * Removes the option and its value from the command line arguments. + * @param {string} option e.g. '--bytes' + * @returns {string} value following the option + */ +function cliGetOption(option) { + let value; + while (process.argv.includes(option)) { + const optionIndex = process.argv.indexOf(option); + value = process.argv.splice(optionIndex, 2)[1]; + if (value === undefined) { + throw new Error(`Missing value for option: ${option}`); + } + } + return value; +} + +const bytes = parseInt(cliGetOption('--bytes'), 10) || 64; +const encoding = cliGetOption('--encoding') || 'base64url'; + +randomBytes(bytes, (err, buf) => { + if (err) { + console.error(err); + throw err; + } + const secret = buf.toString(encoding); + console.log(`Buffer.from('${secret}', '${encoding}')`); +}); diff --git a/package.json b/package.json index 7f8a367..f565399 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,12 @@ "prepare": "husky" }, "files": [ - "lib/**" + "lib/**", + "bin/**" ], + "bin": { + "mystery-box-create-secret": "bin/create-secret.js" + }, "engines": { "node": "^14 >=14.18.0 || >=15.7.0" },