-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.js
40 lines (31 loc) · 1.24 KB
/
crypto.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
const {loadConfig} = require("./utils");
loadConfig();
const crypto = require('crypto');
const {loggerKeys} = require("./logger");
const algorithm = 'aes-256-ctr';
const secretKey = process.env.CRYPTO_SECRET;
const iv = crypto.randomBytes(16);
const encrypt = (text) => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
const encryptedData = {
iv: iv.toString('hex'),
content: encrypted.toString('hex')
};
return Buffer.from(JSON.stringify(encryptedData)).toString('base64');
};
const decrypt = hash => {
const encryptedData = JSON.parse(Buffer.from(hash, 'base64').toString('binary'));
const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(encryptedData.iv, 'hex'));
const decrypted = Buffer.concat([decipher.update(Buffer.from(encryptedData.content, 'hex')), decipher.final()]);
return decrypted.toString();
};
const getSecretKey = (source, bypassRecaptcha) => {
const secret = encrypt(JSON.stringify({source, bypassRecaptcha}));
loggerKeys.info(`Source: ${source}, Bypass recaptcha: ${bypassRecaptcha} - ${secret}`);
return secret;
};
module.exports = {
getSecretKey,
decrypt
};