فهرست منبع

WIP: add encrypt and decrypt operations

Currently the encrypt operation works only to my public key and not to
keys generated by the generate key pair operation. Probably something
wrong with the generate operation.
Toby Lorne 7 سال پیش
والد
کامیت
db8955d90d
6فایلهای تغییر یافته به همراه1422 افزوده شده و 162 حذف شده
  1. 6 0
      .babelrc
  2. 1318 157
      package-lock.json
  3. 4 1
      package.json
  4. 28 0
      src/core/config/OperationConfig.js
  5. 2 0
      src/core/config/modules/PGP.js
  6. 64 4
      src/core/operations/PGP.js

+ 6 - 0
.babelrc

@@ -1,4 +1,10 @@
 {
+    "plugins": [
+        ["transform-runtime", {
+            "polyfill": false,
+            "regenerator": true
+        }]
+    ],
     "presets": [
         ["env", {
             "targets": {

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1318 - 157
package-lock.json


+ 4 - 1
package.json

@@ -32,7 +32,11 @@
   "devDependencies": {
     "babel-core": "^6.26.0",
     "babel-loader": "^7.1.2",
+    "babel-plugin-transform-runtime": "^6.23.0",
+    "babel-polyfill": "^6.26.0",
     "babel-preset-env": "^1.6.0",
+    "babel-preset-es2015": "^6.24.1",
+    "babel-preset-stage-0": "^6.24.1",
     "css-loader": "^0.28.7",
     "exports-loader": "^0.6.4",
     "extract-text-webpack-plugin": "^3.0.1",
@@ -67,7 +71,6 @@
     "worker-loader": "^1.0.0"
   },
   "dependencies": {
-    "babel-polyfill": "^6.26.0",
     "bootstrap": "^3.3.7",
     "bootstrap-colorpicker": "^2.5.2",
     "bootstrap-switch": "^3.3.4",

+ 28 - 0
src/core/config/OperationConfig.js

@@ -3880,6 +3880,34 @@ const OperationConfig = {
             },
         ]
     },
+    "PGP Encrypt": {
+        module: "PGP",
+        manualBake: true,
+        description: "",
+        inputType: "string",
+        outputType: "string",
+        args: [
+            {
+                name: "Public key",
+                type: "text",
+                value: ""
+            },
+        ]
+    },
+    "PGP Decrypt": {
+        module: "PGP",
+        manualBake: true,
+        description: "",
+        inputType: "string",
+        outputType: "string",
+        args: [
+            {
+                name: "Private key",
+                type: "text",
+                value: ""
+            },
+        ]
+    },
 };
 
 

+ 2 - 0
src/core/config/modules/PGP.js

@@ -15,6 +15,8 @@ let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
 
 OpModules.PGP = {
     "Generate PGP Key Pair": PGP.runGenerateKeyPair,
+    "PGP Encrypt":           PGP.runEncrypt,
+    "PGP Decrypt":           PGP.runDecrypt,
 };
 
 export default OpModules;

+ 64 - 4
src/core/operations/PGP.js

@@ -1,3 +1,4 @@
+/*eslint camelcase: ["error", {properties: "never"}]*/
 import * as kbpgp from "kbpgp";
 import promisify from "es6-promisify";
 
@@ -105,16 +106,16 @@ const PGP = {
             primary: {
                 nbits: keySize,
                 flags: flags,
-                expire_in: 0 // eslint-disable-line camelcase
+                expire_in: 0
             },
             subkeys: [{
                 nbits: PGP.getSubkeySize(keySize),
                 flags: kbpgp.const.openpgp.sign_data,
-                expire_in: 86400 * 365 * 8 // eslint-disable-line camelcase
+                expire_in: 86400 * 365 * 8
             }, {
                 nbits: PGP.getSubkeySize(keySize),
                 flags: kbpgp.const.openpgp.encrypt_comm | kbpgp.const.openpgp.encrypt_storage,
-                expire_in: 86400 * 365 * 2 // eslint-disable-line camelcase
+                expire_in: 86400 * 365 * 2
             }],
         };
         return new Promise(async (resolve, reject) => {
@@ -131,7 +132,66 @@ const PGP = {
                 reject(`Error from kbpgp whilst generating key pair: ${err}`);
             }
         });
-    }
+    },
+
+    async runEncrypt(input, args) {
+        let plaintextMessage = input,
+            plainPubKey      = args[0];
+
+        let key, encryptedMessage;
+
+        try {
+            key = await promisify(kbpgp.KeyManager.import_from_armored_pgp)({
+                armored: plainPubKey,
+            });
+        } catch (err) {
+            console.error(err);
+            throw `Could not import public key: ${err}`;
+        }
+
+        try {
+            encryptedMessage = await promisify(kbpgp.box)({
+                msg:         plaintextMessage,
+                encrypt_for: key,
+            });
+        } catch (err) {
+            console.error(err);
+            throw `Could encrypt message to provided public key: ${err}`;
+        }
+
+        console.log(encryptedMessage);
+
+        return encryptedMessage;
+    },
+
+    async runDecrypt(input, args) {
+        let encryptedMessage = input,
+            plainPrivateKey  = args[0],
+            keyring          = new kbpgp.keyring.KeyRing();
+
+        let key, plaintextMessage;
+
+        try {
+            key = await promisify(kbpgp.KeyManager.import_from_armored_pgp)({
+                armored: plainPrivateKey,
+            });
+        } catch (err) {
+            throw `Could not import private key: ${err}`;
+        }
+
+        keyring.add_key_manager(key);
+
+        try {
+            plaintextMessage = await promisify(kbpgp.unbox)({
+                armored: encryptedMessage,
+                keyfetch: keyring,
+            });
+        } catch (err) {
+            throw `Could decrypt message to provided private key: ${err}`;
+        }
+
+        return plaintextMessage;
+    },
 };
 
 export default PGP;

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است