@meebon/meebon-crypto 1.4.0 → 1.5.0

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.
Files changed (2) hide show
  1. package/dist/index.cjs +157 -0
  2. package/package.json +10 -2
package/dist/index.cjs ADDED
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // lib/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ MeebonCrypto: () => MeebonCrypto,
34
+ decryptMiddleware: () => decryptMiddleware,
35
+ encryptionMiddleware: () => encryptionMiddleware
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+
39
+ // lib/MeebonCrypto.ts
40
+ var import_node_forge = __toESM(require("node-forge"), 1);
41
+ var MeebonCrypto = class _MeebonCrypto {
42
+ privateKeyPem;
43
+ publicKeyPem;
44
+ privateKey;
45
+ publicKey;
46
+ schema;
47
+ constructor({ privateKeyPem, publicKeyPem, schema }) {
48
+ this.privateKeyPem = privateKeyPem;
49
+ this.publicKeyPem = publicKeyPem;
50
+ this.privateKey = import_node_forge.default.pki.privateKeyFromPem(privateKeyPem);
51
+ this.publicKey = import_node_forge.default.pki.publicKeyFromPem(publicKeyPem);
52
+ this.schema = schema ?? "RSAES-PKCS1-V1_5";
53
+ }
54
+ /**
55
+ * Initializes a new instance of MeebonCrypto.
56
+ * @param props - The crypto properties including keys and optional encryption scheme.
57
+ * @returns {MeebonCrypto} The initialized MeebonCrypto instance.
58
+ */
59
+ static init(props) {
60
+ return new _MeebonCrypto(props);
61
+ }
62
+ /**
63
+ * Encrypts data using the instance's public key.
64
+ * @param {string} plainText - The data to encrypt.
65
+ * @returns {string} The Base64-encoded encrypted data.
66
+ */
67
+ encrypt(plainText) {
68
+ const encryptedBytes = this.publicKey.encrypt(plainText, this.schema);
69
+ return import_node_forge.default.util.encode64(encryptedBytes);
70
+ }
71
+ /**
72
+ * Decrypts data using the instance's private key.
73
+ * @param {string} encryptedData - The Base64-encoded encrypted data.
74
+ * @returns {string} The decrypted data.
75
+ */
76
+ decrypt(encryptedData) {
77
+ const encryptedBytes = import_node_forge.default.util.decode64(encryptedData);
78
+ return this.privateKey.decrypt(encryptedBytes, this.schema);
79
+ }
80
+ /**
81
+ * Generates an RSA key pair with the specified key size.
82
+ *
83
+ * @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
84
+ * @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
85
+ */
86
+ static generateKeyPair(length = 3072) {
87
+ const keyPair = import_node_forge.default.pki.rsa.generateKeyPair({ bits: length, e: 65537 });
88
+ return {
89
+ publicKey: import_node_forge.default.pki.publicKeyToPem(keyPair.publicKey),
90
+ privateKey: import_node_forge.default.pki.privateKeyToPem(keyPair.privateKey)
91
+ };
92
+ }
93
+ /**
94
+ * Encrypts data with a provided public key.
95
+ * @param {string} data - The data to encrypt.
96
+ * @param {string} publicKey - The public key in PEM format.
97
+ * @param {string} [scheme='RSA-OAEP'] - The encryption scheme to use (defaults to 'RSA-OAEP').
98
+ * @returns {string} The Base64-encoded encrypted data.
99
+ */
100
+ static encryptData(data, publicKey, scheme = "RSA-OAEP") {
101
+ const publicKeyObj = import_node_forge.default.pki.publicKeyFromPem(publicKey);
102
+ const encryptedBytes = publicKeyObj.encrypt(data, scheme);
103
+ return import_node_forge.default.util.encode64(encryptedBytes);
104
+ }
105
+ /**
106
+ * Decrypts data with a provided private key.
107
+ * @param {string} encryptedData - The Base64-encoded encrypted data.
108
+ * @param {string} privateKey - The private key in PEM format.
109
+ * @param {string} [scheme='RSA-OAEP'] - The decryption scheme to use (defaults to 'RSA-OAEP').
110
+ * @returns {string} The decrypted data.
111
+ */
112
+ static decryptData(encryptedData, privateKey, scheme = "RSA-OAEP") {
113
+ const privateKeyObj = import_node_forge.default.pki.privateKeyFromPem(privateKey);
114
+ const decodedBytes = import_node_forge.default.util.decode64(encryptedData);
115
+ return privateKeyObj.decrypt(decodedBytes, scheme);
116
+ }
117
+ };
118
+
119
+ // lib/ExpressEncryptionMiddleware.ts
120
+ function encryptionMiddleware({ publicKey }) {
121
+ return (req, res, next) => {
122
+ const originalSend = res.send.bind(res);
123
+ const originalJson = res.json.bind(res);
124
+ res.send = function(body) {
125
+ let bodyString = typeof body === "object" ? JSON.stringify(body) : body.toString();
126
+ const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
127
+ res.set("Content-Type", "text/plain");
128
+ return originalSend(encryptedBody);
129
+ };
130
+ res.json = function(body) {
131
+ const bodyString = JSON.stringify(body);
132
+ const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
133
+ res.set("Content-Type", "text/plain");
134
+ return originalSend(encryptedBody);
135
+ };
136
+ next();
137
+ };
138
+ }
139
+ function decryptMiddleware({ privateKey }) {
140
+ return (req, res, next) => {
141
+ if (typeof req.body === "string") {
142
+ try {
143
+ const decryptedText = MeebonCrypto.decryptData(req.body, privateKey);
144
+ req.body = JSON.parse(decryptedText);
145
+ } catch (err) {
146
+ res.status(400).json({ error: "Invalid encrypted data" });
147
+ }
148
+ }
149
+ next();
150
+ };
151
+ }
152
+ // Annotate the CommonJS export names for ESM import in node:
153
+ 0 && (module.exports = {
154
+ MeebonCrypto,
155
+ decryptMiddleware,
156
+ encryptionMiddleware
157
+ });
package/package.json CHANGED
@@ -1,12 +1,20 @@
1
1
  {
2
2
  "name": "@meebon/meebon-crypto",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "author": "kajalanS <103587022+kajalanS@users.noreply.github.com>",
6
6
  "homepage": "https://github.com/KsoftmHub/meebon-crypto?tab=readme-ov-file#getting-started",
7
7
  "license": "MIT",
8
- "main": "dist/index.js",
8
+ "main": "dist/index.cjs",
9
+ "module": "dist/index.js",
9
10
  "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ }
17
+ },
10
18
  "types": "dist/index.d.ts",
11
19
  "directories": {
12
20
  "lib": "lib",