encrypt-rsa 3.3.0 → 4.0.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 (39) hide show
  1. package/README.md +217 -154
  2. package/build/node/node/convertKetToBase64.d.ts +6 -0
  3. package/build/{functions → node/node}/convertKetToBase64.js +2 -7
  4. package/build/node/node/crypto.d.ts +6 -0
  5. package/build/node/node/crypto.js +98 -0
  6. package/build/node/node/index.d.ts +17 -0
  7. package/build/node/node/index.js +61 -0
  8. package/build/node/shared/helpers.d.ts +32 -0
  9. package/build/node/shared/helpers.js +101 -0
  10. package/build/{utils → node/shared}/types.d.ts +13 -0
  11. package/build/web/shared/helpers.d.ts +32 -0
  12. package/build/web/shared/helpers.js +101 -0
  13. package/build/web/shared/types.d.ts +68 -0
  14. package/build/web/shared/types.js +2 -0
  15. package/build/web/web/convertKetToBase64.d.ts +6 -0
  16. package/build/web/web/convertKetToBase64.js +13 -0
  17. package/build/web/web/crypto.d.ts +6 -0
  18. package/build/web/web/crypto.js +165 -0
  19. package/build/web/web/index.d.ts +17 -0
  20. package/build/web/web/index.js +77 -0
  21. package/package.json +24 -8
  22. package/build/functions/convertKetToBase64.d.ts +0 -11
  23. package/build/functions/createPrivateAndPublicKeys.d.ts +0 -17
  24. package/build/functions/createPrivateAndPublicKeys.js +0 -61
  25. package/build/functions/decrypt.d.ts +0 -19
  26. package/build/functions/decrypt.js +0 -53
  27. package/build/functions/decryptStringWithRsaPrivateKey.d.ts +0 -18
  28. package/build/functions/decryptStringWithRsaPrivateKey.js +0 -52
  29. package/build/functions/encrypt.d.ts +0 -19
  30. package/build/functions/encrypt.js +0 -53
  31. package/build/functions/encryptStringWithRsaPublicKey.d.ts +0 -19
  32. package/build/functions/encryptStringWithRsaPublicKey.js +0 -53
  33. package/build/functions/index.d.ts +0 -10
  34. package/build/functions/index.js +0 -35
  35. package/build/index.d.ts +0 -92
  36. package/build/index.js +0 -115
  37. package/build/utils/helpers.d.ts +0 -16
  38. package/build/utils/helpers.js +0 -35
  39. /package/build/{utils → node/shared}/types.js +0 -0
package/package.json CHANGED
@@ -1,19 +1,33 @@
1
1
  {
2
2
  "name": "encrypt-rsa",
3
- "version": "3.3.0",
3
+ "version": "4.0.0",
4
4
  "description": "This is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)",
5
- "main": "./build/index.js",
6
- "module": "./build/index.js",
5
+ "main": "./build/node/node/index.js",
6
+ "module": "./build/node/node/index.js",
7
+ "browser": "./build/web/web/index.js",
8
+ "types": "./build/node/node/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "node": "./build/node/node/index.js",
13
+ "default": "./build/web/web/index.js"
14
+ },
15
+ "require": {
16
+ "node": "./build/node/node/index.js",
17
+ "default": "./build/web/web/index.js"
18
+ }
19
+ }
20
+ },
7
21
  "files": [
8
- "build/**/*"
22
+ "build/node/**/*",
23
+ "build/web/**/*"
9
24
  ],
10
- "types": "./build/index.d.ts",
11
- "source": "./src/index.ts",
25
+ "source": "./src/node/index.ts",
12
26
  "scripts": {
13
27
  "test": "mocha -r ts-node/register tests/**/*.spec.ts",
14
28
  "depcheck": "npm-check -u",
15
29
  "prepare": "husky install",
16
- "build": "tsc",
30
+ "build": "tsc -p tsconfig.node.json && tsc -p tsconfig.web.json",
17
31
  "commit": "git-cz",
18
32
  "docs:serve": "docsify serve docs",
19
33
  "_postinstall": "husky install",
@@ -22,7 +36,8 @@
22
36
  "prepublish": "npm run build",
23
37
  "husky": "husky",
24
38
  "lint": "eslint",
25
- "docs": "npx @compodoc/compodoc -p tsconfig.json -d docs",
39
+ "docs": "npx @compodoc/compodoc -p tsconfig.doc.json -d docs",
40
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
26
41
  "release": "standard-version"
27
42
  },
28
43
  "devDependencies": {
@@ -33,6 +48,7 @@
33
48
  "@typescript-eslint/parser": "^5.27.0",
34
49
  "chai": "^4.3.4",
35
50
  "commitizen": "^4.2.4",
51
+ "conventional-changelog-cli": "^4.1.0",
36
52
  "cz-conventional-changelog": "^3.3.0",
37
53
  "docsify-cli": "^4.4.3",
38
54
  "eslint": "^8.16.0",
@@ -1,11 +0,0 @@
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
- */
10
- export declare function convertKetToBase64(key: string): string;
11
- export default convertKetToBase64;
@@ -1,17 +0,0 @@
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
- */
16
- export declare function createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
17
- export default createPrivateAndPublicKeys;
@@ -1,61 +0,0 @@
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
- /**
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
- */
42
- function createPrivateAndPublicKeys(modulusLength) {
43
- if (modulusLength === void 0) { modulusLength = 2048; }
44
- if (typeof crypto.generateKeyPairSync === 'function') {
45
- var _a = crypto.generateKeyPairSync('rsa', {
46
- modulusLength: modulusLength,
47
- publicKeyEncoding: {
48
- type: 'spki',
49
- format: 'pem',
50
- },
51
- privateKeyEncoding: {
52
- type: 'pkcs8',
53
- format: 'pem',
54
- },
55
- }), privateKey = _a.privateKey, publicKey = _a.publicKey;
56
- return { publicKey: publicKey, privateKey: privateKey };
57
- }
58
- return { privateKey: '', publicKey: '' };
59
- }
60
- exports.createPrivateAndPublicKeys = createPrivateAndPublicKeys;
61
- exports.default = createPrivateAndPublicKeys;
@@ -1,19 +0,0 @@
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
- */
18
- export declare function decrypt(args: parametersOfDecryptPublic): string;
19
- export default decrypt;
@@ -1,53 +0,0 @@
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
- /**
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
- */
45
- function decrypt(args) {
46
- var text = args.text, publicKey = args.publicKey;
47
- var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
48
- var buffer = Buffer.from(text, 'base64');
49
- var decrypted = crypto.publicDecrypt(publicKeyDecoded, buffer);
50
- return decrypted.toString('utf8');
51
- }
52
- exports.decrypt = decrypt;
53
- exports.default = decrypt;
@@ -1,18 +0,0 @@
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
- */
17
- export declare function decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
18
- export default decryptStringWithRsaPrivateKey;
@@ -1,52 +0,0 @@
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
- /**
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
- */
44
- function decryptStringWithRsaPrivateKey(args) {
45
- var text = args.text, privateKey = args.privateKey;
46
- var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
47
- var buffer = Buffer.from(text, 'base64');
48
- var decrypted = crypto.privateDecrypt(privateKeyDecoded, buffer);
49
- return decrypted.toString('utf8');
50
- }
51
- exports.decryptStringWithRsaPrivateKey = decryptStringWithRsaPrivateKey;
52
- exports.default = decryptStringWithRsaPrivateKey;
@@ -1,19 +0,0 @@
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
- */
18
- export declare function encrypt(args: parametersOfEncryptPrivate): string;
19
- export default encrypt;
@@ -1,53 +0,0 @@
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
- /**
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
- */
45
- function encrypt(args) {
46
- var text = args.text, privateKey = args.privateKey;
47
- var privateKeyDecoded = (0, helpers_1.decode)(privateKey);
48
- var buffer = Buffer.from(text);
49
- var encrypted = crypto.privateEncrypt(privateKeyDecoded, buffer);
50
- return encrypted.toString('base64');
51
- }
52
- exports.encrypt = encrypt;
53
- exports.default = encrypt;
@@ -1,19 +0,0 @@
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
- */
18
- export declare function encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
19
- export default encryptStringWithRsaPublicKey;
@@ -1,53 +0,0 @@
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
- /**
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
- */
45
- function encryptStringWithRsaPublicKey(args) {
46
- var text = args.text, publicKey = args.publicKey;
47
- var publicKeyDecoded = (0, helpers_1.decode)(publicKey);
48
- var buffer = Buffer === null || Buffer === void 0 ? void 0 : Buffer.from(text);
49
- var encrypted = crypto === null || crypto === void 0 ? void 0 : crypto.publicEncrypt(publicKeyDecoded, buffer);
50
- return encrypted.toString('base64');
51
- }
52
- exports.encryptStringWithRsaPublicKey = encryptStringWithRsaPublicKey;
53
- exports.default = encryptStringWithRsaPublicKey;
@@ -1,10 +0,0 @@
1
- export * from './createPrivateAndPublicKeys';
2
- export { default as createPrivateAndPublicKeys } from './decryptStringWithRsaPrivateKey';
3
- export * from './decrypt';
4
- export { default as decrypt } from './encryptStringWithRsaPublicKey';
5
- export * from './decryptStringWithRsaPrivateKey';
6
- export { default as decryptStringWithRsaPrivateKey } from './decrypt';
7
- export * from './encrypt';
8
- export { default as encrypt } from './decryptStringWithRsaPrivateKey';
9
- export * from './encryptStringWithRsaPublicKey';
10
- export { default as encryptStringWithRsaPublicKey } from './encrypt';
@@ -1,35 +0,0 @@
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
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
18
- };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.encryptStringWithRsaPublicKey = exports.encrypt = exports.decryptStringWithRsaPrivateKey = exports.decrypt = exports.createPrivateAndPublicKeys = void 0;
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; } });
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; } });
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; } });
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; } });
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 DELETED
@@ -1,92 +0,0 @@
1
- /// <reference types="node" />
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
- */
7
- declare class NodeRSA {
8
- /**
9
- * @private
10
- * @type {string | undefined}
11
- * Public key used for encryption.
12
- */
13
- private publicKey;
14
- /**
15
- * @private
16
- * @type {string | undefined}
17
- * Private key used for decryption.
18
- */
19
- private privateKey;
20
- /**
21
- * @private
22
- * @type {number}
23
- * Length of the RSA modulus, defaults to 2048 bits.
24
- */
25
- private modulusLength;
26
- /**
27
- * @private
28
- * @type {'base64'}
29
- * Encoding format for the keys.
30
- */
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
- */
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
- */
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
- */
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
- */
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
- */
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
- */
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;
91
- }
92
- export default NodeRSA;