diff options
Diffstat (limited to 'memoryhole/protection.py')
-rw-r--r-- | memoryhole/protection.py | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/memoryhole/protection.py b/memoryhole/protection.py index b064f47..d1b372b 100644 --- a/memoryhole/protection.py +++ b/memoryhole/protection.py @@ -5,31 +5,52 @@ from memoryhole.gpg import Gnupg from memoryhole.rfc3156 import PGPEncrypted, MultipartEncrypted -def protect(msg, openpgp=Gnupg(), encrypt=True, obscure=True): +class ProtectConfig(object): + + PROTECTED_HEADERS = ('Subject', 'Message-ID', 'Date', 'To', 'From') + OBSCURED_HEADERS = ('Subject', 'Message-ID', 'Date', 'To', 'From') + + def __init__(self, openpgp=None, protected_headers=PROTECTED_HEADERS, + obscured_headers=OBSCURED_HEADERS): + """ + Configuration parameters for the protection + + :param openpgp: the implementation of openpgp to use for encryption + and/or signature + :type openpgp: IOpenPGP + :param protected_headers: list of headers to protect + :type protected_headers: [str] + :param obscured_headers: list of headers to obscure + :type obscured_headers: [str] + """ + if openpgp is None: + openpgp = Gnupg() + self.openpgp = openpgp + + +def protect(msg, encrypt=True, config=None): """ Protect an email with memory hole. It will protect the PROTECTED_HEADERS and if obscure=True will obscure the OBSCURED_HEADERS :param msg: the email to be protected :type msg: Message - :param openpgp: the implementation of openpgp to use for encryption and/or - signature - :type openpgp: OpenPGP :param encrypt: should the message be encrypted :type encrypt: bool - :param obscure: should the headers be obscured - :type obsucre: bool :return: an encrypted and/or signed email :rtype: Message """ + if config is None: + config = ProtectConfig() + if encrypt: - return _encrypt_mime(msg, openpgp) + return _encrypt_mime(msg, config) raise NotImplementedError() -def _encrypt_mime(msg, openpgp): +def _encrypt_mime(msg, config): encraddr = _recipient_addresses(msg) signaddr = _from_address(msg) @@ -38,8 +59,8 @@ def _encrypt_mime(msg, openpgp): newmsg.add_header(hkey, hval) del(msg[hkey]) - encstr = openpgp.encrypt(msg.as_string(unixfrom=False), - encraddr, signaddr) + encstr = config.openpgp.encrypt(msg.as_string(unixfrom=False), + encraddr, signaddr) encmsg = MIMEApplication( encstr, _subtype='octet-stream', _encoder=lambda x: x) encmsg.add_header('content-disposition', 'attachment', |