@react-pakistan/util-functions 1.25.23 → 1.25.25
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.
- package/general/crypto.d.ts +9 -0
- package/general/crypto.js +45 -0
- package/general/generic-cache.js +3 -2
- package/general/index.d.ts +1 -0
- package/general/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encrypt a string using a password. Returns a base64 payload containing
|
|
3
|
+
* salt + iv + authTag + ciphertext.
|
|
4
|
+
*/
|
|
5
|
+
export declare function encryptString(plain: string, password: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Decrypt a base64 payload produced by `encryptString` using the same password.
|
|
8
|
+
*/
|
|
9
|
+
export declare function decryptString(payload: string, password: string): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encryptString = encryptString;
|
|
4
|
+
exports.decryptString = decryptString;
|
|
5
|
+
var crypto_1 = require("crypto");
|
|
6
|
+
var ALGORITHM = 'aes-256-gcm';
|
|
7
|
+
var SALT_LENGTH = 16;
|
|
8
|
+
var IV_LENGTH = 12;
|
|
9
|
+
var TAG_LENGTH = 16;
|
|
10
|
+
var KEY_LENGTH = 32;
|
|
11
|
+
/**
|
|
12
|
+
* Encrypt a string using a password. Returns a base64 payload containing
|
|
13
|
+
* salt + iv + authTag + ciphertext.
|
|
14
|
+
*/
|
|
15
|
+
function encryptString(plain, password) {
|
|
16
|
+
var salt = (0, crypto_1.randomBytes)(SALT_LENGTH);
|
|
17
|
+
var iv = (0, crypto_1.randomBytes)(IV_LENGTH);
|
|
18
|
+
var key = (0, crypto_1.scryptSync)(password, salt, KEY_LENGTH);
|
|
19
|
+
var cipher = (0, crypto_1.createCipheriv)(ALGORITHM, key, iv);
|
|
20
|
+
var ciphertext = Buffer.concat([
|
|
21
|
+
cipher.update(plain, 'utf8'),
|
|
22
|
+
cipher.final(),
|
|
23
|
+
]);
|
|
24
|
+
var tag = cipher.getAuthTag();
|
|
25
|
+
var out = Buffer.concat([salt, iv, tag, ciphertext]);
|
|
26
|
+
return out.toString('base64');
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Decrypt a base64 payload produced by `encryptString` using the same password.
|
|
30
|
+
*/
|
|
31
|
+
function decryptString(payload, password) {
|
|
32
|
+
var data = Buffer.from(payload, 'base64');
|
|
33
|
+
var salt = data.slice(0, SALT_LENGTH);
|
|
34
|
+
var iv = data.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
|
35
|
+
var tag = data.slice(SALT_LENGTH + IV_LENGTH, SALT_LENGTH + IV_LENGTH + TAG_LENGTH);
|
|
36
|
+
var ciphertext = data.slice(SALT_LENGTH + IV_LENGTH + TAG_LENGTH);
|
|
37
|
+
var key = (0, crypto_1.scryptSync)(password, salt, KEY_LENGTH);
|
|
38
|
+
var decipher = (0, crypto_1.createDecipheriv)(ALGORITHM, key, iv);
|
|
39
|
+
decipher.setAuthTag(tag);
|
|
40
|
+
var decrypted = Buffer.concat([
|
|
41
|
+
decipher.update(ciphertext),
|
|
42
|
+
decipher.final(),
|
|
43
|
+
]);
|
|
44
|
+
return decrypted.toString('utf8');
|
|
45
|
+
}
|
package/general/generic-cache.js
CHANGED
|
@@ -166,7 +166,7 @@ exports.getCachedDataSync = getCachedDataSync;
|
|
|
166
166
|
* const active = await getCachedData<UserBE>({ config, filters: { enabled: true }, pageLimit: 200 });
|
|
167
167
|
*/
|
|
168
168
|
var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
|
|
169
|
-
var config, params, headers, cacheKey, apiUrl, responseKey, expirationMs, fallBackPageLimit, fallBackCurrentPage, filters, searchQuery, paramsWithoutFilters_1, otherParams_1, queryObj_1, searchParams_1, response_1, items_1, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, paramsWithoutFilters_2, otherParams, queryObj, searchParams, response, itemsRaw, items, count, shouldCache, itemsMap, updatedCache, error_1;
|
|
169
|
+
var config, params, headers, cacheKey, apiUrl, responseKey, expirationMs, fallBackPageLimit, fallBackCurrentPage, filters, searchQuery, paramsWithoutFilters_1, otherParams_1, queryObj_1, searchParams_1, response_1, items_1, lastCacheVersion, raw, currentTime, cachedTime, ageInMs, cachedTime, ageInMs, itemsArray, paramsWithoutFilters_2, otherParams, queryObj, searchParams, response, itemsRaw, items, count, shouldCache, itemsMap, updatedCache, error_1;
|
|
170
170
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
171
171
|
return __generator(this, function (_m) {
|
|
172
172
|
switch (_m.label) {
|
|
@@ -208,8 +208,9 @@ var getCachedData = function (opts) { return __awaiter(void 0, void 0, void 0, f
|
|
|
208
208
|
items_1 = responseKey
|
|
209
209
|
? ((_f = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _f === void 0 ? void 0 : _f[responseKey]) || []
|
|
210
210
|
: ((_g = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _g === void 0 ? void 0 : _g.items) || (response_1 === null || response_1 === void 0 ? void 0 : response_1.data) || [];
|
|
211
|
+
lastCacheVersion = (0, local_storage_1.getStorageValue)(cacheKey);
|
|
211
212
|
return [2 /*return*/, {
|
|
212
|
-
count: ((_h = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _h === void 0 ? void 0 : _h.count) ||
|
|
213
|
+
count: ((_h = response_1 === null || response_1 === void 0 ? void 0 : response_1.data) === null || _h === void 0 ? void 0 : _h.count) || (lastCacheVersion === null || lastCacheVersion === void 0 ? void 0 : lastCacheVersion.count),
|
|
213
214
|
items: items_1,
|
|
214
215
|
}];
|
|
215
216
|
case 3:
|
package/general/index.d.ts
CHANGED
|
@@ -90,6 +90,7 @@ export * from './text-to-enum';
|
|
|
90
90
|
export * from './time-out';
|
|
91
91
|
export * from './truncate-text';
|
|
92
92
|
export * from './type';
|
|
93
|
+
export * from './crypto';
|
|
93
94
|
export * from './upload-blob-to-private-supabase';
|
|
94
95
|
export * from './upload-blob-to-public-supabase';
|
|
95
96
|
export * from './upload-image-to-private-supabase';
|
package/general/index.js
CHANGED
|
@@ -106,6 +106,7 @@ __exportStar(require("./text-to-enum"), exports);
|
|
|
106
106
|
__exportStar(require("./time-out"), exports);
|
|
107
107
|
__exportStar(require("./truncate-text"), exports);
|
|
108
108
|
__exportStar(require("./type"), exports);
|
|
109
|
+
__exportStar(require("./crypto"), exports);
|
|
109
110
|
__exportStar(require("./upload-blob-to-private-supabase"), exports);
|
|
110
111
|
__exportStar(require("./upload-blob-to-public-supabase"), exports);
|
|
111
112
|
__exportStar(require("./upload-image-to-private-supabase"), exports);
|