1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.encryptString = exports.decryptString = exports.encryptObject = exports.decryptObject = exports.decryptRandomIVBuffer = exports.encryptRandomIVBuffer = void 0;
|
4 | const crypto = require("node:crypto");
|
5 | const js_lib_1 = require("@naturalcycles/js-lib");
|
6 | const hash_util_1 = require("./hash.util");
|
7 | const algorithm = 'aes-256-cbc';
|
8 |
|
9 |
|
10 |
|
11 | function encryptRandomIVBuffer(input, secretKeyBase64) {
|
12 |
|
13 | const key = (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
|
14 |
|
15 | const iv = crypto.randomBytes(16);
|
16 | const cipher = crypto.createCipheriv(algorithm, key, iv);
|
17 | return Buffer.concat([iv, cipher.update(input), cipher.final()]);
|
18 | }
|
19 | exports.encryptRandomIVBuffer = encryptRandomIVBuffer;
|
20 |
|
21 |
|
22 |
|
23 | function decryptRandomIVBuffer(input, secretKeyBase64) {
|
24 |
|
25 | const key = (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
|
26 |
|
27 | const iv = input.subarray(0, 16);
|
28 | const payload = input.subarray(16);
|
29 | const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
30 | return Buffer.concat([decipher.update(payload), decipher.final()]);
|
31 | }
|
32 | exports.decryptRandomIVBuffer = decryptRandomIVBuffer;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function decryptObject(obj, secretKey) {
|
38 | const { key, iv } = getCryptoParams(secretKey);
|
39 | const r = {};
|
40 | (0, js_lib_1._stringMapEntries)(obj).forEach(([k, v]) => {
|
41 | const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
42 | r[k] = decipher.update(v, 'base64', 'utf8') + decipher.final('utf8');
|
43 | });
|
44 | return r;
|
45 | }
|
46 | exports.decryptObject = decryptObject;
|
47 | function encryptObject(obj, secretKey) {
|
48 | const { key, iv } = getCryptoParams(secretKey);
|
49 | const r = {};
|
50 | (0, js_lib_1._stringMapEntries)(obj).forEach(([k, v]) => {
|
51 | const cipher = crypto.createCipheriv(algorithm, key, iv);
|
52 | r[k] = cipher.update(v, 'utf8', 'base64') + cipher.final('base64');
|
53 | });
|
54 | return r;
|
55 | }
|
56 | exports.encryptObject = encryptObject;
|
57 |
|
58 |
|
59 |
|
60 | function decryptString(str, secretKey) {
|
61 | const { key, iv } = getCryptoParams(secretKey);
|
62 | const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
63 | return decipher.update(str, 'base64', 'utf8') + decipher.final('utf8');
|
64 | }
|
65 | exports.decryptString = decryptString;
|
66 |
|
67 |
|
68 |
|
69 | function encryptString(str, secretKey) {
|
70 | const { key, iv } = getCryptoParams(secretKey);
|
71 | const cipher = crypto.createCipheriv(algorithm, key, iv);
|
72 | return cipher.update(str, 'utf8', 'base64') + cipher.final('base64');
|
73 | }
|
74 | exports.encryptString = encryptString;
|
75 | function getCryptoParams(secretKey) {
|
76 | const key = (0, hash_util_1.md5)(secretKey);
|
77 | const iv = (0, hash_util_1.md5)(secretKey + key).slice(0, 16);
|
78 | return { key, iv };
|
79 | }
|