anote-server-libs 0.11.5 → 0.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CryptModelDao = void 0;
4
- const crypto_1 = require("crypto");
5
- const util_1 = require("util");
6
- const ModelDao_1 = require("./ModelDao");
7
- const randomFillAsync = (0, util_1.promisify)(crypto_1.randomFill);
8
- class CryptModelDao extends ModelDao_1.ModelDao {
9
- constructor(keyBase64, encryptedColumns, ...args) {
10
- super(...args);
11
- this.key = Buffer.from(keyBase64, 'base64');
12
- this.encryptedColumns = encryptedColumns;
13
- }
14
- serializeWrapper(instance, request) {
15
- const props = this.serialize(instance, request);
16
- const encryptPromises = [];
17
- this.encryptedColumns.forEach(col => {
18
- const idx = this.updateDefinition.split(',').findIndex(def => def.trim().startsWith('"' + col + '"') || def.trim().startsWith(col + '='));
19
- if (idx >= 0) {
20
- const val = request ? request.parameters[idx] : props[idx];
21
- if (val !== null && val !== undefined) {
22
- const encryptPromise = this.encrypt(String(val)).then(encrypted => {
23
- if (request)
24
- request.replaceInput(String(idx + 1), encrypted);
25
- else
26
- props[idx] = encrypted;
27
- });
28
- encryptPromises.push(encryptPromise);
29
- }
30
- }
31
- });
32
- if (encryptPromises.length > 0) {
33
- return Promise.all(encryptPromises).then(() => props);
34
- }
35
- return props;
36
- }
37
- buildObjectWrapper(row) {
38
- const decryptPromises = [];
39
- this.encryptedColumns.forEach(col => {
40
- if (row[col] !== null && row[col] !== undefined) {
41
- const decryptPromise = this.decrypt(String(row[col])).then(decrypted => {
42
- row[col] = decrypted;
43
- });
44
- decryptPromises.push(decryptPromise);
45
- }
46
- });
47
- if (decryptPromises.length > 0) {
48
- return Promise.all(decryptPromises).then(() => this.buildObject(row));
49
- }
50
- return this.buildObject(row);
51
- }
52
- async encrypt(decrypted) {
53
- const iv = new Uint8Array(16);
54
- await randomFillAsync(iv);
55
- const cipher = (0, crypto_1.createCipheriv)(CryptModelDao.ALGORITHM, this.key, iv);
56
- let encrypted = '';
57
- cipher.setEncoding('base64');
58
- cipher.on('data', (chunk) => encrypted += chunk);
59
- cipher.write(decrypted);
60
- cipher.end();
61
- return `$$enc$$:${Buffer.from(iv).toString('base64')}${encrypted}`;
62
- }
63
- async decrypt(encrypted) {
64
- if (!encrypted.startsWith('$$enc$$:')) {
65
- return encrypted;
66
- }
67
- encrypted = encrypted.slice(8);
68
- const iv = new Uint8Array(Buffer.from(encrypted.slice(0, 24), 'base64'));
69
- const decipher = (0, crypto_1.createDecipheriv)(CryptModelDao.ALGORITHM, this.key, iv);
70
- let decrypted = '';
71
- decipher.on('readable', () => {
72
- for (let chunk = decipher.read(); chunk !== null; chunk = decipher.read()) {
73
- decrypted += chunk.toString('base64');
74
- }
75
- });
76
- decipher.write(encrypted.slice(24), 'base64');
77
- decipher.end();
78
- return Buffer.from(decrypted, 'base64').toString('utf8');
79
- }
80
- }
81
- exports.CryptModelDao = CryptModelDao;
82
- CryptModelDao.ALGORITHM = 'aes-256-cbc';