-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrypto.js
32 lines (26 loc) · 948 Bytes
/
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
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const algorithm = 'aes-256-ctr';
const secretKey = fs.readFileSync(path.resolve(__dirname, './private.key'));
const ivSeparator = ' ';
const encrypt = (text, iv = crypto.randomBytes(16)) => {
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
return iv.toString('hex') + ivSeparator + encrypted.toString('hex');
};
const decrypt = (text) => {
const textParts = text.split(ivSeparator);
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(ivSeparator), 'hex');
const decipher = crypto.createDecipheriv(algorithm, secretKey, iv);
const decrpyted = Buffer.concat([
decipher.update(encryptedText),
decipher.final(),
]);
return decrpyted.toString();
};
module.exports = {
encrypt,
decrypt,
};