encrypt-rsa 1.2.1 → 2.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
@@ -4,11 +4,15 @@
4
4
  [![npm version](https://badge.fury.io/js/encrypt-rsa.svg)](https://badge.fury.io/js/encrypt-rsa) 
5
5
  ![https://img.shields.io/npm/dm/encrypt-rsa.svg](https://img.shields.io/npm/dm/encrypt-rsa.svg)
6
6
 
7
- > 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))
7
+ This package works fine with web browsers and servers
8
+
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))
8
10
 
9
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.
10
12
 
11
- ### For more explanation about RSA Algorithm visit [https://milad-ezzat.vercel.app/posts/rsa-encryption-algorithm](https://milad-ezzat.vercel.app/posts/rsa-encryption-algorithm)
13
+ ### For more explanation about RSA Algorithm visit
14
+
15
+ - [Rsa Algorithm](https://milad-ezzat.vercel.app/blog/encrypt-by-rsa-algorithm)
12
16
 
13
17
  1. [Installation](#installation)
14
18
  2. [Usage](#usage)
@@ -17,58 +21,55 @@ RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means th
17
21
 
18
22
  ```bash
19
23
  npm i encrypt-rsa
20
-
21
24
  // OR
22
-
23
25
  yarn add encrypt-rsa
24
26
  ```
25
27
 
26
-
27
28
  ## Usage
28
29
 
29
30
  ```js
30
- import NodeRSA from 'encrypt-rsa';
31
+ import EncryptRsa from 'encrypt-rsa';
31
32
 
32
33
  //OR
33
34
 
34
- const NodeRSA = require('encrypt-rsa').default;
35
+ const EncryptRsa = require('encrypt-rsa').default;
35
36
 
36
- const fs = require('fs'); // Or import * as fs from 'fs';
37
-
38
- const nodeRSA = new NodeRSA();
39
-
40
- // create public and private keys
41
-
42
- const { privateKey, publicKey } = nodeRSA.createPrivateAndPublicKeys()
37
+ // create instance
38
+ const encryptRsa = new EncryptRsa();
39
+ ```
43
40
 
44
- // to save your keys
45
- fs.writeFileSync('./private-key', privateKey);
46
- fs.writeFileSync('./public-key', publicKey);
41
+ ### Create Private and Public keys
47
42
 
43
+ ```js
44
+ const { privateKey, publicKey } = nodeRSA.createPrivateAndPublicKeys();
45
+ ```
48
46
 
49
- // you must have 'public-key' file the key you want to encrypt by it
47
+ ### Encrypt Text
50
48
 
51
- const encryptedText = nodeRSA.encryptStringWithRsaPublicKey({
52
- text: 'hello',
53
- keyPath: path.join(__dirname, './public-key')
49
+ ```js
50
+ const encryptedText = encryptRsa.encryptStringWithRsaPublicKey({
51
+ text: 'hello world',
52
+ publicKey,
54
53
  });
54
+ console.log(encryptedText);
55
55
 
56
- console.log({ encryptedText });
57
-
58
- //result: {
59
- // encryptedText: 'QAoJBAj7ZqYR9Qb9vFGfpjBVY7BP0MtlPywyxMSodA7WmOmOn0glOlrLxUqjJrmaKsqxdJxZadEMAM8+6gLNhwcLtbFPRLQEUTSHk2NNhehsPOESoNjwbXOj5Y+zBCSkjVuW6MRkdaTZeGXi0sii1OqvIQGmOaOR2xzEdDj2eD8='
60
- // }
56
+ // QMPLJN3KzvXxkll18wax+KxqC0UqiHrwMxYGNUmJWMi98diapGOL/WzliPP3bTrHu7yWU1DnaB3f71w6JBYP+wG98fWLaz8+rwemerVja8B0FJVUphjBUmoDhX52JSoLFI0YVHtihXtoRk1pVaRFWm8FmZPZAcCKL7a0YDI1wABGvcSbLhaacmgX6zR6fzyltWVCrXn0NcVGox7WK7x4sCtywNhZx2XuUVSztr7QYcV2OQe8aDTUd7NXtaBVkj9RUYUR2QvhIpETksx14WD4ytohM68RUIJLRmU3y761mxcF+7Pjw/Utcirqu2Ohg0K18xGqlaE6fdifh0vIlfH+kA==
57
+ ```
61
58
 
62
- // you must have 'private-key' file the key you want to decrypt by it
59
+ ### Decrypt Encrypted Text
63
60
 
61
+ ```js
64
62
  const decryptedText = nodeRSA.decryptStringWithRsaPrivateKey({
65
63
  text: encryptedText,
66
- keyPath: path.join(__dirname, './private-key')
64
+ privateKey
67
65
  });
66
+ console.log(decryptedText);
67
+ // hello world
68
+ ```
68
69
 
69
- console.log({ decryptedText });
70
- // result: { decryptedText: 'hello' }
70
+ ## Rsa Algorithm:
71
+ - [RSA Algorithm](https://simple.wikipedia.org/wiki/RSA_algorithm)
71
72
 
72
- ```
73
- for more information about RSA:
74
- [https://simple.wikipedia.org/wiki/RSA_algorithm](https://simple.wikipedia.org/wiki/RSA_algorithm)
73
+
74
+ ### Contributions
75
+ 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)
package/build/index.d.ts CHANGED
@@ -1,32 +1,34 @@
1
- import { parametersOfEncryptAndDecrypt, returnCreateKeys } from './utils/types';
1
+ import { parametersOfDecrypt, parametersOfEncrypt, returnCreateKeys } from './utils/types';
2
2
  declare class NodeRSA {
3
- private publicKeyPath;
4
- private privateKeyPath;
3
+ private publicKey;
4
+ private privateKey;
5
5
  private modulusLength;
6
+ private keyBase64;
6
7
  /**
7
8
  *
8
- * @param publicKeyPath this should be absolute path
9
- * @param privateKeyPath this should be absolute path
9
+ * @param publicKey
10
+ * @param privateKey
10
11
  */
11
- constructor(publicKeyPath?: string, privateKeyPath?: string, modulusLength?: number);
12
+ constructor(publicKey?: string, privateKey?: string, modulusLength?: number);
12
13
  /**
13
14
  *
14
15
  * @param {Object} args
15
- * @param {String} args.publicKeyPath
16
+ * @param {String} args.publicKey
16
17
  * @param {String} args.text the text that you need to encrypt
17
18
  *
18
19
  * @returns {String}
19
20
  */
20
- encryptStringWithRsaPublicKey(args: parametersOfEncryptAndDecrypt): string;
21
+ encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string;
21
22
  /**
22
23
  *
23
24
  * @param {Object} args
24
- * @param {String} args.privateKeyPath
25
+ * @param {String} args.privateKey
25
26
  * @param {String} args.text the text that you need to decrypt
26
27
  *
27
28
  * @returns {String}
28
29
  */
29
- decryptStringWithRsaPrivateKey(args: parametersOfEncryptAndDecrypt): string;
30
+ decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string;
30
31
  createPrivateAndPublicKeys(modulusLength?: number): returnCreateKeys;
32
+ private convertKetToBase64;
31
33
  }
32
34
  export default NodeRSA;
package/build/index.js CHANGED
@@ -1,57 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var crypto_1 = require("crypto");
4
- var fs_1 = require("fs");
5
- var path_1 = require("path");
6
- var generateKeyPairSync = require('crypto').generateKeyPairSync;
4
+ var helpers_1 = require("./utils/helpers");
7
5
  var NodeRSA = /** @class */ (function () {
8
6
  /**
9
7
  *
10
- * @param publicKeyPath this should be absolute path
11
- * @param privateKeyPath this should be absolute path
8
+ * @param publicKey
9
+ * @param privateKey
12
10
  */
13
- function NodeRSA(publicKeyPath, privateKeyPath, modulusLength) {
14
- if (publicKeyPath === void 0) { publicKeyPath = ''; }
15
- if (privateKeyPath === void 0) { privateKeyPath = ''; }
16
- this.publicKeyPath = publicKeyPath;
17
- this.privateKeyPath = privateKeyPath;
11
+ function NodeRSA(publicKey, privateKey, modulusLength) {
12
+ this.publicKey = publicKey;
13
+ this.privateKey = privateKey;
18
14
  this.modulusLength = modulusLength || 2048;
15
+ this.keyBase64 = '';
19
16
  }
20
17
  /**
21
18
  *
22
19
  * @param {Object} args
23
- * @param {String} args.publicKeyPath
20
+ * @param {String} args.publicKey
24
21
  * @param {String} args.text the text that you need to encrypt
25
22
  *
26
23
  * @returns {String}
27
24
  */
28
25
  NodeRSA.prototype.encryptStringWithRsaPublicKey = function (args) {
29
- var text = args.text, _a = args.keyPath, keyPath = _a === void 0 ? this.publicKeyPath : _a;
30
- var absolutePath = path_1.resolve(keyPath);
31
- var publicKey = fs_1.readFileSync(absolutePath, 'utf8');
26
+ var text = args.text, _a = args.publicKey, publicKey = _a === void 0 ? this.publicKey : _a;
27
+ var publicKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(publicKey));
32
28
  var buffer = Buffer.from(text);
33
- var encrypted = crypto_1.publicEncrypt(publicKey, buffer);
29
+ var encrypted = (0, crypto_1.publicEncrypt)(publicKeyDecoded, buffer);
34
30
  return encrypted.toString('base64');
35
31
  };
36
32
  /**
37
33
  *
38
34
  * @param {Object} args
39
- * @param {String} args.privateKeyPath
35
+ * @param {String} args.privateKey
40
36
  * @param {String} args.text the text that you need to decrypt
41
37
  *
42
38
  * @returns {String}
43
39
  */
44
40
  NodeRSA.prototype.decryptStringWithRsaPrivateKey = function (args) {
45
- var text = args.text, _a = args.keyPath, keyPath = _a === void 0 ? this.privateKeyPath : _a;
46
- var absolutePath = path_1.resolve(keyPath);
47
- var privateKey = fs_1.readFileSync(absolutePath, 'utf8');
41
+ var text = args.text, _a = args.privateKey, privateKey = _a === void 0 ? this.privateKey : _a;
42
+ var privateKeyDecoded = (0, helpers_1.decode)(this.convertKetToBase64(privateKey));
48
43
  var buffer = Buffer.from(text, 'base64');
49
- var decrypted = crypto_1.privateDecrypt(privateKey, buffer);
44
+ var decrypted = (0, crypto_1.privateDecrypt)(privateKeyDecoded, buffer);
50
45
  return decrypted.toString('utf8');
51
46
  };
52
47
  NodeRSA.prototype.createPrivateAndPublicKeys = function (modulusLength) {
53
48
  if (modulusLength === void 0) { modulusLength = this.modulusLength; }
54
- var _a = generateKeyPairSync('rsa', {
49
+ var _a = (0, crypto_1.generateKeyPairSync)('rsa', {
55
50
  modulusLength: modulusLength,
56
51
  publicKeyEncoding: {
57
52
  type: 'spki',
@@ -64,6 +59,9 @@ var NodeRSA = /** @class */ (function () {
64
59
  }), privateKey = _a.privateKey, publicKey = _a.publicKey;
65
60
  return { privateKey: privateKey, publicKey: publicKey };
66
61
  };
62
+ NodeRSA.prototype.convertKetToBase64 = function (key) {
63
+ return (0, helpers_1.encode)(key.replace(/^ +/gm, '') || this.keyBase64);
64
+ };
67
65
  return NodeRSA;
68
66
  }());
69
67
  exports.default = NodeRSA;
@@ -0,0 +1,2 @@
1
+ export declare const decode: (str: string) => string;
2
+ export declare const encode: (str: string) => string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encode = exports.decode = void 0;
4
+ var decode = function (str) { return Buffer.from(str, 'base64').toString('utf-8'); };
5
+ exports.decode = decode;
6
+ var encode = function (str) { return Buffer.from(str, 'utf-8').toString('base64'); };
7
+ exports.encode = encode;
@@ -2,7 +2,11 @@ export declare type returnCreateKeys = {
2
2
  privateKey: string;
3
3
  publicKey: string;
4
4
  };
5
- export declare type parametersOfEncryptAndDecrypt = {
5
+ export declare type parametersOfEncrypt = {
6
6
  text: string;
7
- keyPath: string;
7
+ publicKey?: string;
8
+ };
9
+ export declare type parametersOfDecrypt = {
10
+ text: string;
11
+ privateKey?: string;
8
12
  };
package/package.json CHANGED
@@ -1,49 +1,52 @@
1
1
  {
2
2
  "name": "encrypt-rsa",
3
- "version": "1.2.1",
4
- "description": "this is a little module use to encrypt and decrypt strings with RSA keys (public and private keys)",
3
+ "version": "2.1.1",
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",
7
7
  "files": [
8
8
  "build/**/*"
9
9
  ],
10
+ "engines": {
11
+ "node": "16.x",
12
+ "npm": "8.x"
13
+ },
10
14
  "types": "./build/index.d.ts",
11
15
  "source": "./src/index.ts",
12
16
  "scripts": {
13
- "test": "./node_modules/.bin/mocha -r ts-node/register tests/**/*.spec.ts",
14
- "depcheck": "./node_modules/.bin/depcheck",
17
+ "test": "mocha -r ts-node/register tests/**/*.spec.ts",
18
+ "depcheck": "npm-check",
15
19
  "prepare": "husky install",
16
20
  "build": "tsc",
17
- "commit": "./node_modules/.bin/git-cz",
21
+ "commit": "git-cz",
18
22
  "docs:serve": "docsify serve docs",
19
23
  "_postinstall": "husky install",
20
24
  "prepublishOnly": "pinst --disable",
21
25
  "postpublish": "pinst --enable",
22
- "husky": "./node_modules/.bin/husky",
23
- "lint": "./node_modules/.bin/eslint",
26
+ "husky": "husky",
27
+ "lint": "eslint",
24
28
  "release:major": "changelog -M && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags",
25
29
  "release:minor": "changelog -m && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags",
26
30
  "release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags"
27
31
  },
28
32
  "devDependencies": {
29
33
  "@types/chai": "^4.2.21",
30
- "@types/mocha": "^8.2.3",
31
- "@types/node": "^16.3.3",
32
- "@typescript-eslint/eslint-plugin": "^4.28.3",
33
- "@typescript-eslint/parser": "^4.28.3",
34
+ "@types/mocha": "^9.1.1",
35
+ "@types/node": "^17.0.38",
36
+ "@typescript-eslint/eslint-plugin": "^5.27.0",
37
+ "@typescript-eslint/parser": "^5.27.0",
34
38
  "chai": "^4.3.4",
35
39
  "commitizen": "^4.2.4",
36
- "docsify-cli": "^4.4.3",
37
40
  "cz-conventional-changelog": "^3.3.0",
38
- "depcheck": "^1.4.2",
39
- "eslint": "^7.31.0",
40
- "eslint-config-airbnb-base": "^14.2.1",
41
+ "docsify-cli": "^4.4.3",
42
+ "eslint": "^8.16.0",
43
+ "eslint-config-airbnb-base": "^15.0.0",
41
44
  "eslint-plugin-import": "^2.23.4",
42
45
  "generate-changelog": "^1.8.0",
43
46
  "git-cz": "^4.7.6",
44
- "husky": "^7.0.1",
45
- "mocha": "^9.0.2",
46
- "pinst": "^2.1.6",
47
+ "husky": "^8.0.1",
48
+ "mocha": "^10.0.0",
49
+ "pinst": "^3.0.0",
47
50
  "ts-node": "^10.1.0",
48
51
  "typescript": "^4.3.5"
49
52
  },
@@ -80,5 +83,8 @@
80
83
  "bugs": {
81
84
  "url": "https://github.com/miladezzat/encrypt-rsa/issues"
82
85
  },
83
- "homepage": "https://miladezzat.github.io/encrypt-rsa/"
86
+ "homepage": "https://encrypt-rsa.js.org",
87
+ "dependencies": {
88
+ "npm-check": "^5.9.2"
89
+ }
84
90
  }
package/CHANGELOG.md DELETED
@@ -1,72 +0,0 @@
1
- #### 1.2.1 (2022-02-11)
2
-
3
- ##### Documentation Changes
4
-
5
- * update docs ([846687b4](https://github.com/miladezzat/encrypt-rsa/commit/846687b4226bad946b2c0a20636ebec433c9005a))
6
-
7
- ##### New Features
8
-
9
- * add encrypt-rsa.js.org ([3f6b0b4e](https://github.com/miladezzat/encrypt-rsa/commit/3f6b0b4e1fbc8082b9416f3dadd3be277a6e5c3f))
10
-
11
- ### 1.2.0 (2022-02-09)
12
-
13
- ##### Documentation Changes
14
-
15
- * set docs ([6f1fa5e3](https://github.com/miladezzat/encrypt-rsa/commit/6f1fa5e38e7d1ef0576a8e49501766f540715477))
16
-
17
- ### 1.1.0 (2021-07-21)
18
-
19
- ##### Documentation Changes
20
-
21
- * ✏️ update docs for creating keys ([cebb2758](https://github.com/miladezzat/encrypt-rsa/commit/cebb2758ac55478d46f0ae6228a10f8f1f78b8c3))
22
-
23
- ##### New Features
24
-
25
- * 🎸 add create public and private keys ([74c51131](https://github.com/miladezzat/encrypt-rsa/commit/74c51131a858ad28cdda605b479b6bd21297e730))
26
-
27
- #### 1.0.12 (2021-07-20)
28
-
29
- ##### Chores
30
-
31
- * 🤖 update keywords ([b435e3ba](https://github.com/miladezzat/encrypt-rsa/commit/b435e3ba757a80dd66b95ed8a8a668952b66558e))
32
-
33
- #### 1.0.11 (2021-07-20)
34
-
35
- ##### Bug Fixes
36
-
37
- * 🐛 solve docs ([8a08c44e](https://github.com/miladezzat/encrypt-rsa/commit/8a08c44e767d5dcd242b5a5806602468cb663ec1))
38
-
39
- #### 1.0.10 (2021-07-20)
40
-
41
- ##### Documentation Changes
42
-
43
- * ✏️ update readme ([e29f1805](https://github.com/miladezzat/encrypt-rsa/commit/e29f180513d8f79daaad9fee7e0a532e2e94f076))
44
-
45
- #### 1.0.9 (2021-07-19)
46
-
47
- #### 1.0.8 (2021-07-19)
48
-
49
- #### 1.0.8 (2021-07-19)
50
-
51
- #### 1.0.5 (2021-07-19)
52
-
53
- #### 1.0.4 (2021-07-19)
54
-
55
- #### 1.0.4 (2021-07-19)
56
-
57
- #### 1.0.3 (2021-07-19)
58
-
59
- #### 1.0.2 (2021-07-19)
60
-
61
- #### 1.0.1 (2021-07-19)
62
-
63
- ##### Other Changes
64
-
65
- * miladezzat/rsa-node ([5f3c40de](https://github.com/miladezzat/encrypt-rsa/commit/5f3c40deb5cf9424bee39bfae4620866e828fcf8))
66
-
67
- ## 1.0.0 (2021-07-18)
68
-
69
- ##### Other Changes
70
-
71
- * miladezzat/rsa-node (5f3c40de)
72
-