forked from ravi0lii/node-sharex-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_key.js
More file actions
61 lines (47 loc) · 1.77 KB
/
gen_key.js
File metadata and controls
61 lines (47 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const fs = require('fs');
const crypto = require('crypto');
const path = require('path');
// Generate a secure random key
function generateKey(length = 40) {
// Characters to use in the key (alphanumeric + some special characters)
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let key = '';
// Generate cryptographically secure random bytes
const randomBytes = crypto.randomBytes(length);
// Convert random bytes to characters from our charset
for (let i = 0; i < length; i++) {
const randomIndex = randomBytes[i] % chars.length;
key += chars[randomIndex];
}
return key;
}
// Read the config file
const configPath = path.join(__dirname, 'config.json');
let config;
try {
const configData = fs.readFileSync(configPath, 'utf8');
config = JSON.parse(configData);
// Generate a new key
const newKey = generateKey();
// Prompt for a key name
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter a name for this key: ', (keyName) => {
// Initialize keys object if it doesn't exist
if (!config.keys) {
config.keys = {};
}
// Add the new key with the provided name
config.keys[keyName] = newKey;
// Write the updated config back to the file
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf8');
console.log(`New key generated for "${keyName}" and added to config.json:`);
console.log(newKey);
readline.close();
});
} catch (error) {
console.error('Error updating config file:', error.message);
process.exit(1);
}