aboutsummaryrefslogtreecommitdiff
path: root/plugins/gpgme-vala
diff options
context:
space:
mode:
authorfiaxh <git@mx.ax.lt>2017-10-16 00:23:51 +0200
committerfiaxh <git@mx.ax.lt>2017-10-22 18:26:31 +0200
commit9ea16b6d8568cb383eb1f469d1dc54bfcad4f188 (patch)
tree27da8705fc99f2407af5e60083e34ddeeff39630 /plugins/gpgme-vala
parent8b43df8ec3f92477f857280668a9f29f0b9d6229 (diff)
downloaddino-9ea16b6d8568cb383eb1f469d1dc54bfcad4f188.tar.gz
dino-9ea16b6d8568cb383eb1f469d1dc54bfcad4f188.zip
PGP encrypted file transfers
Diffstat (limited to 'plugins/gpgme-vala')
-rw-r--r--plugins/gpgme-vala/src/gpgme_helper.vala43
-rw-r--r--plugins/gpgme-vala/vapi/gpgme.vapi8
2 files changed, 49 insertions, 2 deletions
diff --git a/plugins/gpgme-vala/src/gpgme_helper.vala b/plugins/gpgme-vala/src/gpgme_helper.vala
index 709b9e0c..cc013164 100644
--- a/plugins/gpgme-vala/src/gpgme_helper.vala
+++ b/plugins/gpgme-vala/src/gpgme_helper.vala
@@ -19,6 +19,20 @@ public static string encrypt_armor(string plain, Key[] keys, EncryptFlags flags)
}
}
+public static uint8[] encrypt_file(string uri, Key[] keys, EncryptFlags flags) throws GLib.Error {
+ global_mutex.lock();
+ try {
+ initialize();
+ Data plain_data = Data.create_from_file(uri);
+ Context context = Context.create();
+ context.set_armor(true);
+ Data enc_data = context.op_encrypt(keys, flags, plain_data);
+ return get_uint8_from_data(enc_data);
+ } finally {
+ global_mutex.unlock();
+ }
+}
+
public static string decrypt(string encr) throws GLib.Error {
global_mutex.lock();
try {
@@ -32,6 +46,19 @@ public static string decrypt(string encr) throws GLib.Error {
}
}
+public static uint8[] decrypt_data(uint8[] data) throws GLib.Error {
+ global_mutex.lock();
+ try {
+ initialize();
+ Data enc_data = Data.create_from_memory(data, false);
+ Context context = Context.create();
+ Data dec_data = context.op_decrypt(enc_data);
+ return get_uint8_from_data(dec_data);
+ } finally {
+ global_mutex.unlock();
+ }
+}
+
public static string sign(string plain, SigMode mode, Key? key = null) throws GLib.Error {
global_mutex.lock();
try {
@@ -125,6 +152,20 @@ private static string get_string_from_data(Data data) {
return res;
}
+private static uint8[] get_uint8_from_data(Data data) {
+ data.seek(0);
+ uint8[] buf = new uint8[256];
+ ssize_t? len = null;
+ Array<uint8> res = new Array<uint8>(false, true, 0);
+ do {
+ len = data.read(buf);
+ if (len > 0) {
+ res.append_vals(buf, (int)len);
+ }
+ } while (len > 0);
+ return res.data;
+}
+
private static void initialize() {
if (!initialized) {
check_version();
@@ -132,4 +173,4 @@ private static void initialize() {
}
}
-} \ No newline at end of file
+}
diff --git a/plugins/gpgme-vala/vapi/gpgme.vapi b/plugins/gpgme-vala/vapi/gpgme.vapi
index 0b14185c..51823a15 100644
--- a/plugins/gpgme-vala/vapi/gpgme.vapi
+++ b/plugins/gpgme-vala/vapi/gpgme.vapi
@@ -464,7 +464,13 @@ namespace GPG {
}
[CCode (cname = "gpgme_data_new_from_file")]
- public static GPGError.Error create_from_file(out Data d, string filename, int copy = 1);
+ public static GPGError.Error new_from_file(out Data d, string filename, int copy = 1);
+
+ public static Data create_from_file(string filename, int copy = 1) {
+ Data data;
+ throw_if_error(new_from_file(out data, filename, copy));
+ return data;
+ }
[CCode (cname = "gpgme_data_release_and_get_mem")]
public string release_and_get_mem(out size_t len);