gen-random-cli 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/bin/gen-random.js +194 -0
- package/package.json +21 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gen-random-cli contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# gen-random-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generate cryptographically secure random hexadecimal values locally from your terminal. After installation, the command works offline: it makes no network requests and includes no tracking or telemetry. It prints both the 128-bit and 256-bit formats by default.
|
|
4
|
+
|
|
5
|
+
## Security and privacy
|
|
6
|
+
|
|
7
|
+
Random bytes are generated locally with Node.js `crypto.randomBytes()`, which Node documents as providing cryptographically strong pseudorandom data ([Node.js crypto documentation](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback)). After installation, the generator works offline: it makes no network requests and includes no tracking, telemetry, analytics, or third-party runtime dependencies.
|
|
8
|
+
|
|
9
|
+
Generated values are printed to the terminal and are not written to files by the CLI. The settings file stores only the prefix. The source is small and available for inspection under the MIT license.
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
- Node.js 22 or newer
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install --global gen-random-cli
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Print both values:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
gen-random
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Print one format only with `--bits`:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
gen-random --bits 128
|
|
33
|
+
gen-random --bits 256
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Override the prefix for one run with `--prefix`:
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
gen-random --prefix dev_
|
|
40
|
+
gen-random --bits 256 --prefix "service key_"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The default prefix is `sk_`. A command-line prefix does not change the saved setting.
|
|
44
|
+
|
|
45
|
+
Open the interactive settings menu to change the saved prefix:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
gen-random settings
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Type printable ASCII characters to add to the prefix, use Backspace to remove characters, press Enter to save, or Esc to cancel. Settings are stored in `~/.config/gen-random/settings.json`.
|
|
52
|
+
|
|
53
|
+
Show all options:
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
gen-random --help
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Output formats
|
|
60
|
+
|
|
61
|
+
The total character count includes the prefix. With the default `sk_` prefix, the output is:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
128 bits (32 chars): sk_<29 lowercase hex characters>
|
|
65
|
+
256 bits (64 chars): sk_<61 lowercase hex characters>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The prefix is fixed text and adds no randomness. With `sk_`, the random portions contain 116 and 244 bits respectively. If a custom prefix is too long to fit, the output grows to retain at least 112 random bits for the 128-bit format and 128 random bits for the 256-bit format.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const crypto = require('node:crypto');
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const os = require('node:os');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const readline = require('node:readline');
|
|
9
|
+
|
|
10
|
+
const DEFAULT_SETTINGS = {
|
|
11
|
+
prefix: 'sk_',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const OUTPUT_SIZES = [
|
|
15
|
+
{ targetBits: 128, totalChars: 32, minRandomHexLength: 28 },
|
|
16
|
+
{ targetBits: 256, totalChars: 64, minRandomHexLength: 32 },
|
|
17
|
+
];
|
|
18
|
+
const settingsPath = path.join(os.homedir(), '.config', 'gen-random', 'settings.json');
|
|
19
|
+
|
|
20
|
+
function isValidSettings(settings) {
|
|
21
|
+
return typeof settings?.prefix === 'string'
|
|
22
|
+
&& /^[\x20-\x7E]*$/.test(settings.prefix);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function loadSettings() {
|
|
26
|
+
try {
|
|
27
|
+
const saved = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
28
|
+
const settings = {
|
|
29
|
+
prefix: saved?.prefix ?? DEFAULT_SETTINGS.prefix,
|
|
30
|
+
};
|
|
31
|
+
if (!isValidSettings(settings)) {
|
|
32
|
+
return { ...DEFAULT_SETTINGS };
|
|
33
|
+
}
|
|
34
|
+
return settings;
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error.code === 'ENOENT' || error instanceof SyntaxError) {
|
|
37
|
+
return { ...DEFAULT_SETTINGS };
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function saveSettings(settings) {
|
|
44
|
+
fs.mkdirSync(path.dirname(settingsPath), { recursive: true, mode: 0o700 });
|
|
45
|
+
const temporaryPath = `${settingsPath}.tmp-${process.pid}`;
|
|
46
|
+
fs.writeFileSync(temporaryPath, `${JSON.stringify(settings, null, 2)}\n`, { mode: 0o600 });
|
|
47
|
+
fs.renameSync(temporaryPath, settingsPath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function drawSettingsMenu(settings, notice = '') {
|
|
51
|
+
process.stdout.write('\u001b[2J\u001b[H');
|
|
52
|
+
process.stdout.write('gen-random settings\n');
|
|
53
|
+
process.stdout.write('Type a printable ASCII prefix Backspace delete Enter save Esc cancel\n\n');
|
|
54
|
+
process.stdout.write(`Prefix: ${settings.prefix || '(empty)'}\n`);
|
|
55
|
+
if (notice) process.stdout.write(`\n${notice}\n`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function runSettingsMenu(settings) {
|
|
59
|
+
if (!process.stdin.isTTY || typeof process.stdin.setRawMode !== 'function') {
|
|
60
|
+
throw new Error('Settings require an interactive terminal.');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new Promise((resolve) => {
|
|
64
|
+
const input = process.stdin;
|
|
65
|
+
const output = process.stdout;
|
|
66
|
+
const previousRawMode = input.isRaw;
|
|
67
|
+
const rl = readline.createInterface({ input, output });
|
|
68
|
+
readline.emitKeypressEvents(input, rl);
|
|
69
|
+
input.setRawMode(true);
|
|
70
|
+
input.resume();
|
|
71
|
+
|
|
72
|
+
let notice = '';
|
|
73
|
+
let finished = false;
|
|
74
|
+
|
|
75
|
+
const finish = (result) => {
|
|
76
|
+
if (finished) return;
|
|
77
|
+
finished = true;
|
|
78
|
+
input.removeListener('keypress', onKeypress);
|
|
79
|
+
input.setRawMode(Boolean(previousRawMode));
|
|
80
|
+
rl.close();
|
|
81
|
+
output.write('\u001b[2J\u001b[H');
|
|
82
|
+
resolve(result);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const onKeypress = (character, key = {}) => {
|
|
86
|
+
if (key.ctrl && key.name === 'c') {
|
|
87
|
+
finish(null);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (key.name === 'escape') {
|
|
92
|
+
finish(null);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (key.name === 'backspace' || key.name === 'delete') {
|
|
96
|
+
settings.prefix = [...settings.prefix].slice(0, -1).join('');
|
|
97
|
+
notice = '';
|
|
98
|
+
} else if (key.name === 'return') {
|
|
99
|
+
if (isValidSettings(settings)) finish(settings);
|
|
100
|
+
else notice = 'Prefix can contain printable ASCII characters only.';
|
|
101
|
+
return;
|
|
102
|
+
} else if (!key.ctrl && !key.meta && typeof character === 'string' && /^[\x20-\x7E]$/.test(character)) {
|
|
103
|
+
settings.prefix += character;
|
|
104
|
+
notice = '';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
drawSettingsMenu(settings, notice);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
input.on('keypress', onKeypress);
|
|
111
|
+
drawSettingsMenu(settings);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function printUsage() {
|
|
116
|
+
process.stdout.write('gen-random - secure random hexadecimal values\n\n');
|
|
117
|
+
process.stdout.write('Usage:\n');
|
|
118
|
+
process.stdout.write(' gen-random [--bits 128|256] [--prefix <text>]\n');
|
|
119
|
+
process.stdout.write(' gen-random settings\n');
|
|
120
|
+
process.stdout.write(' gen-random --help\n\n');
|
|
121
|
+
process.stdout.write('Options:\n');
|
|
122
|
+
process.stdout.write(' --bits <128|256> Print only the selected format.\n');
|
|
123
|
+
process.stdout.write(' --prefix <text> Override the prefix for this run.\n');
|
|
124
|
+
process.stdout.write(' settings Edit and save the default prefix.\n');
|
|
125
|
+
process.stdout.write(' -h, --help Show this help.\n');
|
|
126
|
+
process.stdout.write(' -V, --version Show the installed version.\n\n');
|
|
127
|
+
process.stdout.write('With no size option, both formats are printed. The default prefix is sk_.\n');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function main() {
|
|
131
|
+
const argumentsList = process.argv.slice(2);
|
|
132
|
+
if (argumentsList.length === 1 && (argumentsList[0] === '--help' || argumentsList[0] === '-h')) {
|
|
133
|
+
printUsage();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (argumentsList.length === 1 && (argumentsList[0] === '--version' || argumentsList[0] === '-V')) {
|
|
137
|
+
const packageJson = require('../package.json');
|
|
138
|
+
process.stdout.write(`${packageJson.version}\n`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (argumentsList.length === 1 && argumentsList[0] === 'settings') {
|
|
142
|
+
const settings = loadSettings();
|
|
143
|
+
const result = await runSettingsMenu(settings);
|
|
144
|
+
if (result) {
|
|
145
|
+
saveSettings(result);
|
|
146
|
+
process.stdout.write(`Settings saved to ${settingsPath}\n`);
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let selectedBits = 'both';
|
|
152
|
+
let bitsWereSelected = false;
|
|
153
|
+
let prefixOverride;
|
|
154
|
+
for (let index = 0; index < argumentsList.length; index += 1) {
|
|
155
|
+
const argument = argumentsList[index];
|
|
156
|
+
if (argument === '--bits') {
|
|
157
|
+
if (bitsWereSelected) throw new Error('Use --bits only once.');
|
|
158
|
+
const value = argumentsList[index + 1];
|
|
159
|
+
if (value !== '128' && value !== '256') throw new Error('--bits must be 128 or 256.');
|
|
160
|
+
selectedBits = value;
|
|
161
|
+
bitsWereSelected = true;
|
|
162
|
+
index += 1;
|
|
163
|
+
} else if (argument === '--prefix') {
|
|
164
|
+
if (index + 1 >= argumentsList.length) throw new Error(`${argument} requires a value.`);
|
|
165
|
+
prefixOverride = argumentsList[index + 1];
|
|
166
|
+
if (!/^[\x20-\x7E]*$/.test(prefixOverride)) {
|
|
167
|
+
throw new Error('Prefix can contain printable ASCII characters only.');
|
|
168
|
+
}
|
|
169
|
+
index += 1;
|
|
170
|
+
} else {
|
|
171
|
+
throw new Error(`Unknown argument: ${argument}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const settings = loadSettings();
|
|
176
|
+
if (prefixOverride !== undefined) settings.prefix = prefixOverride;
|
|
177
|
+
const selectedSizes = selectedBits === '128'
|
|
178
|
+
? OUTPUT_SIZES.slice(0, 1)
|
|
179
|
+
: selectedBits === '256'
|
|
180
|
+
? OUTPUT_SIZES.slice(1)
|
|
181
|
+
: OUTPUT_SIZES;
|
|
182
|
+
|
|
183
|
+
for (const { targetBits, totalChars, minRandomHexLength } of selectedSizes) {
|
|
184
|
+
const randomHexLength = Math.max(totalChars - settings.prefix.length, minRandomHexLength);
|
|
185
|
+
const randomHex = crypto.randomBytes(Math.ceil(randomHexLength / 2)).toString('hex').slice(0, randomHexLength);
|
|
186
|
+
const value = `${settings.prefix}${randomHex}`;
|
|
187
|
+
process.stdout.write(`${targetBits} bits (${value.length} chars): ${value}\n`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
main().catch((error) => {
|
|
192
|
+
process.stderr.write(`gen-random: ${error.message}\n`);
|
|
193
|
+
process.exitCode = 1;
|
|
194
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gen-random-cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A secure multi-length random password generator for your terminal",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"gen-random": "bin/gen-random.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"bin/gen-random.js",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"password",
|
|
19
|
+
"generator",
|
|
20
|
+
"cli",
|
|
21
|
+
"secure-random"
|
|
22
|
+
]
|
|
23
|
+
}
|