ismx-nexo-node-app 0.4.118 → 0.4.120

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.
@@ -52,6 +52,18 @@ class ArrayUtils {
52
52
  const onlyInArr2 = ArrayUtils.subtract(arr2, arr1);
53
53
  return Array.from(new Set([...onlyInArr1, ...onlyInArr2]).values());
54
54
  }
55
+ /**
56
+ * Returns the intersection of two arrays.
57
+ * It will include only the elements that exist in both arrays.
58
+ *
59
+ * @param {T[]} arr1 - The first array to intersect.
60
+ * @param {T[]} arr2 - The second array to intersect.
61
+ * @return {T[]} An array containing only the elements present in both arr1 and arr2.
62
+ * @template T
63
+ */
64
+ static intersect(arr1, arr2) {
65
+ return arr1.filter(item => arr2.includes(item));
66
+ }
55
67
  /**
56
68
  * Toggles the presence of an item in an array. If the item exists in the array, it is removed;
57
69
  * if it does not exist, it is added.
@@ -37,7 +37,7 @@ class CryptoUtils {
37
37
  const pad = Buffer.alloc((blocksize - (buf.length % blocksize)) % blocksize, 0);
38
38
  return Buffer.concat([buf, pad]);
39
39
  }
40
- static encrypt3DES(key, message) {
40
+ static symEncrypt(key, message) {
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
42
  var _a;
43
43
  let crypto = (_a = this.crypto) !== null && _a !== void 0 ? _a : (yield Promise.resolve().then(() => __importStar(require("crypto")))).default;
@@ -54,11 +54,27 @@ class CryptoUtils {
54
54
  return encryptedBuf.slice(0, maxLength);
55
55
  });
56
56
  }
57
+ static symDecrypt(key, encrypted) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ var _a;
60
+ let crypto = (_a = this.crypto) !== null && _a !== void 0 ? _a : (yield Promise.resolve().then(() => __importStar(require("crypto")))).default;
61
+ const keyBuf = Buffer.from(key, 'base64');
62
+ const iv = Buffer.alloc(8, 0);
63
+ const decipher = crypto.createDecipheriv('des-ede3-cbc', keyBuf, iv);
64
+ decipher.setAutoPadding(false);
65
+ const decryptedBuf = Buffer.concat([
66
+ decipher.update(encrypted),
67
+ decipher.final()
68
+ ]);
69
+ // Remove zero padding
70
+ return decryptedBuf.toString('utf8').replace(/\x00+$/g, '');
71
+ });
72
+ }
57
73
  static sha256Sign(merchantKey, orderId, encodedOrder) {
58
74
  return __awaiter(this, void 0, void 0, function* () {
59
75
  var _a;
60
76
  let crypto = (_a = this.crypto) !== null && _a !== void 0 ? _a : (yield Promise.resolve().then(() => __importStar(require("crypto")))).default;
61
- const orderKeyBuf = yield CryptoUtils.encrypt3DES(merchantKey, orderId);
77
+ const orderKeyBuf = yield CryptoUtils.symEncrypt(merchantKey, orderId);
62
78
  return crypto.createHmac('sha256', orderKeyBuf).update(encodedOrder).digest('base64');
63
79
  });
64
80
  }
@@ -25,6 +25,16 @@ export default abstract class ArrayUtils {
25
25
  * @template T
26
26
  */
27
27
  static diff<T>(arr1: T[], arr2: T[]): T[];
28
+ /**
29
+ * Returns the intersection of two arrays.
30
+ * It will include only the elements that exist in both arrays.
31
+ *
32
+ * @param {T[]} arr1 - The first array to intersect.
33
+ * @param {T[]} arr2 - The second array to intersect.
34
+ * @return {T[]} An array containing only the elements present in both arr1 and arr2.
35
+ * @template T
36
+ */
37
+ static intersect<T>(arr1: T[], arr2: T[]): T[];
28
38
  /**
29
39
  * Toggles the presence of an item in an array. If the item exists in the array, it is removed;
30
40
  * if it does not exist, it is added.
@@ -1,6 +1,7 @@
1
1
  export default abstract class CryptoUtils {
2
2
  private static crypto;
3
3
  static zeroPad(buf: Buffer, blocksize: number): Buffer;
4
- static encrypt3DES(key: string, message: string): Promise<Buffer>;
4
+ static symEncrypt(key: string, message: string): Promise<Buffer>;
5
+ static symDecrypt(key: string, encrypted: Buffer): Promise<string>;
5
6
  static sha256Sign(merchantKey: string, orderId: string, encodedOrder: string): Promise<string>;
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.118",
3
+ "version": "0.4.120",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -41,6 +41,19 @@ export default abstract class ArrayUtils {
41
41
  return Array.from(new Set([...onlyInArr1, ...onlyInArr2]).values());
42
42
  }
43
43
 
44
+ /**
45
+ * Returns the intersection of two arrays.
46
+ * It will include only the elements that exist in both arrays.
47
+ *
48
+ * @param {T[]} arr1 - The first array to intersect.
49
+ * @param {T[]} arr2 - The second array to intersect.
50
+ * @return {T[]} An array containing only the elements present in both arr1 and arr2.
51
+ * @template T
52
+ */
53
+ static intersect<T>(arr1: T[], arr2: T[]): T[] {
54
+ return arr1.filter(item => arr2.includes(item));
55
+ }
56
+
44
57
  /**
45
58
  * Toggles the presence of an item in an array. If the item exists in the array, it is removed;
46
59
  * if it does not exist, it is added.
@@ -7,7 +7,7 @@ export default abstract class CryptoUtils {
7
7
  return Buffer.concat([buf, pad]);
8
8
  }
9
9
 
10
- static async encrypt3DES(key: string, message: string)
10
+ static async symEncrypt(key: string, message: string): Promise<Buffer>
11
11
  {
12
12
  let crypto = this.crypto ?? (await import("crypto")).default;
13
13
 
@@ -27,9 +27,27 @@ export default abstract class CryptoUtils {
27
27
  return encryptedBuf.slice(0, maxLength);
28
28
  }
29
29
 
30
+ static async symDecrypt(key: string, encrypted: Buffer): Promise<string> {
31
+ let crypto = this.crypto ?? (await import("crypto")).default;
32
+
33
+ const keyBuf = Buffer.from(key, 'base64');
34
+ const iv = Buffer.alloc(8, 0);
35
+
36
+ const decipher = crypto.createDecipheriv('des-ede3-cbc', keyBuf, iv);
37
+ decipher.setAutoPadding(false);
38
+
39
+ const decryptedBuf = Buffer.concat([
40
+ decipher.update(encrypted),
41
+ decipher.final()
42
+ ]);
43
+
44
+ // Remove zero padding
45
+ return decryptedBuf.toString('utf8').replace(/\x00+$/g, '');
46
+ }
47
+
30
48
  static async sha256Sign(merchantKey: string, orderId: string, encodedOrder: string) {
31
49
  let crypto = this.crypto ?? (await import("crypto")).default;
32
- const orderKeyBuf = await CryptoUtils.encrypt3DES(merchantKey, orderId);
50
+ const orderKeyBuf = await CryptoUtils.symEncrypt(merchantKey, orderId);
33
51
  return crypto.createHmac('sha256', orderKeyBuf).update(encodedOrder).digest('base64');
34
52
  }
35
53
  }