@react-pakistan/util-functions 1.25.22 → 1.25.24
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/index.d.ts +2 -0
- package/general/index.js +2 -0
- package/general/is-created-or-updated.d.ts +11 -0
- package/general/is-created-or-updated.js +26 -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/index.d.ts
CHANGED
|
@@ -72,6 +72,7 @@ export * from './get-number-of-days-in-month';
|
|
|
72
72
|
export * from './get-pathname';
|
|
73
73
|
export * from './get-week-day';
|
|
74
74
|
export * from './image-url-to-base64';
|
|
75
|
+
export * from './is-created-or-updated';
|
|
75
76
|
export * from './is-next-button-disabled';
|
|
76
77
|
export * from './is-odd';
|
|
77
78
|
export * from './is-previous-button-disabled';
|
|
@@ -89,6 +90,7 @@ export * from './text-to-enum';
|
|
|
89
90
|
export * from './time-out';
|
|
90
91
|
export * from './truncate-text';
|
|
91
92
|
export * from './type';
|
|
93
|
+
export * from './crypto';
|
|
92
94
|
export * from './upload-blob-to-private-supabase';
|
|
93
95
|
export * from './upload-blob-to-public-supabase';
|
|
94
96
|
export * from './upload-image-to-private-supabase';
|
package/general/index.js
CHANGED
|
@@ -88,6 +88,7 @@ __exportStar(require("./get-number-of-days-in-month"), exports);
|
|
|
88
88
|
__exportStar(require("./get-pathname"), exports);
|
|
89
89
|
__exportStar(require("./get-week-day"), exports);
|
|
90
90
|
__exportStar(require("./image-url-to-base64"), exports);
|
|
91
|
+
__exportStar(require("./is-created-or-updated"), exports);
|
|
91
92
|
__exportStar(require("./is-next-button-disabled"), exports);
|
|
92
93
|
__exportStar(require("./is-odd"), exports);
|
|
93
94
|
__exportStar(require("./is-previous-button-disabled"), exports);
|
|
@@ -105,6 +106,7 @@ __exportStar(require("./text-to-enum"), exports);
|
|
|
105
106
|
__exportStar(require("./time-out"), exports);
|
|
106
107
|
__exportStar(require("./truncate-text"), exports);
|
|
107
108
|
__exportStar(require("./type"), exports);
|
|
109
|
+
__exportStar(require("./crypto"), exports);
|
|
108
110
|
__exportStar(require("./upload-blob-to-private-supabase"), exports);
|
|
109
111
|
__exportStar(require("./upload-blob-to-public-supabase"), exports);
|
|
110
112
|
__exportStar(require("./upload-image-to-private-supabase"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility to determine whether a returned entity was just created
|
|
3
|
+
* by comparing createdAt and updatedAt timestamps.
|
|
4
|
+
*
|
|
5
|
+
* Returns `true` if createdAt and updatedAt are present and equal (created),
|
|
6
|
+
* otherwise returns `false` (updated or timestamps missing).
|
|
7
|
+
*/
|
|
8
|
+
export declare function isCreatedOrUpdated(entity: {
|
|
9
|
+
createdAt?: string | Date | null;
|
|
10
|
+
updatedAt?: string | Date | null;
|
|
11
|
+
}): boolean;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isCreatedOrUpdated = isCreatedOrUpdated;
|
|
4
|
+
/**
|
|
5
|
+
* Utility to determine whether a returned entity was just created
|
|
6
|
+
* by comparing createdAt and updatedAt timestamps.
|
|
7
|
+
*
|
|
8
|
+
* Returns `true` if createdAt and updatedAt are present and equal (created),
|
|
9
|
+
* otherwise returns `false` (updated or timestamps missing).
|
|
10
|
+
*/
|
|
11
|
+
function isCreatedOrUpdated(entity) {
|
|
12
|
+
if (!entity)
|
|
13
|
+
return false;
|
|
14
|
+
var toDate = function (v) {
|
|
15
|
+
if (v === undefined || v === null)
|
|
16
|
+
return undefined;
|
|
17
|
+
if (typeof v === 'string')
|
|
18
|
+
return new Date(v);
|
|
19
|
+
return v instanceof Date ? v : new Date(String(v));
|
|
20
|
+
};
|
|
21
|
+
var created = toDate(entity.createdAt);
|
|
22
|
+
var updated = toDate(entity.updatedAt);
|
|
23
|
+
if (!created || !updated)
|
|
24
|
+
return false;
|
|
25
|
+
return created.getTime() === updated.getTime();
|
|
26
|
+
}
|