encrypt-rsa 3.1.1 → 3.3.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 +25 -0
- package/build/functions/convertKetToBase64.d.ts +9 -0
- package/build/functions/convertKetToBase64.js +9 -0
- package/build/functions/createPrivateAndPublicKeys.d.ts +14 -0
- package/build/functions/createPrivateAndPublicKeys.js +14 -0
- package/build/functions/decrypt.d.ts +16 -0
- package/build/functions/decrypt.js +16 -0
- package/build/functions/decryptStringWithRsaPrivateKey.d.ts +15 -0
- package/build/functions/decryptStringWithRsaPrivateKey.js +15 -0
- package/build/functions/encrypt.d.ts +16 -0
- package/build/functions/encrypt.js +16 -0
- package/build/functions/encryptStringWithRsaPublicKey.d.ts +16 -0
- package/build/functions/encryptStringWithRsaPublicKey.js +16 -0
- package/build/functions/index.d.ts +5 -0
- package/build/functions/index.js +14 -0
- package/build/index.d.ts +78 -0
- package/build/index.js +63 -0
- package/build/utils/helpers.d.ts +14 -0
- package/build/utils/helpers.js +14 -0
- package/build/utils/types.d.ts +35 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -174,6 +174,31 @@ const decryptedCredentials = nodeRSA.decryptStringWithRsaPrivateKey({ text: encr
|
|
|
174
174
|
console.log('Decrypted Credentials:', decryptedCredentials);
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
+
### Buffer Encryption/Decryption Methods:
|
|
178
|
+
|
|
179
|
+
1. `encryptBufferWithRsaPublicKey`: Converts a buffer to Base64 and then encrypts the Base64 string.
|
|
180
|
+
2. `decryptBufferWithRsaPrivateKey`: Decrypts the Base64 string and converts it back to a buffer.
|
|
181
|
+
|
|
182
|
+
#### Example Usage
|
|
183
|
+
```ts
|
|
184
|
+
const nodeRSA = new NodeRSA();
|
|
185
|
+
|
|
186
|
+
// Generate keys
|
|
187
|
+
const { publicKey, privateKey } = nodeRSA.createPrivateAndPublicKeys();
|
|
188
|
+
|
|
189
|
+
// Example buffer
|
|
190
|
+
const buffer = Buffer.from('This is some binary data');
|
|
191
|
+
|
|
192
|
+
// Encrypt the buffer
|
|
193
|
+
const encryptedBuffer = nodeRSA.encryptBufferWithRsaPublicKey(buffer, publicKey);
|
|
194
|
+
console.log('Encrypted Buffer:', encryptedBuffer);
|
|
195
|
+
|
|
196
|
+
// Decrypt back to buffer
|
|
197
|
+
const decryptedBuffer = nodeRSA.decryptBufferWithRsaPrivateKey(encryptedBuffer, privateKey);
|
|
198
|
+
console.log('Decrypted Buffer:', decryptedBuffer.toString()); // should log: 'This is some binary data'
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
|
|
177
202
|
#### Digital Signatures
|
|
178
203
|
|
|
179
204
|
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.
|
|
@@ -1,2 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an RSA key to a base64-encoded string.
|
|
3
|
+
*
|
|
4
|
+
* This function removes any leading spaces from each line of the key and then
|
|
5
|
+
* encodes it to base64 format.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} key - The RSA key to be converted to base64. It may contain leading spaces in each line.
|
|
8
|
+
* @returns {string} The base64-encoded version of the RSA key.
|
|
9
|
+
*/
|
|
1
10
|
export declare function convertKetToBase64(key: string): string;
|
|
2
11
|
export default convertKetToBase64;
|
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertKetToBase64 = void 0;
|
|
4
4
|
var helpers_1 = require("../utils/helpers");
|
|
5
|
+
/**
|
|
6
|
+
* Converts an RSA key to a base64-encoded string.
|
|
7
|
+
*
|
|
8
|
+
* This function removes any leading spaces from each line of the key and then
|
|
9
|
+
* encodes it to base64 format.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} key - The RSA key to be converted to base64. It may contain leading spaces in each line.
|
|
12
|
+
* @returns {string} The base64-encoded version of the RSA key.
|
|
13
|
+
*/
|
|
5
14
|
function convertKetToBase64(key) {
|
|
6
15
|
return (0, helpers_1.encode)(key.replace(/^ +/gm, ''));
|
|
7
16
|
}
|
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
import { returnCreateKeys } from '../utils/types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates a pair of RSA private and public keys with the specified modulus length.
|
|
4
|
+
*
|
|
5
|
+
* If the `crypto.generateKeyPairSync` function is available, it generates the keys using the provided modulus length,
|
|
6
|
+
* otherwise, it returns empty strings for both keys.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} [modulusLength=2048] - The length of the RSA modulus in bits. Defaults to 2048 bits.
|
|
9
|
+
* @returns {returnCreateKeys} An object containing the generated RSA public and private keys in PEM format.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const { publicKey, privateKey } = createPrivateAndPublicKeys(4096);
|
|
13
|
+
* console.log(publicKey); // Outputs the public key in PEM format
|
|
14
|
+
* console.log(privateKey); // Outputs the private key in PEM format
|
|
15
|
+
*/
|
|
2
16
|
export declare function createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
|
|
3
17
|
export default createPrivateAndPublicKeys;
|
|
@@ -25,6 +25,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.createPrivateAndPublicKeys = void 0;
|
|
27
27
|
var crypto = __importStar(require("crypto"));
|
|
28
|
+
/**
|
|
29
|
+
* Generates a pair of RSA private and public keys with the specified modulus length.
|
|
30
|
+
*
|
|
31
|
+
* If the `crypto.generateKeyPairSync` function is available, it generates the keys using the provided modulus length,
|
|
32
|
+
* otherwise, it returns empty strings for both keys.
|
|
33
|
+
*
|
|
34
|
+
* @param {number} [modulusLength=2048] - The length of the RSA modulus in bits. Defaults to 2048 bits.
|
|
35
|
+
* @returns {returnCreateKeys} An object containing the generated RSA public and private keys in PEM format.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const { publicKey, privateKey } = createPrivateAndPublicKeys(4096);
|
|
39
|
+
* console.log(publicKey); // Outputs the public key in PEM format
|
|
40
|
+
* console.log(privateKey); // Outputs the private key in PEM format
|
|
41
|
+
*/
|
|
28
42
|
function createPrivateAndPublicKeys(modulusLength) {
|
|
29
43
|
if (modulusLength === void 0) { modulusLength = 2048; }
|
|
30
44
|
if (typeof crypto.generateKeyPairSync === 'function') {
|
|
@@ -1,3 +1,19 @@
|
|
|
1
1
|
import { parametersOfDecryptPublic } from '../utils/types';
|
|
2
|
+
/**
|
|
3
|
+
* Decrypts a base64-encoded string using an RSA public key.
|
|
4
|
+
*
|
|
5
|
+
* The function first decodes the provided RSA public key and the base64-encoded text,
|
|
6
|
+
* then decrypts the text using the RSA public key.
|
|
7
|
+
*
|
|
8
|
+
* @param {parametersOfDecryptPublic} args - An object containing the text to decrypt and the RSA public key.
|
|
9
|
+
* @param {string} args.text - The base64-encoded string to be decrypted.
|
|
10
|
+
* @param {string} [args.publicKey] - The RSA public key to use for decryption. If not provided, a default key may be used.
|
|
11
|
+
* @returns {string} The decrypted string in UTF-8 format.
|
|
12
|
+
* @throws {Error} If decryption fails due to invalid key or input.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const decryptedText = decrypt({ text: 'base64EncryptedText', publicKey: 'publicKeyString' });
|
|
16
|
+
* console.log(decryptedText); // Outputs the decrypted string in UTF-8 format
|
|
17
|
+
*/
|
|
2
18
|
export declare function decrypt(args: parametersOfDecryptPublic): string;
|
|
3
19
|
export default decrypt;
|
|
@@ -26,6 +26,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.decrypt = void 0;
|
|
27
27
|
var crypto = __importStar(require("crypto"));
|
|
28
28
|
var helpers_1 = require("../utils/helpers");
|
|
29
|
+
/**
|
|
30
|
+
* Decrypts a base64-encoded string using an RSA public key.
|
|
31
|
+
*
|
|
32
|
+
* The function first decodes the provided RSA public key and the base64-encoded text,
|
|
33
|
+
* then decrypts the text using the RSA public key.
|
|
34
|
+
*
|
|
35
|
+
* @param {parametersOfDecryptPublic} args - An object containing the text to decrypt and the RSA public key.
|
|
36
|
+
* @param {string} args.text - The base64-encoded string to be decrypted.
|
|
37
|
+
* @param {string} [args.publicKey] - The RSA public key to use for decryption. If not provided, a default key may be used.
|
|
38
|
+
* @returns {string} The decrypted string in UTF-8 format.
|
|
39
|
+
* @throws {Error} If decryption fails due to invalid key or input.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const decryptedText = decrypt({ text: 'base64EncryptedText', publicKey: 'publicKeyString' });
|
|
43
|
+
* console.log(decryptedText); // Outputs the decrypted string in UTF-8 format
|
|
44
|
+
*/
|
|
29
45
|
function decrypt(args) {
|
|
30
46
|
var text = args.text, publicKey = args.publicKey;
|
|
31
47
|
var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
|
|
@@ -1,3 +1,18 @@
|
|
|
1
1
|
import { parametersOfDecrypt } from '../utils/types';
|
|
2
|
+
/**
|
|
3
|
+
* Decrypts a base64-encoded string using an RSA private key.
|
|
4
|
+
*
|
|
5
|
+
* This function decodes the provided RSA private key, then uses it to decrypt the base64-encoded string.
|
|
6
|
+
*
|
|
7
|
+
* @param {parametersOfDecrypt} args - An object containing the text to decrypt and the RSA private key.
|
|
8
|
+
* @param {string} args.text - The base64-encoded string to be decrypted.
|
|
9
|
+
* @param {string} [args.privateKey] - The RSA private key to use for decryption. If not provided, a default key may be used.
|
|
10
|
+
* @returns {string} The decrypted string in UTF-8 format.
|
|
11
|
+
* @throws {Error} If decryption fails due to invalid key or input.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const decryptedText = decryptStringWithRsaPrivateKey({ text: 'base64EncryptedText', privateKey: 'privateKeyString' });
|
|
15
|
+
* console.log(decryptedText); // Outputs the decrypted string in UTF-8 format
|
|
16
|
+
*/
|
|
2
17
|
export declare function decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
|
|
3
18
|
export default decryptStringWithRsaPrivateKey;
|
|
@@ -26,6 +26,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.decryptStringWithRsaPrivateKey = void 0;
|
|
27
27
|
var crypto = __importStar(require("crypto"));
|
|
28
28
|
var helpers_1 = require("../utils/helpers");
|
|
29
|
+
/**
|
|
30
|
+
* Decrypts a base64-encoded string using an RSA private key.
|
|
31
|
+
*
|
|
32
|
+
* This function decodes the provided RSA private key, then uses it to decrypt the base64-encoded string.
|
|
33
|
+
*
|
|
34
|
+
* @param {parametersOfDecrypt} args - An object containing the text to decrypt and the RSA private key.
|
|
35
|
+
* @param {string} args.text - The base64-encoded string to be decrypted.
|
|
36
|
+
* @param {string} [args.privateKey] - The RSA private key to use for decryption. If not provided, a default key may be used.
|
|
37
|
+
* @returns {string} The decrypted string in UTF-8 format.
|
|
38
|
+
* @throws {Error} If decryption fails due to invalid key or input.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const decryptedText = decryptStringWithRsaPrivateKey({ text: 'base64EncryptedText', privateKey: 'privateKeyString' });
|
|
42
|
+
* console.log(decryptedText); // Outputs the decrypted string in UTF-8 format
|
|
43
|
+
*/
|
|
29
44
|
function decryptStringWithRsaPrivateKey(args) {
|
|
30
45
|
var text = args.text, privateKey = args.privateKey;
|
|
31
46
|
var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
|
|
@@ -1,3 +1,19 @@
|
|
|
1
1
|
import { parametersOfEncryptPrivate } from '../utils/types';
|
|
2
|
+
/**
|
|
3
|
+
* Encrypts a string using an RSA private key.
|
|
4
|
+
*
|
|
5
|
+
* This function encodes the provided RSA private key, converts the input text to a buffer,
|
|
6
|
+
* and encrypts it using the private key. The result is returned as a base64-encoded string.
|
|
7
|
+
*
|
|
8
|
+
* @param {parametersOfEncryptPrivate} args - An object containing the text to encrypt and the RSA private key.
|
|
9
|
+
* @param {string} args.text - The plain text to be encrypted.
|
|
10
|
+
* @param {string} [args.privateKey] - The RSA private key to use for encryption. If not provided, a default key may be used.
|
|
11
|
+
* @returns {string} The encrypted string in base64 format.
|
|
12
|
+
* @throws {Error} If encryption fails due to invalid key or input.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const encryptedText = encrypt({ text: 'Hello, World!', privateKey: 'privateKeyString' });
|
|
16
|
+
* console.log(encryptedText); // Outputs the encrypted string in base64 format
|
|
17
|
+
*/
|
|
2
18
|
export declare function encrypt(args: parametersOfEncryptPrivate): string;
|
|
3
19
|
export default encrypt;
|
|
@@ -26,6 +26,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.encrypt = void 0;
|
|
27
27
|
var crypto = __importStar(require("crypto"));
|
|
28
28
|
var helpers_1 = require("../utils/helpers");
|
|
29
|
+
/**
|
|
30
|
+
* Encrypts a string using an RSA private key.
|
|
31
|
+
*
|
|
32
|
+
* This function encodes the provided RSA private key, converts the input text to a buffer,
|
|
33
|
+
* and encrypts it using the private key. The result is returned as a base64-encoded string.
|
|
34
|
+
*
|
|
35
|
+
* @param {parametersOfEncryptPrivate} args - An object containing the text to encrypt and the RSA private key.
|
|
36
|
+
* @param {string} args.text - The plain text to be encrypted.
|
|
37
|
+
* @param {string} [args.privateKey] - The RSA private key to use for encryption. If not provided, a default key may be used.
|
|
38
|
+
* @returns {string} The encrypted string in base64 format.
|
|
39
|
+
* @throws {Error} If encryption fails due to invalid key or input.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const encryptedText = encrypt({ text: 'Hello, World!', privateKey: 'privateKeyString' });
|
|
43
|
+
* console.log(encryptedText); // Outputs the encrypted string in base64 format
|
|
44
|
+
*/
|
|
29
45
|
function encrypt(args) {
|
|
30
46
|
var text = args.text, privateKey = args.privateKey;
|
|
31
47
|
var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
|
|
@@ -1,3 +1,19 @@
|
|
|
1
1
|
import { parametersOfEncrypt } from '../utils/types';
|
|
2
|
+
/**
|
|
3
|
+
* Encrypts a string using an RSA public key.
|
|
4
|
+
*
|
|
5
|
+
* This function decodes the provided RSA public key, converts the input text into a buffer,
|
|
6
|
+
* and encrypts it using the public key. The encrypted result is returned as a base64-encoded string.
|
|
7
|
+
*
|
|
8
|
+
* @param {parametersOfEncrypt} args - An object containing the text to encrypt and the RSA public key.
|
|
9
|
+
* @param {string} args.text - The plain text to be encrypted.
|
|
10
|
+
* @param {string} [args.publicKey] - The RSA public key to use for encryption. If not provided, a default key may be used.
|
|
11
|
+
* @returns {string} The encrypted string in base64 format.
|
|
12
|
+
* @throws {Error} If encryption fails due to invalid key or input.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const encryptedText = encryptStringWithRsaPublicKey({ text: 'Hello, World!', publicKey: 'publicKeyString' });
|
|
16
|
+
* console.log(encryptedText); // Outputs the encrypted string in base64 format
|
|
17
|
+
*/
|
|
2
18
|
export declare function encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
|
|
3
19
|
export default encryptStringWithRsaPublicKey;
|
|
@@ -26,6 +26,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.encryptStringWithRsaPublicKey = void 0;
|
|
27
27
|
var crypto = __importStar(require("crypto"));
|
|
28
28
|
var helpers_1 = require("../utils/helpers");
|
|
29
|
+
/**
|
|
30
|
+
* Encrypts a string using an RSA public key.
|
|
31
|
+
*
|
|
32
|
+
* This function decodes the provided RSA public key, converts the input text into a buffer,
|
|
33
|
+
* and encrypts it using the public key. The encrypted result is returned as a base64-encoded string.
|
|
34
|
+
*
|
|
35
|
+
* @param {parametersOfEncrypt} args - An object containing the text to encrypt and the RSA public key.
|
|
36
|
+
* @param {string} args.text - The plain text to be encrypted.
|
|
37
|
+
* @param {string} [args.publicKey] - The RSA public key to use for encryption. If not provided, a default key may be used.
|
|
38
|
+
* @returns {string} The encrypted string in base64 format.
|
|
39
|
+
* @throws {Error} If encryption fails due to invalid key or input.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const encryptedText = encryptStringWithRsaPublicKey({ text: 'Hello, World!', publicKey: 'publicKeyString' });
|
|
43
|
+
* console.log(encryptedText); // Outputs the encrypted string in base64 format
|
|
44
|
+
*/
|
|
29
45
|
function encryptStringWithRsaPublicKey(args) {
|
|
30
46
|
var text = args.text, publicKey = args.publicKey;
|
|
31
47
|
var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export * from './createPrivateAndPublicKeys';
|
|
2
|
+
export { default as createPrivateAndPublicKeys } from './decryptStringWithRsaPrivateKey';
|
|
2
3
|
export * from './decrypt';
|
|
4
|
+
export { default as decrypt } from './encryptStringWithRsaPublicKey';
|
|
3
5
|
export * from './decryptStringWithRsaPrivateKey';
|
|
6
|
+
export { default as decryptStringWithRsaPrivateKey } from './decrypt';
|
|
4
7
|
export * from './encrypt';
|
|
8
|
+
export { default as encrypt } from './decryptStringWithRsaPrivateKey';
|
|
5
9
|
export * from './encryptStringWithRsaPublicKey';
|
|
10
|
+
export { default as encryptStringWithRsaPublicKey } from './encrypt';
|
package/build/functions/index.js
CHANGED
|
@@ -13,9 +13,23 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
16
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.encryptStringWithRsaPublicKey = exports.encrypt = exports.decryptStringWithRsaPrivateKey = exports.decrypt = exports.createPrivateAndPublicKeys = void 0;
|
|
17
21
|
__exportStar(require("./createPrivateAndPublicKeys"), exports);
|
|
22
|
+
var decryptStringWithRsaPrivateKey_1 = require("./decryptStringWithRsaPrivateKey");
|
|
23
|
+
Object.defineProperty(exports, "createPrivateAndPublicKeys", { enumerable: true, get: function () { return __importDefault(decryptStringWithRsaPrivateKey_1).default; } });
|
|
18
24
|
__exportStar(require("./decrypt"), exports);
|
|
25
|
+
var encryptStringWithRsaPublicKey_1 = require("./encryptStringWithRsaPublicKey");
|
|
26
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return __importDefault(encryptStringWithRsaPublicKey_1).default; } });
|
|
19
27
|
__exportStar(require("./decryptStringWithRsaPrivateKey"), exports);
|
|
28
|
+
var decrypt_1 = require("./decrypt");
|
|
29
|
+
Object.defineProperty(exports, "decryptStringWithRsaPrivateKey", { enumerable: true, get: function () { return __importDefault(decrypt_1).default; } });
|
|
20
30
|
__exportStar(require("./encrypt"), exports);
|
|
31
|
+
var decryptStringWithRsaPrivateKey_2 = require("./decryptStringWithRsaPrivateKey");
|
|
32
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return __importDefault(decryptStringWithRsaPrivateKey_2).default; } });
|
|
21
33
|
__exportStar(require("./encryptStringWithRsaPublicKey"), exports);
|
|
34
|
+
var encrypt_1 = require("./encrypt");
|
|
35
|
+
Object.defineProperty(exports, "encryptStringWithRsaPublicKey", { enumerable: true, get: function () { return __importDefault(encrypt_1).default; } });
|
package/build/index.d.ts
CHANGED
|
@@ -1,14 +1,92 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { parametersOfDecrypt, parametersOfDecryptPublic, parametersOfEncrypt, parametersOfEncryptPrivate, returnCreateKeys } from './utils/types';
|
|
3
|
+
/**
|
|
4
|
+
* NodeRSA class provides encryption and decryption methods using RSA keys.
|
|
5
|
+
* It supports string and buffer encryption/decryption with both public and private keys.
|
|
6
|
+
*/
|
|
2
7
|
declare class NodeRSA {
|
|
8
|
+
/**
|
|
9
|
+
* @private
|
|
10
|
+
* @type {string | undefined}
|
|
11
|
+
* Public key used for encryption.
|
|
12
|
+
*/
|
|
3
13
|
private publicKey;
|
|
14
|
+
/**
|
|
15
|
+
* @private
|
|
16
|
+
* @type {string | undefined}
|
|
17
|
+
* Private key used for decryption.
|
|
18
|
+
*/
|
|
4
19
|
private privateKey;
|
|
20
|
+
/**
|
|
21
|
+
* @private
|
|
22
|
+
* @type {number}
|
|
23
|
+
* Length of the RSA modulus, defaults to 2048 bits.
|
|
24
|
+
*/
|
|
5
25
|
private modulusLength;
|
|
26
|
+
/**
|
|
27
|
+
* @private
|
|
28
|
+
* @type {'base64'}
|
|
29
|
+
* Encoding format for the keys.
|
|
30
|
+
*/
|
|
6
31
|
private keyBase64;
|
|
32
|
+
/**
|
|
33
|
+
* Constructs a new instance of the NodeRSA class.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} [publicKey] - Optional public key for encryption.
|
|
36
|
+
* @param {string} [privateKey] - Optional private key for decryption.
|
|
37
|
+
* @param {number} [modulusLength=2048] - Length of the RSA modulus in bits.
|
|
38
|
+
*/
|
|
7
39
|
constructor(publicKey?: string, privateKey?: string, modulusLength?: number);
|
|
40
|
+
/**
|
|
41
|
+
* Encrypts a string using the RSA public key.
|
|
42
|
+
*
|
|
43
|
+
* @param {parametersOfEncrypt} args - Parameters for encryption, including the text and public key.
|
|
44
|
+
* @returns {string} Encrypted string in base64 format.
|
|
45
|
+
*/
|
|
8
46
|
encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
|
|
47
|
+
/**
|
|
48
|
+
* Decrypts a string using the RSA private key.
|
|
49
|
+
*
|
|
50
|
+
* @param {parametersOfDecrypt} args - Parameters for decryption, including the encrypted text and private key.
|
|
51
|
+
* @returns {string} Decrypted plain text string.
|
|
52
|
+
*/
|
|
9
53
|
decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
|
|
54
|
+
/**
|
|
55
|
+
* Encrypts a string using the RSA private key.
|
|
56
|
+
*
|
|
57
|
+
* @param {parametersOfEncryptPrivate} args - Parameters for encryption, including the text and private key.
|
|
58
|
+
* @returns {string} Encrypted string in base64 format.
|
|
59
|
+
*/
|
|
10
60
|
encrypt(args: parametersOfEncryptPrivate): string;
|
|
61
|
+
/**
|
|
62
|
+
* Decrypts a string using the RSA public key.
|
|
63
|
+
*
|
|
64
|
+
* @param {parametersOfDecryptPublic} args - Parameters for decryption, including the encrypted text and public key.
|
|
65
|
+
* @returns {string} Decrypted plain text string.
|
|
66
|
+
*/
|
|
11
67
|
decrypt(args: parametersOfDecryptPublic): string;
|
|
68
|
+
/**
|
|
69
|
+
* Creates a pair of RSA private and public keys with the given modulus length.
|
|
70
|
+
*
|
|
71
|
+
* @param {number} [modulusLength=this.modulusLength] - Length of the RSA modulus in bits.
|
|
72
|
+
* @returns {returnCreateKeys} The generated RSA private and public keys.
|
|
73
|
+
*/
|
|
12
74
|
createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
|
|
75
|
+
/**
|
|
76
|
+
* Encrypts a buffer using the RSA public key.
|
|
77
|
+
*
|
|
78
|
+
* @param {Buffer} buffer - The buffer to encrypt.
|
|
79
|
+
* @param {string} [publicKey] - Optional public key for encryption.
|
|
80
|
+
* @returns {string} Encrypted buffer as a base64 string.
|
|
81
|
+
*/
|
|
82
|
+
encryptBufferWithRsaPublicKey(buffer: Buffer, publicKey?: string): string;
|
|
83
|
+
/**
|
|
84
|
+
* Decrypts a buffer using the RSA private key.
|
|
85
|
+
*
|
|
86
|
+
* @param {string} encryptedText - The encrypted base64 string to decrypt.
|
|
87
|
+
* @param {string} [privateKey] - Optional private key for decryption.
|
|
88
|
+
* @returns {Buffer} Decrypted buffer.
|
|
89
|
+
*/
|
|
90
|
+
decryptBufferWithRsaPrivateKey(encryptedText: string, privateKey?: string): Buffer;
|
|
13
91
|
}
|
|
14
92
|
export default NodeRSA;
|
package/build/index.js
CHANGED
|
@@ -20,33 +20,96 @@ var decrypt_1 = __importDefault(require("./functions/decrypt"));
|
|
|
20
20
|
var decryptStringWithRsaPrivateKey_1 = __importDefault(require("./functions/decryptStringWithRsaPrivateKey"));
|
|
21
21
|
var encrypt_1 = __importDefault(require("./functions/encrypt"));
|
|
22
22
|
var encryptStringWithRsaPublicKey_1 = __importDefault(require("./functions/encryptStringWithRsaPublicKey"));
|
|
23
|
+
/**
|
|
24
|
+
* NodeRSA class provides encryption and decryption methods using RSA keys.
|
|
25
|
+
* It supports string and buffer encryption/decryption with both public and private keys.
|
|
26
|
+
*/
|
|
23
27
|
var NodeRSA = /** @class */ (function () {
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a new instance of the NodeRSA class.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} [publicKey] - Optional public key for encryption.
|
|
32
|
+
* @param {string} [privateKey] - Optional private key for decryption.
|
|
33
|
+
* @param {number} [modulusLength=2048] - Length of the RSA modulus in bits.
|
|
34
|
+
*/
|
|
24
35
|
function NodeRSA(publicKey, privateKey, modulusLength) {
|
|
25
36
|
this.publicKey = publicKey;
|
|
26
37
|
this.privateKey = privateKey;
|
|
27
38
|
this.modulusLength = modulusLength || 2048;
|
|
28
39
|
this.keyBase64 = 'base64';
|
|
29
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Encrypts a string using the RSA public key.
|
|
43
|
+
*
|
|
44
|
+
* @param {parametersOfEncrypt} args - Parameters for encryption, including the text and public key.
|
|
45
|
+
* @returns {string} Encrypted string in base64 format.
|
|
46
|
+
*/
|
|
30
47
|
NodeRSA.prototype.encryptStringWithRsaPublicKey = function (args) {
|
|
31
48
|
var _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
|
|
32
49
|
return (0, encryptStringWithRsaPublicKey_1.default)(__assign(__assign({}, args), { publicKey: (0, convertKetToBase64_1.default)(publicKey) }));
|
|
33
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* Decrypts a string using the RSA private key.
|
|
53
|
+
*
|
|
54
|
+
* @param {parametersOfDecrypt} args - Parameters for decryption, including the encrypted text and private key.
|
|
55
|
+
* @returns {string} Decrypted plain text string.
|
|
56
|
+
*/
|
|
34
57
|
NodeRSA.prototype.decryptStringWithRsaPrivateKey = function (args) {
|
|
35
58
|
var _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
|
|
36
59
|
return (0, decryptStringWithRsaPrivateKey_1.default)(__assign(__assign({}, args), { privateKey: (0, convertKetToBase64_1.default)(privateKey) }));
|
|
37
60
|
};
|
|
61
|
+
/**
|
|
62
|
+
* Encrypts a string using the RSA private key.
|
|
63
|
+
*
|
|
64
|
+
* @param {parametersOfEncryptPrivate} args - Parameters for encryption, including the text and private key.
|
|
65
|
+
* @returns {string} Encrypted string in base64 format.
|
|
66
|
+
*/
|
|
38
67
|
NodeRSA.prototype.encrypt = function (args) {
|
|
39
68
|
var _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
|
|
40
69
|
return (0, encrypt_1.default)(__assign(__assign({}, args), { privateKey: (0, convertKetToBase64_1.default)(privateKey) }));
|
|
41
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Decrypts a string using the RSA public key.
|
|
73
|
+
*
|
|
74
|
+
* @param {parametersOfDecryptPublic} args - Parameters for decryption, including the encrypted text and public key.
|
|
75
|
+
* @returns {string} Decrypted plain text string.
|
|
76
|
+
*/
|
|
42
77
|
NodeRSA.prototype.decrypt = function (args) {
|
|
43
78
|
var _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
|
|
44
79
|
return (0, decrypt_1.default)(__assign(__assign({}, args), { publicKey: (0, convertKetToBase64_1.default)(publicKey) }));
|
|
45
80
|
};
|
|
81
|
+
/**
|
|
82
|
+
* Creates a pair of RSA private and public keys with the given modulus length.
|
|
83
|
+
*
|
|
84
|
+
* @param {number} [modulusLength=this.modulusLength] - Length of the RSA modulus in bits.
|
|
85
|
+
* @returns {returnCreateKeys} The generated RSA private and public keys.
|
|
86
|
+
*/
|
|
46
87
|
NodeRSA.prototype.createPrivateAndPublicKeys = function (modulusLength) {
|
|
47
88
|
if (modulusLength === void 0) { modulusLength = this.modulusLength; }
|
|
48
89
|
return (0, createPrivateAndPublicKeys_1.default)(modulusLength);
|
|
49
90
|
};
|
|
91
|
+
/**
|
|
92
|
+
* Encrypts a buffer using the RSA public key.
|
|
93
|
+
*
|
|
94
|
+
* @param {Buffer} buffer - The buffer to encrypt.
|
|
95
|
+
* @param {string} [publicKey] - Optional public key for encryption.
|
|
96
|
+
* @returns {string} Encrypted buffer as a base64 string.
|
|
97
|
+
*/
|
|
98
|
+
NodeRSA.prototype.encryptBufferWithRsaPublicKey = function (buffer, publicKey) {
|
|
99
|
+
var base64String = buffer.toString(this.keyBase64);
|
|
100
|
+
return this.encryptStringWithRsaPublicKey({ text: base64String, publicKey: publicKey });
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Decrypts a buffer using the RSA private key.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} encryptedText - The encrypted base64 string to decrypt.
|
|
106
|
+
* @param {string} [privateKey] - Optional private key for decryption.
|
|
107
|
+
* @returns {Buffer} Decrypted buffer.
|
|
108
|
+
*/
|
|
109
|
+
NodeRSA.prototype.decryptBufferWithRsaPrivateKey = function (encryptedText, privateKey) {
|
|
110
|
+
var decryptedBase64 = this.decryptStringWithRsaPrivateKey({ text: encryptedText, privateKey: privateKey });
|
|
111
|
+
return Buffer.from(decryptedBase64, this.keyBase64);
|
|
112
|
+
};
|
|
50
113
|
return NodeRSA;
|
|
51
114
|
}());
|
|
52
115
|
exports.default = NodeRSA;
|
package/build/utils/helpers.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes a base64-encoded string into a UTF-8 string.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} str - The base64-encoded string to decode.
|
|
5
|
+
* @returns {string} The decoded UTF-8 string.
|
|
6
|
+
* @throws {Error} If the input string cannot be decoded.
|
|
7
|
+
*/
|
|
1
8
|
export declare const decode: (str: string) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Encodes a UTF-8 string into a base64-encoded string.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} str - The UTF-8 string to encode.
|
|
13
|
+
* @returns {string} The base64-encoded string.
|
|
14
|
+
* @throws {Error} If the input string cannot be encoded.
|
|
15
|
+
*/
|
|
2
16
|
export declare const encode: (str: string) => string;
|
package/build/utils/helpers.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.encode = exports.decode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Decodes a base64-encoded string into a UTF-8 string.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} str - The base64-encoded string to decode.
|
|
8
|
+
* @returns {string} The decoded UTF-8 string.
|
|
9
|
+
* @throws {Error} If the input string cannot be decoded.
|
|
10
|
+
*/
|
|
4
11
|
var decode = function (str) {
|
|
5
12
|
try {
|
|
6
13
|
return Buffer.from(str, 'base64').toString('utf-8');
|
|
@@ -10,6 +17,13 @@ var decode = function (str) {
|
|
|
10
17
|
}
|
|
11
18
|
};
|
|
12
19
|
exports.decode = decode;
|
|
20
|
+
/**
|
|
21
|
+
* Encodes a UTF-8 string into a base64-encoded string.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} str - The UTF-8 string to encode.
|
|
24
|
+
* @returns {string} The base64-encoded string.
|
|
25
|
+
* @throws {Error} If the input string cannot be encoded.
|
|
26
|
+
*/
|
|
13
27
|
var encode = function (str) {
|
|
14
28
|
try {
|
|
15
29
|
return Buffer.from(str, 'utf-8').toString('base64');
|
package/build/utils/types.d.ts
CHANGED
|
@@ -1,19 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type representing the return value of a function that creates RSA keys.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {Object} returnCreateKeys
|
|
5
|
+
* @property {string} privateKey - The generated RSA private key.
|
|
6
|
+
* @property {string} publicKey - The generated RSA public key.
|
|
7
|
+
*/
|
|
1
8
|
export type returnCreateKeys = {
|
|
2
9
|
privateKey: string;
|
|
3
10
|
publicKey: string;
|
|
4
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Type representing the parameters required to encrypt text using a public key.
|
|
14
|
+
*
|
|
15
|
+
* @typedef {Object} parametersOfEncrypt
|
|
16
|
+
* @property {string} text - The plain text to be encrypted.
|
|
17
|
+
* @property {string} [publicKey] - Optional RSA public key for encryption. If not provided, a default key may be used.
|
|
18
|
+
*/
|
|
5
19
|
export type parametersOfEncrypt = {
|
|
6
20
|
text: string;
|
|
7
21
|
publicKey?: string;
|
|
8
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* Type representing the parameters required to decrypt text using a private key.
|
|
25
|
+
*
|
|
26
|
+
* @typedef {Object} parametersOfDecrypt
|
|
27
|
+
* @property {string} text - The base64-encoded string to be decrypted.
|
|
28
|
+
* @property {string} [privateKey] - Optional RSA private key for decryption. If not provided, a default key may be used.
|
|
29
|
+
*/
|
|
9
30
|
export type parametersOfDecrypt = {
|
|
10
31
|
text: string;
|
|
11
32
|
privateKey?: string;
|
|
12
33
|
};
|
|
34
|
+
/**
|
|
35
|
+
* Type representing the parameters required to encrypt text using a private key.
|
|
36
|
+
*
|
|
37
|
+
* @typedef {Object} parametersOfEncryptPrivate
|
|
38
|
+
* @property {string} text - The plain text to be encrypted.
|
|
39
|
+
* @property {string} [privateKey] - Optional RSA private key for encryption. If not provided, a default key may be used.
|
|
40
|
+
*/
|
|
13
41
|
export type parametersOfEncryptPrivate = {
|
|
14
42
|
text: string;
|
|
15
43
|
privateKey?: string;
|
|
16
44
|
};
|
|
45
|
+
/**
|
|
46
|
+
* Type representing the parameters required to decrypt text using a public key.
|
|
47
|
+
*
|
|
48
|
+
* @typedef {Object} parametersOfDecryptPublic
|
|
49
|
+
* @property {string} text - The base64-encoded string to be decrypted.
|
|
50
|
+
* @property {string} [publicKey] - Optional RSA public key for decryption. If not provided, a default key may be used.
|
|
51
|
+
*/
|
|
17
52
|
export type parametersOfDecryptPublic = {
|
|
18
53
|
text: string;
|
|
19
54
|
publicKey?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "encrypt-rsa",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
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",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"prepublish": "npm run build",
|
|
23
23
|
"husky": "husky",
|
|
24
24
|
"lint": "eslint",
|
|
25
|
+
"docs": "npx @compodoc/compodoc -p tsconfig.json -d docs",
|
|
25
26
|
"release": "standard-version"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|