@react-pakistan/util-functions 1.25.23 → 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.
@@ -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
+ }
@@ -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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-pakistan/util-functions",
3
- "version": "1.25.23",
3
+ "version": "1.25.24",
4
4
  "description": "A library of all util functions",
5
5
  "main": "index.js",
6
6
  "scripts": {