@meebon/meebon-crypto 1.0.0 → 1.1.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.
- package/README.md +1 -3
- package/dist/ExpressEncryptionMiddleware.d.ts +8 -0
- package/dist/ExpressEncryptionMiddleware.d.ts.map +1 -0
- package/dist/MeebonCrypto.d.ts +41 -3
- package/dist/MeebonCrypto.d.ts.map +1 -1
- package/dist/index.js +58 -7
- package/package.json +2 -1
- package/types/global.d.ts +5 -0
- package/dist/types/global.d.ts +0 -7
package/README.md
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { MeebonCryptoKeyPair } from '../types/global';
|
|
3
|
+
/**
|
|
4
|
+
* Middleware Function for Encryption and Decryption
|
|
5
|
+
*/
|
|
6
|
+
declare const encryptionMiddleware: ({ privateKey, publicKey, }: MeebonCryptoKeyPair) => (req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
|
|
7
|
+
export default encryptionMiddleware;
|
|
8
|
+
//# sourceMappingURL=ExpressEncryptionMiddleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;GAEG;AACH,QAAA,MAAM,oBAAoB,+BAAgC,mBAAmB,WAC9D,OAAO,OAAO,QAAQ,QAAQ,YAAY,mDAyBxD,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
|
package/dist/MeebonCrypto.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import forge from 'node-forge';
|
|
2
|
-
import { MeebonCryptoProps } from '../types/global';
|
|
2
|
+
import { MeebonCryptoKeyPair, MeebonCryptoProps } from '../types/global';
|
|
3
3
|
export declare class MeebonCrypto {
|
|
4
4
|
protected privateKeyPem: string;
|
|
5
5
|
protected publicKeyPem: string;
|
|
@@ -7,8 +7,46 @@ export declare class MeebonCrypto {
|
|
|
7
7
|
publicKey: forge.pki.rsa.PublicKey;
|
|
8
8
|
protected schema: forge.pki.rsa.EncryptionScheme;
|
|
9
9
|
private constructor();
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Initializes a new instance of MeebonCrypto.
|
|
12
|
+
* @param props - The crypto properties including keys and optional encryption scheme.
|
|
13
|
+
* @returns {MeebonCrypto} The initialized MeebonCrypto instance.
|
|
14
|
+
*/
|
|
15
|
+
static init(props: MeebonCryptoProps): MeebonCrypto;
|
|
16
|
+
/**
|
|
17
|
+
* Encrypts data using the instance's public key.
|
|
18
|
+
* @param {string} plainText - The data to encrypt.
|
|
19
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
20
|
+
*/
|
|
21
|
+
encrypt(plainText: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Decrypts data using the instance's private key.
|
|
24
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
25
|
+
* @returns {string} The decrypted data.
|
|
26
|
+
*/
|
|
12
27
|
decrypt(encryptedData: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Generates an RSA key pair with the specified key size.
|
|
30
|
+
*
|
|
31
|
+
* @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
|
|
32
|
+
* @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
|
|
33
|
+
*/
|
|
34
|
+
static generateKeyPair(length?: number): MeebonCryptoKeyPair;
|
|
35
|
+
/**
|
|
36
|
+
* Encrypts data with a provided public key.
|
|
37
|
+
* @param {string} data - The data to encrypt.
|
|
38
|
+
* @param {string} publicKey - The public key in PEM format.
|
|
39
|
+
* @param {string} [scheme='RSA-OAEP'] - The encryption scheme to use (defaults to 'RSA-OAEP').
|
|
40
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
41
|
+
*/
|
|
42
|
+
static encryptData(data: string, publicKey: string, scheme?: forge.pki.rsa.EncryptionScheme): string;
|
|
43
|
+
/**
|
|
44
|
+
* Decrypts data with a provided private key.
|
|
45
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
46
|
+
* @param {string} privateKey - The private key in PEM format.
|
|
47
|
+
* @param {string} [scheme='RSA-OAEP'] - The decryption scheme to use (defaults to 'RSA-OAEP').
|
|
48
|
+
* @returns {string} The decrypted data.
|
|
49
|
+
*/
|
|
50
|
+
static decryptData(encryptedData: string, privateKey: string, scheme?: forge.pki.rsa.EncryptionScheme): string;
|
|
13
51
|
}
|
|
14
52
|
//# sourceMappingURL=MeebonCrypto.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MeebonCrypto.d.ts","sourceRoot":"","sources":["../lib/MeebonCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"MeebonCrypto.d.ts","sourceRoot":"","sources":["../lib/MeebonCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzE,qBAAa,YAAY;IACvB,SAAS,CAAC,aAAa,EAAE,MAAM,CAAC;IAChC,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAExB,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC;IACrC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC;IAE1C,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC;IAEjD,OAAO;IASP;;;;OAIG;WACW,IAAI,CAAC,KAAK,EAAE,iBAAiB,GAAG,YAAY;IAI1D;;;;OAIG;IACI,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAKzC;;;;OAIG;IACI,OAAO,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAK7C;;;;;OAKG;WACW,eAAe,CAAC,MAAM,GAAE,MAAa,GAAG,mBAAmB;IAQzE;;;;;;OAMG;WACW,WAAW,CACvB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,MAAM,EACjB,MAAM,GAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,gBAA6B,GAClD,MAAM;IAMT;;;;;;OAMG;WACW,WAAW,CACvB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,gBAA6B,GAClD,MAAM;CAKV"}
|
package/dist/index.js
CHANGED
|
@@ -13,17 +13,68 @@ var MeebonCrypto = class _MeebonCrypto {
|
|
|
13
13
|
this.publicKey = forge.pki.publicKeyFromPem(publicKeyPem);
|
|
14
14
|
this.schema = schema ?? "RSAES-PKCS1-V1_5";
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
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);
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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);
|
|
22
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
|
+
*/
|
|
23
38
|
decrypt(encryptedData) {
|
|
24
39
|
const encryptedBytes = forge.util.decode64(encryptedData);
|
|
25
|
-
|
|
26
|
-
|
|
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);
|
|
27
78
|
}
|
|
28
79
|
};
|
|
29
80
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meebon/meebon-crypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@eslint/js": "^9.18.0",
|
|
47
47
|
"@jest/globals": "^29.7.0",
|
|
48
|
+
"@types/express": "^5.0.0",
|
|
48
49
|
"@types/jest": "^29.5.14",
|
|
49
50
|
"@types/node": "^22.10.5",
|
|
50
51
|
"@types/node-forge": "^1.3.11",
|
package/types/global.d.ts
CHANGED