ismx-nexo-node-app 0.4.179 → 0.4.181
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
|
+
static 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
|
+
static 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;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export default abstract class CryptoUtils {
|
|
2
2
|
private static crypto;
|
|
3
|
-
static zeroPad
|
|
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
|
-
|
|
6
|
+
/** Asymmetric encryption (RSA) */
|
|
7
|
+
static asymEncrypt(value: string, publicKey: string): Promise<string>;
|
|
8
|
+
/** Asymmetric decryption (RSA) */
|
|
9
|
+
static asymDecrypt(hash: string, privateKey: string): Promise<string>;
|
|
10
|
+
private static sha256Sign;
|
|
7
11
|
}
|
package/package.json
CHANGED
|
@@ -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
|
-
|
|
50
|
+
/** Asymmetric encryption (RSA) */
|
|
51
|
+
static 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
|
+
static 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');
|