diff options
author | Justus Winter <4winter@informatik.uni-hamburg.de> | 2013-05-24 02:12:22 +0200 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2013-06-16 21:17:09 +0100 |
commit | 223cb4cf93684242bf701cea87ae6b4cdffbc891 (patch) | |
tree | 9a888fae878abe61f1a4763c81fd004d4faf8adb /alot/crypto.py | |
parent | 5be498b1b22ba567fe6e62eb4a7d5a116f543952 (diff) |
Parse and decrypt OpenPGP encrypted data
Parse and decrypt OpenPGP encrypted data as specified by RFC 3156. If
such a message is detected and found to be well-formed, it is
decrypted and any MIME messages found within the plain text are
attached to the original message.
Signed-off-by: Justus Winter <4winter@informatik.uni-hamburg.de>
Diffstat (limited to 'alot/crypto.py')
-rw-r--r-- | alot/crypto.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/alot/crypto.py b/alot/crypto.py index 74f1f5c5..d321cb2d 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 os import logging from email.generator import Generator @@ -209,6 +210,28 @@ def verify_detached(message, signature): raise GPGProblem(e.message, code=e.code) +def decrypt_verify(encrypted): + '''Decrypts the given ciphertext string and returns both the + signatures (if any) and the plaintext. + + :param encrypted: the mail to decrypt + :returns: a tuple (sigs, plaintext) with sigs being a list of a + :class:`gpgme.Signature` and plaintext is a `str` holding + the decrypted mail + :raises: :class:`~alot.errors.GPGProblem` if the decryption fails + ''' + encrypted_data = StringIO(encrypted) + plaintext_data = StringIO() + ctx = gpgme.Context() + try: + sigs = ctx.decrypt_verify(encrypted_data, plaintext_data) + except gpgme.GpgmeError as e: + raise GPGProblem(e.message, code=e.code) + + plaintext_data.seek(0, os.SEEK_SET) + return sigs, plaintext_data.read() + + def hash_key(key): """ Returns a hash of the given key. This is a workaround for |