forked from lexogrine/csgo-react-hud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.js
75 lines (60 loc) · 1.98 KB
/
sign.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const jwt = require('jsonwebtoken');
const sign = () => {
const getAllFilesToSign = (hudDir) => {
const files = [];
const getFiles = (dir) => {
fs.readdirSync(dir).forEach(file => {
const fileDirectory = path.join(dir, file);
if (fs.statSync(fileDirectory).isDirectory()) return getFiles(fileDirectory);
else if (fileDirectory.endsWith('.js') || fileDirectory.endsWith('.css')) return files.push(fileDirectory);
})
}
getFiles(hudDir)
return files;
}
const dir = path.join(__dirname, 'build');
const keyFile = path.join(dir, 'key');
if (fs.existsSync(keyFile)) {
return true;
}
const filesToSign = getAllFilesToSign(dir);
const passphrase = crypto.randomBytes(48).toString('hex');
filesToSign.push(path.join(dir, 'hud.json'));
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase
}
});
let success = true;
const fileToContent = {};
filesToSign.forEach(file => {
if (!success) {
return;
}
const content = fs.readFileSync(file, 'utf8');
try {
const signed = jwt.sign(content, { key: keys.privateKey.toString(), passphrase }, { algorithm: 'RS256' });
fileToContent[file] = signed;
} catch {
success = false;
}
});
if (!success) return false;
filesToSign.forEach(file => {
fs.writeFileSync(file, fileToContent[file]);
});
fs.writeFileSync(keyFile, keys.publicKey.toString());
return success;
}
sign();