From 9ea16b6d8568cb383eb1f469d1dc54bfcad4f188 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 16 Oct 2017 00:23:51 +0200 Subject: PGP encrypted file transfers --- plugins/gpgme-vala/src/gpgme_helper.vala | 43 +++++++++++++++++++++++++++++++- plugins/gpgme-vala/vapi/gpgme.vapi | 8 +++++- 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'plugins/gpgme-vala') 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 res = new Array(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); -- cgit v1.2.3-54-g00ecf