summaryrefslogtreecommitdiff
path: root/alot/crypto.py
diff options
context:
space:
mode:
authorJohannes Kulick <kulick@hildensia.de>2012-12-11 18:21:46 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2013-02-19 10:10:07 +0000
commit41357c538730da035ecc145f16c4dcc14b943e35 (patch)
tree4d932e86e1e141c6b4c64a99089d1b298a711b1e /alot/crypto.py
parent17ac6c9539986f3a8e3c079e7cfe56812d7da384 (diff)
add encryption function
This function takes a plaintext string and encryptes it with a given gpg key and returns the encrypted text
Diffstat (limited to 'alot/crypto.py')
-rw-r--r--alot/crypto.py19
1 files changed, 19 insertions, 0 deletions
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
+
+