1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.decryptRandomIVBuffer = exports.encryptRandomIVBuffer = void 0;
|
4 | const crypto = require("crypto");
|
5 | const hash_util_1 = require("./hash.util");
|
6 |
|
7 | function aes256Key(secretKeyBase64) {
|
8 |
|
9 | return (0, hash_util_1.md5)(Buffer.from(secretKeyBase64, 'base64'));
|
10 | }
|
11 | function encryptRandomIVBuffer(input, secretKeyBase64, algorithm = 'aes-256-cbc') {
|
12 | const key = aes256Key(secretKeyBase64);
|
13 |
|
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 | function decryptRandomIVBuffer(input, secretKeyBase64, algorithm = 'aes-256-cbc') {
|
21 | const key = aes256Key(secretKeyBase64);
|
22 |
|
23 | const iv = input.slice(0, 16);
|
24 | const payload = input.slice(16);
|
25 | const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
26 | return Buffer.concat([decipher.update(payload), decipher.final()]);
|
27 | }
|
28 | exports.decryptRandomIVBuffer = decryptRandomIVBuffer;
|