ismx-nexo-node-app 0.4.178 → 0.4.180

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.
@@ -32,6 +32,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
32
32
  });
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
+ const node_crypto_1 = require("node:crypto");
35
36
  class CryptoUtils {
36
37
  static zeroPad(buf, blocksize) {
37
38
  const pad = Buffer.alloc((blocksize - (buf.length % blocksize)) % blocksize, 0);
@@ -70,6 +71,22 @@ class CryptoUtils {
70
71
  return decryptedBuf.toString('utf8').replace(/\x00+$/g, '');
71
72
  });
72
73
  }
74
+ /** Asymmetric encryption (RSA) */
75
+ asymEncrypt(value, publicKey) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const buffer = Buffer.from(value, 'utf8');
78
+ const encrypted = (0, node_crypto_1.publicEncrypt)(publicKey, buffer);
79
+ return encrypted.toString('base64');
80
+ });
81
+ }
82
+ /** Asymmetric decryption (RSA) */
83
+ asymDecrypt(hash, privateKey) {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ const buffer = Buffer.from(hash, 'base64');
86
+ const decrypted = (0, node_crypto_1.privateDecrypt)(privateKey, buffer);
87
+ return decrypted.toString('utf8');
88
+ });
89
+ }
73
90
  static sha256Sign(merchantKey, orderId, encodedOrder) {
74
91
  return __awaiter(this, void 0, void 0, function* () {
75
92
  var _a;
@@ -53,7 +53,7 @@ class RepositoryRest extends Repository_1.default {
53
53
  return call.then((res) => __awaiter(this, void 0, void 0, function* () {
54
54
  var _a, _b, _c, _d, _e;
55
55
  let headers = {};
56
- res.headers.forEach((value, key) => headers[key.toLowerCase()] = value);
56
+ Object.entries(res.headers).forEach(([key, value]) => headers[key.toLowerCase()] = value);
57
57
  let content = yield res.text();
58
58
  (_b = (_a = this.options).onRawResponse) === null || _b === void 0 ? void 0 : _b.call(_a, request, content);
59
59
  if ((_c = headers['content-type']) === null || _c === void 0 ? void 0 : _c.startsWith('application/json'))
@@ -1,7 +1,11 @@
1
1
  export default abstract class CryptoUtils {
2
2
  private static crypto;
3
- static zeroPad(buf: Buffer, blocksize: number): Buffer;
3
+ private static zeroPad;
4
4
  static symEncrypt(key: string, message: string): Promise<Buffer>;
5
5
  static symDecrypt(key: string, encrypted: Buffer): Promise<string>;
6
- static sha256Sign(merchantKey: string, orderId: string, encodedOrder: string): Promise<string>;
6
+ /** Asymmetric encryption (RSA) */
7
+ asymEncrypt(value: string, publicKey: string): Promise<string>;
8
+ /** Asymmetric decryption (RSA) */
9
+ asymDecrypt(hash: string, privateKey: string): Promise<string>;
10
+ private static sha256Sign;
7
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ismx-nexo-node-app",
3
- "version": "0.4.178",
3
+ "version": "0.4.180",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rm -rf ./dist && npx tsc",
@@ -1,8 +1,10 @@
1
+ import {privateDecrypt, publicEncrypt} from "node:crypto";
2
+
1
3
  export default abstract class CryptoUtils {
2
4
 
3
5
  private static crypto: typeof import("crypto");
4
6
 
5
- static zeroPad(buf: Buffer, blocksize: number) {
7
+ private static zeroPad(buf: Buffer, blocksize: number) {
6
8
  const pad = Buffer.alloc((blocksize - (buf.length % blocksize)) % blocksize, 0);
7
9
  return Buffer.concat([buf, pad]);
8
10
  }
@@ -45,7 +47,21 @@ export default abstract class CryptoUtils {
45
47
  return decryptedBuf.toString('utf8').replace(/\x00+$/g, '');
46
48
  }
47
49
 
48
- static async sha256Sign(merchantKey: string, orderId: string, encodedOrder: string) {
50
+ /** Asymmetric encryption (RSA) */
51
+ async asymEncrypt(value: string, publicKey: string): Promise<string> {
52
+ const buffer = Buffer.from(value, 'utf8');
53
+ const encrypted = publicEncrypt(publicKey, buffer);
54
+ return encrypted.toString('base64');
55
+ }
56
+
57
+ /** Asymmetric decryption (RSA) */
58
+ async asymDecrypt(hash: string, privateKey: string): Promise<string> {
59
+ const buffer = Buffer.from(hash, 'base64');
60
+ const decrypted = privateDecrypt(privateKey, buffer);
61
+ return decrypted.toString('utf8');
62
+ }
63
+
64
+ private static async sha256Sign(merchantKey: string, orderId: string, encodedOrder: string) {
49
65
  let crypto = this.crypto ?? (await import("crypto")).default;
50
66
  const orderKeyBuf = await CryptoUtils.symEncrypt(merchantKey, orderId);
51
67
  return crypto.createHmac('sha256', orderKeyBuf).update(encodedOrder).digest('base64');
@@ -66,7 +66,8 @@ export default class RepositoryRest<Body=any,Res=any> extends Repository
66
66
  // Añade información adicional a la respuesta y devuelve el resultado (o el error en su caso).
67
67
  // @ts-ignore
68
68
  return call.then(async (res: Response) => {
69
- let headers: any = {}; res.headers.forEach((value, key) => headers[key.toLowerCase()] = value);
69
+ let headers: any = {};
70
+ Object.entries(res.headers).forEach(([key, value]) => headers[key.toLowerCase()] = value);
70
71
  let content: string | E = await res.text();
71
72
  this.options.onRawResponse?.(request, content);
72
73