From 41357c538730da035ecc145f16c4dcc14b943e35 Mon Sep 17 00:00:00 2001 From: Johannes Kulick Date: Tue, 11 Dec 2012 18:21:46 +0100 Subject: add encryption function This function takes a plaintext string and encryptes it with a given gpg key and returns the encrypted text --- alot/crypto.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'alot/crypto.py') diff --git a/alot/crypto.py b/alot/crypto.py index a457dd8e..2f1781f2 100644 --- a/alot/crypto.py +++ b/alot/crypto.py @@ -2,6 +2,7 @@ # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file import re +import logging from email.generator import Generator from cStringIO import StringIO @@ -150,3 +151,21 @@ def detached_signature_for(plaintext_str, key=None): signature_data.seek(0, 0) signature = signature_data.read() return sigs, signature + +def encrypt(plaintext_str, key=None): + """ + Encrypts the given plaintext string and returns a PGP/MIME compatible string + :param plaintext_str: the mail to encrypt + :param key: gpgme_key_t object representing the key to use + :rtype: a string holding the encrypted mail + """ + plaintext_data = StringIO(plaintext_str) + encrypted_data = StringIO() + ctx = gpgme.Context() + ctx.armor = True + ctx.encrypt([key], gpgme.ENCRYPT_ALWAYS_TRUST, plaintext_data, encrypted_data) + encrypted_data.seek(0, 0) + encrypted = encrypted_data.read() + return encrypted + + -- cgit v1.2.3