@meebon/meebon-crypto 1.2.2 → 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.d.ts +6 -3
- package/dist/ExpressEncryptionMiddleware.d.ts.map +1 -1
- 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 -30
- package/package.json +1 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
2
|
import { MeebonCryptoKeyPair } from '../types/global';
|
|
3
3
|
/**
|
|
4
|
-
* Middleware
|
|
4
|
+
* Middleware for encrypting incoming request data and decrypting outgoing response data.
|
|
5
|
+
*
|
|
6
|
+
* @param {MeebonCryptoKeyPair} keys - An object containing the privateKey and publicKey in PEM format.
|
|
7
|
+
* @returns {RequestHandler} The Express middleware function.
|
|
5
8
|
*/
|
|
6
|
-
declare const encryptionMiddleware: ({ privateKey, publicKey
|
|
9
|
+
declare const encryptionMiddleware: ({ privateKey, publicKey }: MeebonCryptoKeyPair) => RequestHandler;
|
|
7
10
|
export default encryptionMiddleware;
|
|
8
11
|
//# sourceMappingURL=ExpressEncryptionMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;GAKG;AACH,QAAA,MAAM,oBAAoB,8BAA+B,mBAAmB,KAAG,cAkC9E,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
|
|
@@ -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
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MeebonCrypto } from './MeebonCrypto';
|
|
2
|
-
export declare const EncryptionMiddleware: ({ privateKey, publicKey, }: import("../types/global").MeebonCryptoKeyPair) => (req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => void;
|
|
3
2
|
export default MeebonCrypto;
|
|
4
3
|
//# sourceMappingURL=index.d.ts.map
|
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,38 +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.data) {
|
|
85
|
-
try {
|
|
86
|
-
const encrypted = MeebonCrypto.encryptData(req.body.dataToEncrypt, publicKey);
|
|
87
|
-
req.body.data = encrypted;
|
|
88
|
-
} catch (error) {
|
|
89
|
-
throw new Error(error?.message ?? error);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
const originalSend = res.send;
|
|
93
|
-
res.send = function(body) {
|
|
94
|
-
try {
|
|
95
|
-
if (body && body.encryptedData) {
|
|
96
|
-
const decrypted = MeebonCrypto.decryptData(body.encryptedData, privateKey);
|
|
97
|
-
body.decryptedData = decrypted;
|
|
98
|
-
}
|
|
99
|
-
return originalSend.call(this, body);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
throw new Error(error?.message ?? error);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
return next();
|
|
105
|
-
};
|
|
106
|
-
};
|
|
107
|
-
var ExpressEncryptionMiddleware_default = encryptionMiddleware;
|
|
108
|
-
|
|
109
81
|
// lib/index.ts
|
|
110
|
-
var EncryptionMiddleware = ExpressEncryptionMiddleware_default;
|
|
111
82
|
var index_default = MeebonCrypto;
|
|
112
83
|
export {
|
|
113
|
-
EncryptionMiddleware,
|
|
114
84
|
index_default as default
|
|
115
85
|
};
|
package/package.json
CHANGED