|
@@ -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
|