deploy1 6 years ago
parent
commit
2bf7b1d551
2 changed files with 45 additions and 9 deletions
  1. 9 9
      index.js
  2. 36 0
      pgpstuff.js

+ 9 - 9
index.js

@@ -1,11 +1,11 @@
 module.exports = {
-	generatezip: require("./generatezip"),
-	getthezip: require("./getthezip"),
-	isbrowser: require("./isbrowser"),
-	lib_browser: require("./lib_browser"),
-	lib_futch: require("./lib_futch"),
-	pgpstuff: require("./pgpstuff"),
-	promisetimeout: require("./promisetimeout"),
-	unpackzip.js: require("./unpackzip"),
-	uploader: require("./uploader")
+    generatezip: require("./generatezip"),
+    getthezip: require("./getthezip"),
+    isbrowser: require("./isbrowser"),
+    lib_browser: require("./lib_browser"),
+    lib_futch: require("./lib_futch"),
+    pgpstuff: require("./pgpstuff"),
+    promisetimeout: require("./promisetimeout"),
+    unpackzip: require("./unpackzip"),
+    uploader: require("./uploader")
 }

+ 36 - 0
pgpstuff.js

@@ -55,7 +55,43 @@ function create_identity(opt) {
     return oper;
 }
 
+function encrypt_object_to(keystr, obj, cb) {
+    var keyen = openpgp.key.readArmored(nb(keystr, 54).toString());
+    var key = keyen.keys[0];
+    var options = {
+        data: JSON.stringify(obj, true, 2), // input as String (or Uint8Array)
+        publicKeys: key, // for encryption
+    };
+    openpgp.encrypt(options).then(function(ciphertext) {
+        var encrypted = nb(cleanpgpstring(ciphertext.data)).toBase(59); // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
+        cb(encrypted);
+    });
+}
+
+function decrypt_object_to(keystr, objstr, cb, passphrase) {
+    var keyen = openpgp.key.readArmored(nb(keystr, 54).toString());
+    var key = keyen.keys[0];
+    if (passphrase) {
+        key.decrypt(passphrase)
+    } else {
+        key.decrypt(prompt("passphrase"))
+    }
+
+    var options = {
+        message: openpgp.message.readArmored(nb(objstr, 59).toString()), // parse armored message
+        privateKey: key // for decryption
+    };
+    openpgp.decrypt(options).then(function(plaintext) {
+        var decrypted = JSON.parse(plaintext.data)
+        cb(decrypted);
+    })
+
+}
+
+
 module.exports = {
+    decrypt_object_to: decrypt_object_to,
+    encrypt_object_to: encrypt_object_to,
     create_identity: create_identity,
     makeloginstr: makeloginstr,
     cleanpgpstring: cleanpgpstring