@meebon/meebon-crypto 1.2.3 → 1.2.4
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.
- package/dist/ExpressEncryptionMiddleware.js +115 -0
- package/dist/MeebonCrypto.js +82 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -35
- package/package.json +1 -1
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// lib/MeebonCrypto.ts
|
|
2
|
+
import forge from "node-forge";
|
|
3
|
+
var MeebonCrypto = class _MeebonCrypto {
|
|
4
|
+
privateKeyPem;
|
|
5
|
+
publicKeyPem;
|
|
6
|
+
privateKey;
|
|
7
|
+
publicKey;
|
|
8
|
+
schema;
|
|
9
|
+
constructor({ privateKeyPem, publicKeyPem, schema }) {
|
|
10
|
+
this.privateKeyPem = privateKeyPem;
|
|
11
|
+
this.publicKeyPem = publicKeyPem;
|
|
12
|
+
this.privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
|
|
13
|
+
this.publicKey = forge.pki.publicKeyFromPem(publicKeyPem);
|
|
14
|
+
this.schema = schema ?? "RSAES-PKCS1-V1_5";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initializes a new instance of MeebonCrypto.
|
|
18
|
+
* @param props - The crypto properties including keys and optional encryption scheme.
|
|
19
|
+
* @returns {MeebonCrypto} The initialized MeebonCrypto instance.
|
|
20
|
+
*/
|
|
21
|
+
static init(props) {
|
|
22
|
+
return new _MeebonCrypto(props);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Encrypts data using the instance's public key.
|
|
26
|
+
* @param {string} plainText - The data to encrypt.
|
|
27
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
28
|
+
*/
|
|
29
|
+
encrypt(plainText) {
|
|
30
|
+
const encryptedBytes = this.publicKey.encrypt(plainText, this.schema);
|
|
31
|
+
return forge.util.encode64(encryptedBytes);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Decrypts data using the instance's private key.
|
|
35
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
36
|
+
* @returns {string} The decrypted data.
|
|
37
|
+
*/
|
|
38
|
+
decrypt(encryptedData) {
|
|
39
|
+
const encryptedBytes = forge.util.decode64(encryptedData);
|
|
40
|
+
return this.privateKey.decrypt(encryptedBytes, this.schema);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generates an RSA key pair with the specified key size.
|
|
44
|
+
*
|
|
45
|
+
* @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
|
|
46
|
+
* @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
|
|
47
|
+
*/
|
|
48
|
+
static generateKeyPair(length = 3072) {
|
|
49
|
+
const keyPair = forge.pki.rsa.generateKeyPair({ bits: length, e: 65537 });
|
|
50
|
+
return {
|
|
51
|
+
publicKey: forge.pki.publicKeyToPem(keyPair.publicKey),
|
|
52
|
+
privateKey: forge.pki.privateKeyToPem(keyPair.privateKey)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Encrypts data with a provided public key.
|
|
57
|
+
* @param {string} data - The data to encrypt.
|
|
58
|
+
* @param {string} publicKey - The public key in PEM format.
|
|
59
|
+
* @param {string} [scheme='RSA-OAEP'] - The encryption scheme to use (defaults to 'RSA-OAEP').
|
|
60
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
61
|
+
*/
|
|
62
|
+
static encryptData(data, publicKey, scheme = "RSA-OAEP") {
|
|
63
|
+
const publicKeyObj = forge.pki.publicKeyFromPem(publicKey);
|
|
64
|
+
const encryptedBytes = publicKeyObj.encrypt(data, scheme);
|
|
65
|
+
return forge.util.encode64(encryptedBytes);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Decrypts data with a provided private key.
|
|
69
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
70
|
+
* @param {string} privateKey - The private key in PEM format.
|
|
71
|
+
* @param {string} [scheme='RSA-OAEP'] - The decryption scheme to use (defaults to 'RSA-OAEP').
|
|
72
|
+
* @returns {string} The decrypted data.
|
|
73
|
+
*/
|
|
74
|
+
static decryptData(encryptedData, privateKey, scheme = "RSA-OAEP") {
|
|
75
|
+
const privateKeyObj = forge.pki.privateKeyFromPem(privateKey);
|
|
76
|
+
const decodedBytes = forge.util.decode64(encryptedData);
|
|
77
|
+
return privateKeyObj.decrypt(decodedBytes, scheme);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// lib/ExpressEncryptionMiddleware.ts
|
|
82
|
+
var encryptionMiddleware = ({ privateKey, publicKey }) => {
|
|
83
|
+
return (req, res, next) => {
|
|
84
|
+
if (req.body && req.body.data) {
|
|
85
|
+
try {
|
|
86
|
+
const plainText = JSON.stringify(req.body.data);
|
|
87
|
+
const encrypted = MeebonCrypto.encryptData(plainText, publicKey);
|
|
88
|
+
req.body.data = encrypted;
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return next(new Error(error?.message || "Encryption failed."));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const originalSend = res.send.bind(res);
|
|
94
|
+
res.send = (body) => {
|
|
95
|
+
try {
|
|
96
|
+
if (typeof body === "string") {
|
|
97
|
+
const decrypted = MeebonCrypto.decryptData(body, privateKey);
|
|
98
|
+
try {
|
|
99
|
+
body = JSON.parse(decrypted);
|
|
100
|
+
} catch {
|
|
101
|
+
body = decrypted;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw new Error(error?.message ?? "Decryption failed");
|
|
106
|
+
}
|
|
107
|
+
return originalSend(body);
|
|
108
|
+
};
|
|
109
|
+
return next();
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
var ExpressEncryptionMiddleware_default = encryptionMiddleware;
|
|
113
|
+
export {
|
|
114
|
+
ExpressEncryptionMiddleware_default as default
|
|
115
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// lib/MeebonCrypto.ts
|
|
2
|
+
import forge from "node-forge";
|
|
3
|
+
var MeebonCrypto = class _MeebonCrypto {
|
|
4
|
+
privateKeyPem;
|
|
5
|
+
publicKeyPem;
|
|
6
|
+
privateKey;
|
|
7
|
+
publicKey;
|
|
8
|
+
schema;
|
|
9
|
+
constructor({ privateKeyPem, publicKeyPem, schema }) {
|
|
10
|
+
this.privateKeyPem = privateKeyPem;
|
|
11
|
+
this.publicKeyPem = publicKeyPem;
|
|
12
|
+
this.privateKey = forge.pki.privateKeyFromPem(privateKeyPem);
|
|
13
|
+
this.publicKey = forge.pki.publicKeyFromPem(publicKeyPem);
|
|
14
|
+
this.schema = schema ?? "RSAES-PKCS1-V1_5";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initializes a new instance of MeebonCrypto.
|
|
18
|
+
* @param props - The crypto properties including keys and optional encryption scheme.
|
|
19
|
+
* @returns {MeebonCrypto} The initialized MeebonCrypto instance.
|
|
20
|
+
*/
|
|
21
|
+
static init(props) {
|
|
22
|
+
return new _MeebonCrypto(props);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Encrypts data using the instance's public key.
|
|
26
|
+
* @param {string} plainText - The data to encrypt.
|
|
27
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
28
|
+
*/
|
|
29
|
+
encrypt(plainText) {
|
|
30
|
+
const encryptedBytes = this.publicKey.encrypt(plainText, this.schema);
|
|
31
|
+
return forge.util.encode64(encryptedBytes);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Decrypts data using the instance's private key.
|
|
35
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
36
|
+
* @returns {string} The decrypted data.
|
|
37
|
+
*/
|
|
38
|
+
decrypt(encryptedData) {
|
|
39
|
+
const encryptedBytes = forge.util.decode64(encryptedData);
|
|
40
|
+
return this.privateKey.decrypt(encryptedBytes, this.schema);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generates an RSA key pair with the specified key size.
|
|
44
|
+
*
|
|
45
|
+
* @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
|
|
46
|
+
* @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
|
|
47
|
+
*/
|
|
48
|
+
static generateKeyPair(length = 3072) {
|
|
49
|
+
const keyPair = forge.pki.rsa.generateKeyPair({ bits: length, e: 65537 });
|
|
50
|
+
return {
|
|
51
|
+
publicKey: forge.pki.publicKeyToPem(keyPair.publicKey),
|
|
52
|
+
privateKey: forge.pki.privateKeyToPem(keyPair.privateKey)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Encrypts data with a provided public key.
|
|
57
|
+
* @param {string} data - The data to encrypt.
|
|
58
|
+
* @param {string} publicKey - The public key in PEM format.
|
|
59
|
+
* @param {string} [scheme='RSA-OAEP'] - The encryption scheme to use (defaults to 'RSA-OAEP').
|
|
60
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
61
|
+
*/
|
|
62
|
+
static encryptData(data, publicKey, scheme = "RSA-OAEP") {
|
|
63
|
+
const publicKeyObj = forge.pki.publicKeyFromPem(publicKey);
|
|
64
|
+
const encryptedBytes = publicKeyObj.encrypt(data, scheme);
|
|
65
|
+
return forge.util.encode64(encryptedBytes);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Decrypts data with a provided private key.
|
|
69
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
70
|
+
* @param {string} privateKey - The private key in PEM format.
|
|
71
|
+
* @param {string} [scheme='RSA-OAEP'] - The decryption scheme to use (defaults to 'RSA-OAEP').
|
|
72
|
+
* @returns {string} The decrypted data.
|
|
73
|
+
*/
|
|
74
|
+
static decryptData(encryptedData, privateKey, scheme = "RSA-OAEP") {
|
|
75
|
+
const privateKeyObj = forge.pki.privateKeyFromPem(privateKey);
|
|
76
|
+
const decodedBytes = forge.util.decode64(encryptedData);
|
|
77
|
+
return privateKeyObj.decrypt(decodedBytes, scheme);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
export {
|
|
81
|
+
MeebonCrypto
|
|
82
|
+
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,eAAe,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -78,43 +78,8 @@ var MeebonCrypto = class _MeebonCrypto {
|
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
// lib/ExpressEncryptionMiddleware.ts
|
|
82
|
-
var encryptionMiddleware = ({ privateKey, publicKey }) => {
|
|
83
|
-
return (req, res, next) => {
|
|
84
|
-
if (req.body && req.body.data) {
|
|
85
|
-
try {
|
|
86
|
-
const plainText = JSON.stringify(req.body.data);
|
|
87
|
-
const encrypted = MeebonCrypto.encryptData(plainText, publicKey);
|
|
88
|
-
req.body.data = encrypted;
|
|
89
|
-
} catch (error) {
|
|
90
|
-
return next(new Error(error?.message || "Encryption failed."));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
const originalSend = res.send.bind(res);
|
|
94
|
-
res.send = (body) => {
|
|
95
|
-
try {
|
|
96
|
-
if (typeof body === "string") {
|
|
97
|
-
const decrypted = MeebonCrypto.decryptData(body, privateKey);
|
|
98
|
-
try {
|
|
99
|
-
body = JSON.parse(decrypted);
|
|
100
|
-
} catch {
|
|
101
|
-
body = decrypted;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} catch (error) {
|
|
105
|
-
throw new Error(error?.message ?? "Decryption failed");
|
|
106
|
-
}
|
|
107
|
-
return originalSend(body);
|
|
108
|
-
};
|
|
109
|
-
return next();
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
var ExpressEncryptionMiddleware_default = encryptionMiddleware;
|
|
113
|
-
|
|
114
81
|
// lib/index.ts
|
|
115
|
-
var EncryptionMiddleware = ExpressEncryptionMiddleware_default;
|
|
116
82
|
var index_default = MeebonCrypto;
|
|
117
83
|
export {
|
|
118
|
-
EncryptionMiddleware,
|
|
119
84
|
index_default as default
|
|
120
85
|
};
|
package/package.json
CHANGED