@konplit-services/common 1.0.268 → 1.0.269
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,58 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* this is how the aes key and iv should be generated
|
|
4
|
+
*
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export declare const generateAesKeyAndIv: () => {
|
|
8
|
+
aesKey: Buffer;
|
|
9
|
+
iv: Buffer;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* @param data the data as string to be encrypted use JSON.stringify on objects
|
|
14
|
+
* @param aesKey
|
|
15
|
+
* @param iv
|
|
16
|
+
* @returns base 64 of the iv and encrypted data in iv:data
|
|
17
|
+
*/
|
|
18
|
+
export declare const encryptWithAes: (data: string, aesKey: Buffer, iv: Buffer) => string;
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param aesKey the key generated for encryption
|
|
22
|
+
* @param publicKey the public key shared will always be on the validation payload
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
export declare const encryptWithRsaPublicKey: (aesKey: Buffer, publicKey: string) => string;
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param encryptedAesKey the encrypted Aes key to be decrypted by the private key for transaction processing
|
|
29
|
+
* @param privateKey the secured save private key
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
export declare const decryptWithRsaPrivateKey: (encryptedAesKey: string, privateKey: string) => Buffer;
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param encryptedDataWithIv the encrypted data that was encrypted in the format iv:data
|
|
36
|
+
* @param aesKey the AES key as Buffer obtain from the decryption of the data
|
|
37
|
+
* @returns return data as string
|
|
38
|
+
*/
|
|
39
|
+
export declare const decryptWithAes: (encryptedDataWithIv: string, aesKey: Buffer) => string;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param data data to be encrpted as string
|
|
43
|
+
* @param publicKey the RSA public key
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
export declare const hybridEncrypt: (data: string, publicKey: string) => {
|
|
47
|
+
encryptedData: string;
|
|
48
|
+
encryptedAesKey: string;
|
|
49
|
+
iv: string;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param encryptedData the encrypted data as a strig
|
|
54
|
+
* @param encryptedAesKey the encrypted AESKey data as string
|
|
55
|
+
* @param privateKey the private key of the RSA
|
|
56
|
+
* @returns string
|
|
57
|
+
*/
|
|
58
|
+
export declare const hybridDecrypt: (encryptedData: string, encryptedAesKey: string, privateKey: string) => string;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.hybridDecrypt = exports.hybridEncrypt = exports.decryptWithAes = exports.decryptWithRsaPrivateKey = exports.encryptWithRsaPublicKey = exports.encryptWithAes = exports.generateAesKeyAndIv = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
/**
|
|
9
|
+
* this is how the aes key and iv should be generated
|
|
10
|
+
*
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
const generateAesKeyAndIv = () => {
|
|
14
|
+
const aesKey = crypto_1.default.randomBytes(32); // 256-bit key
|
|
15
|
+
const iv = crypto_1.default.randomBytes(16); // 128-bit IV
|
|
16
|
+
return { aesKey, iv };
|
|
17
|
+
};
|
|
18
|
+
exports.generateAesKeyAndIv = generateAesKeyAndIv;
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param data the data as string to be encrypted use JSON.stringify on objects
|
|
22
|
+
* @param aesKey
|
|
23
|
+
* @param iv
|
|
24
|
+
* @returns base 64 of the iv and encrypted data in iv:data
|
|
25
|
+
*/
|
|
26
|
+
const encryptWithAes = (data, aesKey, iv) => {
|
|
27
|
+
const cipher = crypto_1.default.createCipheriv("aes-256-cbc", aesKey, iv);
|
|
28
|
+
let encrypted = cipher.update(data, "utf8", "base64");
|
|
29
|
+
encrypted += cipher.final("base64");
|
|
30
|
+
// Prepend the IV to the ciphertext (IV is not secret but must be unique)
|
|
31
|
+
return iv.toString("base64") + ":" + encrypted;
|
|
32
|
+
};
|
|
33
|
+
exports.encryptWithAes = encryptWithAes;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param aesKey the key generated for encryption
|
|
37
|
+
* @param publicKey the public key shared will always be on the validation payload
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
const encryptWithRsaPublicKey = (aesKey, publicKey) => {
|
|
41
|
+
const encryptedKey = crypto_1.default.publicEncrypt({
|
|
42
|
+
key: publicKey,
|
|
43
|
+
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
|
|
44
|
+
oaepHash: "sha256",
|
|
45
|
+
}, aesKey);
|
|
46
|
+
return encryptedKey.toString("base64");
|
|
47
|
+
};
|
|
48
|
+
exports.encryptWithRsaPublicKey = encryptWithRsaPublicKey;
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @param encryptedAesKey the encrypted Aes key to be decrypted by the private key for transaction processing
|
|
52
|
+
* @param privateKey the secured save private key
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
const decryptWithRsaPrivateKey = (encryptedAesKey, privateKey) => {
|
|
56
|
+
const decryptedKey = crypto_1.default.privateDecrypt({
|
|
57
|
+
key: privateKey,
|
|
58
|
+
padding: crypto_1.default.constants.RSA_PKCS1_OAEP_PADDING,
|
|
59
|
+
oaepHash: "sha256",
|
|
60
|
+
}, Buffer.from(encryptedAesKey, "base64"));
|
|
61
|
+
return decryptedKey;
|
|
62
|
+
};
|
|
63
|
+
exports.decryptWithRsaPrivateKey = decryptWithRsaPrivateKey;
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param encryptedDataWithIv the encrypted data that was encrypted in the format iv:data
|
|
67
|
+
* @param aesKey the AES key as Buffer obtain from the decryption of the data
|
|
68
|
+
* @returns return data as string
|
|
69
|
+
*/
|
|
70
|
+
const decryptWithAes = (encryptedDataWithIv, aesKey) => {
|
|
71
|
+
// Split the IV and the encrypted data
|
|
72
|
+
const [ivBase64, encryptedData] = encryptedDataWithIv.split(":");
|
|
73
|
+
const iv = Buffer.from(ivBase64, "base64");
|
|
74
|
+
// Decrypt the data
|
|
75
|
+
const decipher = crypto_1.default.createDecipheriv("aes-256-cbc", aesKey, iv);
|
|
76
|
+
let decrypted = decipher.update(encryptedData, "base64", "utf8");
|
|
77
|
+
decrypted += decipher.final("utf8");
|
|
78
|
+
return decrypted;
|
|
79
|
+
};
|
|
80
|
+
exports.decryptWithAes = decryptWithAes;
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
* @param data data to be encrpted as string
|
|
84
|
+
* @param publicKey the RSA public key
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
const hybridEncrypt = (data, publicKey) => {
|
|
88
|
+
// Step 1: Generate AES key and IV
|
|
89
|
+
const { aesKey, iv } = (0, exports.generateAesKeyAndIv)();
|
|
90
|
+
// Step 2: Encrypt data with AES
|
|
91
|
+
const encryptedData = (0, exports.encryptWithAes)(data, aesKey, iv);
|
|
92
|
+
// Step 3: Encrypt AES key with RSA public key
|
|
93
|
+
const encryptedAesKey = (0, exports.encryptWithRsaPublicKey)(aesKey, publicKey);
|
|
94
|
+
// Return the encrypted data, encrypted AES key, and IV
|
|
95
|
+
return {
|
|
96
|
+
encryptedData,
|
|
97
|
+
encryptedAesKey,
|
|
98
|
+
iv: iv.toString("base64"), // IV is not secret but must be unique no need to return it
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
exports.hybridEncrypt = hybridEncrypt;
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @param encryptedData the encrypted data as a strig
|
|
105
|
+
* @param encryptedAesKey the encrypted AESKey data as string
|
|
106
|
+
* @param privateKey the private key of the RSA
|
|
107
|
+
* @returns string
|
|
108
|
+
*/
|
|
109
|
+
const hybridDecrypt = (encryptedData, encryptedAesKey, privateKey) => {
|
|
110
|
+
// Step 1: Decrypt AES key with RSA private key
|
|
111
|
+
const aesKey = (0, exports.decryptWithRsaPrivateKey)(encryptedAesKey, privateKey);
|
|
112
|
+
const decryptedData = (0, exports.decryptWithAes)(encryptedData, aesKey);
|
|
113
|
+
return decryptedData;
|
|
114
|
+
};
|
|
115
|
+
exports.hybridDecrypt = hybridDecrypt;
|
package/build/helper/index.d.ts
CHANGED
package/build/helper/index.js
CHANGED
|
@@ -61,3 +61,4 @@ __exportStar(require("./keys"), exports);
|
|
|
61
61
|
__exportStar(require("./wallet-transaction.types"), exports);
|
|
62
62
|
__exportStar(require("./generate-query-params"), exports);
|
|
63
63
|
__exportStar(require("./subscription-cron-expression"), exports);
|
|
64
|
+
__exportStar(require("./encryptions"), exports);
|