UNPKG

3.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.encryptString = exports.decryptString = exports.encryptObject = exports.decryptObject = exports.decryptRandomIVBuffer = exports.encryptRandomIVBuffer = void 0;
4const crypto = require("node:crypto");
5const js_lib_1 = require("@naturalcycles/js-lib");
6const hash_util_1 = require("./hash.util");
7const algorithm = 'aes-256-cbc';
8/**
9 * Using aes-256-cbc
10 */
11function encryptRandomIVBuffer(input, secretKeyBase64) {
12 // md5 to match aes-256 key length of 32 bytes
13 const key = (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
14 // Random iv to achieve non-deterministic encryption (but deterministic decryption)
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}
19exports.encryptRandomIVBuffer = encryptRandomIVBuffer;
20/**
21 * Using aes-256-cbc
22 */
23function decryptRandomIVBuffer(input, secretKeyBase64) {
24 // md5 to match aes-256 key length of 32 bytes
25 const key = (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
26 // iv is first 16 bytes of encrypted buffer, the rest is payload
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}
32exports.decryptRandomIVBuffer = decryptRandomIVBuffer;
33/**
34 * Decrypts all object values.
35 * Returns object with decrypted values.
36 */
37function 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}
46exports.decryptObject = decryptObject;
47function 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}
56exports.encryptObject = encryptObject;
57/**
58 * Using aes-256-cbc
59 */
60function 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}
65exports.decryptString = decryptString;
66/**
67 * Using aes-256-cbc
68 */
69function 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}
74exports.encryptString = encryptString;
75function 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}