encrypt-rsa 3.0.1 → 3.1.1

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 CHANGED
@@ -1,73 +1,212 @@
1
+ # NodeRSA
1
2
 
2
- # Encrypt By RSA Algorithm
3
+ **NodeRSA** is a library that provides easy-to-use methods for RSA encryption and decryption. It allows generating RSA key pairs, encrypting, and decrypting strings with RSA public and private keys. This library is ideal for secure data transmission, authentication systems, and any application requiring cryptographic security.
3
4
 
4
- [![npm version](https://badge.fury.io/js/encrypt-rsa.svg)](https://badge.fury.io/js/encrypt-rsa) 
5
- ![https://img.shields.io/npm/dm/encrypt-rsa.svg](https://img.shields.io/npm/dm/encrypt-rsa.svg)
5
+ ## Installation
6
+ ```bash
7
+ npm install encrypt-rsa
8
+ // OR
9
+ yarn add encrypt-rsa
10
+ ```
6
11
 
7
- This package works fine with web browsers and servers
12
+ ## Usage
13
+ **Importing the Library**
8
14
 
9
- > RSA is a public-key cryptoSystem that is widely used for secure data transmission. It is also one of the oldest. The acronym RSA comes from the surnames of Ron Rivest, Adi Shamir and Leonard Adleman, who publicly described the algorithm in 1977. [Wikipedia](https://en.wikipedia.org/wiki/RSA_(cryptosystem))
15
+ ```ts
16
+ import NodeRSA from 'encrypt-rsa';
17
+ ```
10
18
 
11
- RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works on two different keys i.e. Public Key and Private Key. As the name describes that the Public Key is given to everyone and Private key is kept private.
19
+ ## Creating an Instance
12
20
 
13
- ### For more explanation about RSA Algorithm visit
21
+ You can create an instance of the NodeRSA class with optional public and private keys and modulus length.
14
22
 
15
- - [Rsa Algorithm](https://milad-ezzat.vercel.app/blog/encrypt-by-rsa-algorithm)
23
+ ```ts
24
+ const nodeRSA = new NodeRSA(publicKey, privateKey, modulusLength);
25
+ ```
16
26
 
17
- 1. [Installation](#installation)
18
- 2. [Usage](#usage)
27
+ ## Generating RSA Key Pairs
28
+ To generate a new pair of RSA keys:
19
29
 
20
- ## Installation
30
+ ```ts
31
+ const { publicKey, privateKey } = nodeRSA.createPrivateAndPublicKeys(modulusLength);
32
+ console.log('Public Key:', publicKey);
33
+ console.log('Private Key:', privateKey);
34
+ ```
21
35
 
22
- ```bash
23
- npm i encrypt-rsa --only=production
24
- // OR
25
- yarn add encrypt-rsa --only=production
36
+ ## Encrypting and Decrypting Strings
37
+ ### Encrypting with RSA Public Key
38
+ ```ts
39
+ const text = "Hello, World!";
40
+ const encryptedString = nodeRSA.encryptStringWithRsaPublicKey({ text, publicKey });
41
+ console.log('Encrypted:', encryptedString);
26
42
  ```
27
43
 
28
- ## Usage
44
+ ### Decrypting with RSA Private Key
45
+ ```ts
46
+ const decryptedString = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedString, privateKey });
47
+ console.log('Decrypted:', decryptedString);
48
+ ```
29
49
 
30
- ```js
31
- import EncryptRsa from 'encrypt-rsa';
50
+ ### Encrypting with RSA Private Key
51
+ ```ts
52
+ const text = "Hello, World!";
53
+ const encryptedString = nodeRSA.encrypt({ text, privateKey });
54
+ console.log('Encrypted with Private Key:', encryptedString);
55
+ ```
32
56
 
33
- //OR
57
+ ### Decrypting with RSA Public Key
58
+ ```ts
59
+ const decryptedString = nodeRSA.decrypt({ text: encryptedString, publicKey });
60
+ console.log('Decrypted with Public Key:', decryptedString);
61
+ ```
34
62
 
35
- const EncryptRsa = require('encrypt-rsa').default;
63
+ ## API
64
+ ### NodeRSA Class
36
65
 
37
- // create instance
38
- const encryptRsa = new EncryptRsa();
66
+ ### Constructor
67
+ ```ts
68
+ constructor(publicKey?: string, privateKey?: string, modulusLength?: number)
69
+ ```
70
+ - `publicKey`: Optional. The RSA public key.
71
+ - `privateKey`: Optional. The RSA private key.
72
+ - `modulusLength`: Optional. The modulus length for the RSA key pair (default is 2048).
73
+
74
+ ### Methods
75
+ 1. `createPrivateAndPublicKeys(modulusLength: number = this.modulusLength): returnCreateKeys`
76
+ - Generates a new pair of RSA keys.
77
+ - `modulusLength`: Optional. The modulus length for the RSA key pair (default is the instance's modulus length).
78
+ - Returns an object containing the `publicKey` and `privateKey`.
79
+ 2. `encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string`
80
+ - Encrypts a string with the given RSA public key.
81
+ - `args`: Object containing `text` and optionally `publicKey`.
82
+ - Returns the encrypted string in base64 format.
83
+
84
+ 3. `decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string`
85
+ - Decrypts a string with the given RSA private key.
86
+ - `args`: Object containing `text` and optionally `privateKey`.
87
+ - Returns the decrypted string.
88
+
89
+ 4. `encrypt(args: parametersOfEncryptPrivate): string`
90
+ - Encrypts a string with the given RSA private key.
91
+ - `args`: Object containing `text` and optionally `privateKey`.
92
+ - Returns the encrypted string in base64 format.
93
+ -
94
+ 5. `decrypt(args: parametersOfDecryptPublic): string`
95
+ - Decrypts a string with the given RSA public key.
96
+ - `args`: Object containing `text` and optionally `publicKey`.
97
+ - Returns the decrypted string.
98
+
99
+ ### Types
100
+ 1. parametersOfEncrypt
101
+ ```ts
102
+ {
103
+ text: string;
104
+ publicKey?: string;
105
+ }
106
+ ```
107
+ 2. parametersOfDecrypt
108
+ ```ts
109
+ {
110
+ text: string;
111
+ privateKey?: string;
112
+ }
39
113
  ```
114
+ 3. parametersOfEncryptPrivate
115
+ ```ts
116
+ {
117
+ text: string;
118
+ privateKey?: string;
119
+ }
120
+ ```
121
+ 4. parametersOfDecryptPublic
122
+ ```ts
123
+ {
124
+ text: string;
125
+ publicKey?: string;
126
+ }
127
+ ```
128
+ 5. returnCreateKeys
129
+ ```ts
130
+ {
131
+ publicKey: string;
132
+ privateKey: string;
133
+ }
134
+ ```
135
+
136
+ ### Utilities
137
+ 1. `convertKetToBase64(key: string): string`
138
+ Converts a given key to base64 format.
40
139
 
41
- ### Create Private and Public keys
140
+ ### Helper Functions
141
+ 1. `encode`
142
+ Encodes a string to base64.
42
143
 
43
- ```js
44
- const { privateKey, publicKey } = nodeRSA.createPrivateAndPublicKeys();
144
+ 2. `decode`
145
+ Decodes a base64 string.
146
+
147
+ ### Use Cases
148
+
149
+ #### Secure Data Transmission
150
+
151
+ NodeRSA can be used to securely transmit sensitive data over insecure channels. Encrypt data with the recipient's public key before sending it. Only the recipient can decrypt the data with their private key.
152
+
153
+
154
+ ```ts
155
+ // Sender
156
+ const encryptedMessage = nodeRSA.encryptStringWithRsaPublicKey({ text: "Sensitive data", publicKey: recipientPublicKey });
157
+ // Send `encryptedMessage` to the recipient
158
+
159
+ // Recipient
160
+ const decryptedMessage = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedMessage, privateKey: recipientPrivateKey });
161
+ console.log('Decrypted Message:', decryptedMessage);
45
162
  ```
46
163
 
47
- ### Encrypt Text
164
+ #### Authentication Systems
165
+ NodeRSA can be used in authentication systems to encrypt credentials and sensitive information.
166
+
167
+
168
+ ```ts
169
+ // Encrypting user credentials
170
+ const encryptedCredentials = nodeRSA.encryptStringWithRsaPublicKey({ text: "username:password", publicKey: serverPublicKey });
48
171
 
49
- ```js
50
- const encryptedText = encryptRsa.encrypt({
51
- text: 'hello world',
52
- privateKey,
53
- });
54
- console.log(encryptedText);
172
+ // Decrypting credentials on the server
173
+ const decryptedCredentials = nodeRSA.decryptStringWithRsaPrivateKey({ text: encryptedCredentials, privateKey: serverPrivateKey });
174
+ console.log('Decrypted Credentials:', decryptedCredentials);
55
175
  ```
56
176
 
57
- ### Decrypt Encrypted Text
177
+ #### Digital Signatures
178
+
179
+ Although not directly covered by the current implementation, RSA can also be used for creating and verifying digital signatures to ensure data integrity and authenticity.
180
+
58
181
 
59
- ```js
60
- const decryptedText = nodeRSA.decrypt({
61
- text: encryptedText,
62
- publicKey
63
- });
64
- console.log(decryptedText);
65
- // hello world
182
+ ## Contribution
183
+ We welcome contributions to the NodeRSA library! If you'd like to contribute, please follow these steps:
184
+
185
+ 1. Fork the repository on GitHub.
186
+ 2. Clone your forked repository to your local machine.
187
+ ```bash
188
+ git clone git@github.com:miladezzat/encrypt-rsa.git
66
189
  ```
190
+ 3. Create a new branch for your feature or bugfix.
191
+ ```bash
192
+ git checkout -b feature/your-feature-name
193
+ ```
194
+ 4. Make your changes to the codebase.
195
+ 5. Commit your changes with a clear and descriptive commit message.
196
+ ```bash
197
+ git commit -m "Description of your feature or fix"
198
+ ```
199
+ 6. Push your changes to your forked repository.
200
+ ```bash
201
+ git push origin feature/your-feature-name
202
+ ```
203
+ 7. Create a pull request on the original repository. Be sure to include a detailed description of your changes and the problem they solve.
204
+
205
+ ## Code of Conduct
206
+ Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
67
207
 
68
- ## Rsa Algorithm:
69
- - [RSA Algorithm](https://simple.wikipedia.org/wiki/RSA_algorithm)
70
208
 
209
+ ## Reporting Issues
210
+ If you encounter any issues, please report them using the GitHub issue tracker. Include details about the problem and your environment (OS, Node.js version, etc.).
71
211
 
72
- ### Contributions
73
- feel free to open an [issue](https://github.com/miladezzat/encrypt-rsa/issues) and make a [pull request](https://github.com/miladezzat/encrypt-rsa/pulls)
212
+ Thank you for contributing to NodeRSA!
@@ -0,0 +1,2 @@
1
+ export declare function convertKetToBase64(key: string): string;
2
+ export default convertKetToBase64;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertKetToBase64 = void 0;
4
+ var helpers_1 = require("../utils/helpers");
5
+ function convertKetToBase64(key) {
6
+ return (0, helpers_1.encode)(key.replace(/^ +/gm, ''));
7
+ }
8
+ exports.convertKetToBase64 = convertKetToBase64;
9
+ exports.default = convertKetToBase64;
@@ -0,0 +1,3 @@
1
+ import { returnCreateKeys } from '../utils/types';
2
+ export declare function createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
3
+ export default createPrivateAndPublicKeys;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createPrivateAndPublicKeys = void 0;
27
+ var crypto = __importStar(require("crypto"));
28
+ function createPrivateAndPublicKeys(modulusLength) {
29
+ if (modulusLength === void 0) { modulusLength = 2048; }
30
+ if (typeof crypto.generateKeyPairSync === 'function') {
31
+ var _a = crypto.generateKeyPairSync('rsa', {
32
+ modulusLength: modulusLength,
33
+ publicKeyEncoding: {
34
+ type: 'spki',
35
+ format: 'pem',
36
+ },
37
+ privateKeyEncoding: {
38
+ type: 'pkcs8',
39
+ format: 'pem',
40
+ },
41
+ }), privateKey = _a.privateKey, publicKey = _a.publicKey;
42
+ return { publicKey: publicKey, privateKey: privateKey };
43
+ }
44
+ return { privateKey: '', publicKey: '' };
45
+ }
46
+ exports.createPrivateAndPublicKeys = createPrivateAndPublicKeys;
47
+ exports.default = createPrivateAndPublicKeys;
@@ -0,0 +1,3 @@
1
+ import { parametersOfDecryptPublic } from '../utils/types';
2
+ export declare function decrypt(args: parametersOfDecryptPublic): string;
3
+ export default decrypt;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.decrypt = void 0;
27
+ var crypto = __importStar(require("crypto"));
28
+ var helpers_1 = require("../utils/helpers");
29
+ function decrypt(args) {
30
+ var text = args.text, publicKey = args.publicKey;
31
+ var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
32
+ var buffer = Buffer.from(text, 'base64');
33
+ var decrypted = crypto.publicDecrypt(publicKeyDecoded, buffer);
34
+ return decrypted.toString('utf8');
35
+ }
36
+ exports.decrypt = decrypt;
37
+ exports.default = decrypt;
@@ -0,0 +1,3 @@
1
+ import { parametersOfDecrypt } from '../utils/types';
2
+ export declare function decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
3
+ export default decryptStringWithRsaPrivateKey;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.decryptStringWithRsaPrivateKey = void 0;
27
+ var crypto = __importStar(require("crypto"));
28
+ var helpers_1 = require("../utils/helpers");
29
+ function decryptStringWithRsaPrivateKey(args) {
30
+ var text = args.text, privateKey = args.privateKey;
31
+ var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
32
+ var buffer = Buffer.from(text, 'base64');
33
+ var decrypted = crypto.privateDecrypt(privateKeyDecoded, buffer);
34
+ return decrypted.toString('utf8');
35
+ }
36
+ exports.decryptStringWithRsaPrivateKey = decryptStringWithRsaPrivateKey;
37
+ exports.default = decryptStringWithRsaPrivateKey;
@@ -0,0 +1,3 @@
1
+ import { parametersOfEncryptPrivate } from '../utils/types';
2
+ export declare function encrypt(args: parametersOfEncryptPrivate): string;
3
+ export default encrypt;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.encrypt = void 0;
27
+ var crypto = __importStar(require("crypto"));
28
+ var helpers_1 = require("../utils/helpers");
29
+ function encrypt(args) {
30
+ var text = args.text, privateKey = args.privateKey;
31
+ var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
32
+ var buffer = Buffer.from(text);
33
+ var encrypted = crypto.privateEncrypt(privateKeyDecoded, buffer);
34
+ return encrypted.toString('base64');
35
+ }
36
+ exports.encrypt = encrypt;
37
+ exports.default = encrypt;
@@ -0,0 +1,3 @@
1
+ import { parametersOfEncrypt } from '../utils/types';
2
+ export declare function encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
3
+ export default encryptStringWithRsaPublicKey;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.encryptStringWithRsaPublicKey = void 0;
27
+ var crypto = __importStar(require("crypto"));
28
+ var helpers_1 = require("../utils/helpers");
29
+ function encryptStringWithRsaPublicKey(args) {
30
+ var text = args.text, publicKey = args.publicKey;
31
+ var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
32
+ var buffer = Buffer === null || Buffer === void 0 ? void 0 : Buffer.from(text);
33
+ var encrypted = crypto === null || crypto === void 0 ? void 0 : crypto.publicEncrypt(publicKeyDecoded, buffer);
34
+ return encrypted.toString('base64');
35
+ }
36
+ exports.encryptStringWithRsaPublicKey = encryptStringWithRsaPublicKey;
37
+ exports.default = encryptStringWithRsaPublicKey;
@@ -0,0 +1,5 @@
1
+ export * from './createPrivateAndPublicKeys';
2
+ export * from './decrypt';
3
+ export * from './decryptStringWithRsaPrivateKey';
4
+ export * from './encrypt';
5
+ export * from './encryptStringWithRsaPublicKey';
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./createPrivateAndPublicKeys"), exports);
18
+ __exportStar(require("./decrypt"), exports);
19
+ __exportStar(require("./decryptStringWithRsaPrivateKey"), exports);
20
+ __exportStar(require("./encrypt"), exports);
21
+ __exportStar(require("./encryptStringWithRsaPublicKey"), exports);
package/build/index.d.ts CHANGED
@@ -1,52 +1,14 @@
1
- import { parametersOfDecrypt, parametersOfEncrypt, returnCreateKeys } from './utils/types';
1
+ import { parametersOfDecrypt, parametersOfDecryptPublic, parametersOfEncrypt, parametersOfEncryptPrivate, returnCreateKeys } from './utils/types';
2
2
  declare class NodeRSA {
3
3
  private publicKey;
4
4
  private privateKey;
5
5
  private modulusLength;
6
6
  private keyBase64;
7
- /**
8
- *
9
- * @param publicKey
10
- * @param privateKey
11
- */
12
7
  constructor(publicKey?: string, privateKey?: string, modulusLength?: number);
13
- /**
14
- *
15
- * @param {Object} args
16
- * @param {String} args.privateKey
17
- * @param {String} args.text the text that you need to encrypt
18
- * @deprecated
19
- * @returns {String}
20
- */
21
8
  encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
22
- /**
23
- *
24
- * @param {Object} args
25
- * @param {String} args.privateKey
26
- * @param {String} args.text the text that you need to decrypt
27
- * @deprecated
28
- * @returns {String}
29
- */
30
9
  decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
31
- /**
32
- *
33
- * @param {Object} args
34
- * @param {String} args.privateKey
35
- * @param {String} args.text the text that you need to encrypt
36
- *
37
- * @returns {String}
38
- */
39
- encrypt(args: parametersOfEncrypt): string;
40
- /**
41
- *
42
- * @param {Object} args
43
- * @param {String} args.privateKey
44
- * @param {String} args.text the text that you need to decrypt
45
- *
46
- * @returns {String}
47
- */
48
- decrypt(args: parametersOfDecrypt): string;
10
+ encrypt(args: parametersOfEncryptPrivate): string;
11
+ decrypt(args: parametersOfDecryptPublic): string;
49
12
  createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
50
- private convertKetToBase64;
51
13
  }
52
14
  export default NodeRSA;
package/build/index.js CHANGED
@@ -1,122 +1,51 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
15
  };
25
16
  Object.defineProperty(exports, "__esModule", { value: true });
26
- var crypto = __importStar(require("crypto"));
27
- var helpers_1 = require("./utils/helpers");
17
+ var convertKetToBase64_1 = __importDefault(require("./functions/convertKetToBase64"));
18
+ var createPrivateAndPublicKeys_1 = __importDefault(require("./functions/createPrivateAndPublicKeys"));
19
+ var decrypt_1 = __importDefault(require("./functions/decrypt"));
20
+ var decryptStringWithRsaPrivateKey_1 = __importDefault(require("./functions/decryptStringWithRsaPrivateKey"));
21
+ var encrypt_1 = __importDefault(require("./functions/encrypt"));
22
+ var encryptStringWithRsaPublicKey_1 = __importDefault(require("./functions/encryptStringWithRsaPublicKey"));
28
23
  var NodeRSA = /** @class */ (function () {
29
- /**
30
- *
31
- * @param publicKey
32
- * @param privateKey
33
- */
34
24
  function NodeRSA(publicKey, privateKey, modulusLength) {
35
25
  this.publicKey = publicKey;
36
26
  this.privateKey = privateKey;
37
27
  this.modulusLength = modulusLength || 2048;
38
- this.keyBase64 = '';
28
+ this.keyBase64 = 'base64';
39
29
  }
40
- /**
41
- *
42
- * @param {Object} args
43
- * @param {String} args.privateKey
44
- * @param {String} args.text the text that you need to encrypt
45
- * @deprecated
46
- * @returns {String}
47
- */
48
30
  NodeRSA.prototype.encryptStringWithRsaPublicKey = function (args) {
49
- var text = args.text, _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
50
- var publicKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(privateKey));
51
- var buffer = Buffer.from(text);
52
- var encrypted = crypto === null || crypto === void 0 ? void 0 : crypto.publicEncrypt(publicKeyDecoded, buffer);
53
- return encrypted.toString('base64');
31
+ var _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
32
+ return (0, encryptStringWithRsaPublicKey_1.default)(__assign(__assign({}, args), { publicKey: (0, convertKetToBase64_1.default)(publicKey) }));
54
33
  };
55
- /**
56
- *
57
- * @param {Object} args
58
- * @param {String} args.privateKey
59
- * @param {String} args.text the text that you need to decrypt
60
- * @deprecated
61
- * @returns {String}
62
- */
63
34
  NodeRSA.prototype.decryptStringWithRsaPrivateKey = function (args) {
64
- var text = args.text, _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
65
- var publicKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(publicKey));
66
- var buffer = Buffer.from(text, 'base64');
67
- var decrypted = crypto === null || crypto === void 0 ? void 0 : crypto.privateDecrypt(publicKeyDecoded, buffer);
68
- return decrypted.toString('utf8');
35
+ var _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
36
+ return (0, decryptStringWithRsaPrivateKey_1.default)(__assign(__assign({}, args), { privateKey: (0, convertKetToBase64_1.default)(privateKey) }));
69
37
  };
70
- /**
71
- *
72
- * @param {Object} args
73
- * @param {String} args.privateKey
74
- * @param {String} args.text the text that you need to encrypt
75
- *
76
- * @returns {String}
77
- */
78
38
  NodeRSA.prototype.encrypt = function (args) {
79
- var text = args.text, _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
80
- var publicKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(privateKey));
81
- var buffer = Buffer.from(text);
82
- var encrypted = crypto === null || crypto === void 0 ? void 0 : crypto.privateEncrypt(publicKeyDecoded, buffer);
83
- return encrypted.toString('base64');
39
+ var _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
40
+ return (0, encrypt_1.default)(__assign(__assign({}, args), { privateKey: (0, convertKetToBase64_1.default)(privateKey) }));
84
41
  };
85
- /**
86
- *
87
- * @param {Object} args
88
- * @param {String} args.privateKey
89
- * @param {String} args.text the text that you need to decrypt
90
- *
91
- * @returns {String}
92
- */
93
42
  NodeRSA.prototype.decrypt = function (args) {
94
- var text = args.text, _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
95
- var publicKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(publicKey));
96
- var buffer = Buffer.from(text, 'base64');
97
- var decrypted = crypto === null || crypto === void 0 ? void 0 : crypto.publicDecrypt(publicKeyDecoded, buffer);
98
- return decrypted.toString('utf8');
43
+ var _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
44
+ return (0, decrypt_1.default)(__assign(__assign({}, args), { publicKey: (0, convertKetToBase64_1.default)(publicKey) }));
99
45
  };
100
46
  NodeRSA.prototype.createPrivateAndPublicKeys = function (modulusLength) {
101
47
  if (modulusLength === void 0) { modulusLength = this.modulusLength; }
102
- if (typeof crypto.generateKeyPairSync === 'function') {
103
- var _a = crypto.generateKeyPairSync('rsa', {
104
- modulusLength: modulusLength,
105
- publicKeyEncoding: {
106
- type: 'spki',
107
- format: 'pem',
108
- },
109
- privateKeyEncoding: {
110
- type: 'pkcs8',
111
- format: 'pem',
112
- },
113
- }), privateKey = _a.privateKey, publicKey = _a.publicKey;
114
- return { publicKey: publicKey, privateKey: privateKey }; // Corrected order
115
- }
116
- return { privateKey: '', publicKey: '' };
117
- };
118
- NodeRSA.prototype.convertKetToBase64 = function (key) {
119
- return (0, helpers_1.encode)(key.replace(/^ +/gm, '') || this.keyBase64);
48
+ return (0, createPrivateAndPublicKeys_1.default)(modulusLength);
120
49
  };
121
50
  return NodeRSA;
122
51
  }());
@@ -1,7 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.encode = exports.decode = void 0;
4
- var decode = function (str) { return Buffer.from(str, 'base64').toString('utf-8'); };
4
+ var decode = function (str) {
5
+ try {
6
+ return Buffer.from(str, 'base64').toString('utf-8');
7
+ }
8
+ catch (error) {
9
+ throw new Error('Failed to decode base64 string');
10
+ }
11
+ };
5
12
  exports.decode = decode;
6
- var encode = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); };
13
+ var encode = function (str) {
14
+ try {
15
+ return Buffer.from(str, 'utf-8').toString('base64');
16
+ }
17
+ catch (error) {
18
+ throw new Error('Failed to encode string to base64');
19
+ }
20
+ };
7
21
  exports.encode = encode;
@@ -1,12 +1,20 @@
1
- export declare type returnCreateKeys = {
1
+ export type returnCreateKeys = {
2
2
  privateKey: string;
3
3
  publicKey: string;
4
4
  };
5
- export declare type parametersOfEncrypt = {
5
+ export type parametersOfEncrypt = {
6
+ text: string;
7
+ publicKey?: string;
8
+ };
9
+ export type parametersOfDecrypt = {
10
+ text: string;
11
+ privateKey?: string;
12
+ };
13
+ export type parametersOfEncryptPrivate = {
6
14
  text: string;
7
15
  privateKey?: string;
8
16
  };
9
- export declare type parametersOfDecrypt = {
17
+ export type parametersOfDecryptPublic = {
10
18
  text: string;
11
19
  publicKey?: string;
12
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "encrypt-rsa",
3
- "version": "3.0.1",
3
+ "version": "3.1.1",
4
4
  "description": "This is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)",
5
5
  "main": "./build/index.js",
6
6
  "module": "./build/index.js",
@@ -11,7 +11,7 @@
11
11
  "source": "./src/index.ts",
12
12
  "scripts": {
13
13
  "test": "mocha -r ts-node/register tests/**/*.spec.ts",
14
- "depcheck": "npm-check",
14
+ "depcheck": "npm-check -u",
15
15
  "prepare": "husky install",
16
16
  "build": "tsc",
17
17
  "commit": "git-cz",
@@ -26,8 +26,8 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/chai": "^4.2.21",
29
- "@types/mocha": "^9.1.1",
30
- "@types/node": "^17.0.38",
29
+ "@types/mocha": "^10.0.7",
30
+ "@types/node": "^20.14.11",
31
31
  "@typescript-eslint/eslint-plugin": "^5.27.0",
32
32
  "@typescript-eslint/parser": "^5.27.0",
33
33
  "chai": "^4.3.4",
@@ -41,9 +41,10 @@
41
41
  "git-cz": "^4.7.6",
42
42
  "husky": "^8.0.1",
43
43
  "mocha": "^10.0.0",
44
+ "npm-check": "^6.0.1",
44
45
  "pinst": "^3.0.0",
45
- "ts-node": "^10.1.0",
46
46
  "standard-version": "^9.5.0",
47
+ "ts-node": "^10.1.0",
47
48
  "typescript": "^4.3.5"
48
49
  },
49
50
  "keywords": [
@@ -54,6 +55,7 @@
54
55
  "private key",
55
56
  "rsa",
56
57
  "node-rsa",
58
+ "encrypt-rsa",
57
59
  "encrypt",
58
60
  "encrypt by public key",
59
61
  "decrypt by private key",
@@ -79,8 +81,5 @@
79
81
  "bugs": {
80
82
  "url": "https://github.com/miladezzat/encrypt-rsa/issues"
81
83
  },
82
- "homepage": "https://encrypt-rsa.js.org",
83
- "dependencies": {
84
- "npm-check": "^6.0.1"
85
- }
84
+ "homepage": "https://encrypt-rsa.js.org"
86
85
  }