encrypt-rsa 1.2.0 → 2.0.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/CHANGELOG.md CHANGED
@@ -1,3 +1,36 @@
1
+ #### 2.0.1 (2022-03-23)
2
+
3
+ ##### Documentation Changes
4
+
5
+ * update docs ([c06781e3](https://github.com/miladezzat/encrypt-rsa/commit/c06781e34ec15219f12c1be880005b0ab6696d4a))
6
+
7
+ ## 2.0.0 (2022-03-23)
8
+
9
+ ##### Documentation Changes
10
+
11
+ * update docs ([e4e3f254](https://github.com/miladezzat/encrypt-rsa/commit/e4e3f254a4819922138f1e1763415207dc1591c6))
12
+ * update docs ([de0f8024](https://github.com/miladezzat/encrypt-rsa/commit/de0f8024aad85c591273f93b50952cd72e230693))
13
+
14
+ ##### New Features
15
+
16
+ * make methods to take public and private keys as strings ([d47af0f5](https://github.com/miladezzat/encrypt-rsa/commit/d47af0f5c39d49cfb1589b0e236cc7c995bfb395))
17
+ * add new helper methods to decode and encode keys to base64 ([d3cba4c0](https://github.com/miladezzat/encrypt-rsa/commit/d3cba4c0f36d42e615f22c4ed4b828ed151faecb))
18
+ * update the docs ([4e470c6f](https://github.com/miladezzat/encrypt-rsa/commit/4e470c6feabfc828172bc484285239a24df064a4))
19
+
20
+ ##### Tests
21
+
22
+ * add tests ([373d6c12](https://github.com/miladezzat/encrypt-rsa/commit/373d6c125184bd2e1394091def64280a5392413e))
23
+
24
+ #### 1.2.1 (2022-02-11)
25
+
26
+ ##### Documentation Changes
27
+
28
+ * update docs ([846687b4](https://github.com/miladezzat/encrypt-rsa/commit/846687b4226bad946b2c0a20636ebec433c9005a))
29
+
30
+ ##### New Features
31
+
32
+ * add encrypt-rsa.js.org ([3f6b0b4e](https://github.com/miladezzat/encrypt-rsa/commit/3f6b0b4e1fbc8082b9416f3dadd3be277a6e5c3f))
33
+
1
34
  ### 1.2.0 (2022-02-09)
2
35
 
3
36
  ##### Documentation Changes
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  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
10
 
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)
11
+ ## For more explanation about RSA Algorithm visit [RSA algorithm](https://milad-ezzat.vercel.app/blog/encrypt-by-rsa-algorithm)
12
12
 
13
13
  1. [Installation](#installation)
14
14
  2. [Usage](#usage)
@@ -41,16 +41,12 @@ const nodeRSA = new NodeRSA();
41
41
 
42
42
  const { privateKey, publicKey } = nodeRSA.createPrivateAndPublicKeys()
43
43
 
44
- // to save your keys
45
- fs.writeFileSync('./private-key', privateKey);
46
- fs.writeFileSync('./public-key', publicKey);
47
-
48
44
 
49
45
  // you must have 'public-key' file the key you want to encrypt by it
50
46
 
51
47
  const encryptedText = nodeRSA.encryptStringWithRsaPublicKey({
52
48
  text: 'hello',
53
- keyPath: path.join(__dirname, './public-key')
49
+ publicKey: public
54
50
  });
55
51
 
56
52
  console.log({ encryptedText });
@@ -63,12 +59,10 @@ console.log({ encryptedText });
63
59
 
64
60
  const decryptedText = nodeRSA.decryptStringWithRsaPrivateKey({
65
61
  text: encryptedText,
66
- keyPath: path.join(__dirname, './private-key')
62
+ privateKey: private
67
63
  });
68
64
 
69
65
  console.log({ decryptedText });
70
66
  // result: { decryptedText: 'hello' }
71
67
 
72
68
  ```
73
- for more information about RSA:
74
- [https://simple.wikipedia.org/wiki/RSA_algorithm](https://simple.wikipedia.org/wiki/RSA_algorithm)
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 = helpers_1.decode(this.convertKetToBase64(publicKey));
32
28
  var buffer = Buffer.from(text);
33
- var encrypted = crypto_1.publicEncrypt(publicKey, buffer);
29
+ var encrypted = 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 = 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 = 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 = 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 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,6 +1,6 @@
1
1
  {
2
2
  "name": "encrypt-rsa",
3
- "version": "1.2.0",
3
+ "version": "2.0.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",
@@ -80,5 +80,5 @@
80
80
  "bugs": {
81
81
  "url": "https://github.com/miladezzat/encrypt-rsa/issues"
82
82
  },
83
- "homepage": "https://miladezzat.github.io/encrypt-rsa/"
83
+ "homepage": "https://encrypt-rsa.js.org"
84
84
  }