summaryrefslogtreecommitdiff
path: root/alot/crypto.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-02-21 09:43:20 -0800
committerDylan Baker <dylan@pnwbakers.com>2018-03-01 10:34:56 -0800
commit7805f34f55f7143e1ebfca982b0654a3ef42e1d0 (patch)
tree508e38cc7de1c0777e122918cdcd157b88526491 /alot/crypto.py
parent62c0e7354d64ddbfbb0130f690a5768185e3eb92 (diff)
Fix crypto
This makes me a little nervous. I wonder if we're better off leaving the bits that gpg works with as bytes while gpg is working with them and do the string transformation later.
Diffstat (limited to 'alot/crypto.py')
-rw-r--r--alot/crypto.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/alot/crypto.py b/alot/crypto.py
index 4ebb72f7..cfb148ac 100644
--- a/alot/crypto.py
+++ b/alot/crypto.py
@@ -8,6 +8,7 @@ from __future__ import absolute_import
import gpg
from .errors import GPGProblem, GPGCode
+from . import helper
def RFC3156_micalg_from_algo(hash_algo):
@@ -152,9 +153,9 @@ def detached_signature_for(plaintext_str, keys):
"""
ctx = gpg.core.Context(armor=True)
ctx.signers = keys
- (sigblob, sign_result) = ctx.sign(plaintext_str,
+ (sigblob, sign_result) = ctx.sign(plaintext_str.encode('utf-8'),
mode=gpg.constants.SIG_MODE_DETACH)
- return sign_result.signatures, sigblob
+ return sign_result.signatures, sigblob.decode('ascii')
def encrypt(plaintext_str, keys):
@@ -168,9 +169,9 @@ def encrypt(plaintext_str, keys):
"""
assert keys, 'Must provide at least one key to encrypt with'
ctx = gpg.core.Context(armor=True)
- out = ctx.encrypt(plaintext_str, recipients=keys, sign=False,
+ out = ctx.encrypt(plaintext_str.encode('utf-8'), recipients=keys, sign=False,
always_trust=True)[0]
- return out
+ return out.decode('ascii')
NO_ERROR = None
@@ -200,7 +201,7 @@ def verify_detached(message, signature):
"""
ctx = gpg.core.Context()
try:
- verify_results = ctx.verify(message, signature)[1]
+ verify_results = ctx.verify(message.encode('utf-8'), signature.encode('ascii'))[1]
return verify_results.signatures
except gpg.errors.BadSignatures as e:
raise GPGProblem(bad_signatures_to_str(e), code=GPGCode.BAD_SIGNATURE)
@@ -219,12 +220,13 @@ def decrypt_verify(encrypted):
"""
ctx = gpg.core.Context()
try:
- (plaintext, _, verify_result) = ctx.decrypt(encrypted, verify=True)
+ (plaintext, _, verify_result) = ctx.decrypt(
+ encrypted.encode('utf-8'), verify=True)
except gpg.errors.GPGMEError as e:
raise GPGProblem(str(e), code=e.getcode())
# what if the signature is bad?
- return verify_result.signatures, plaintext
+ return verify_result.signatures, helper.try_decode(plaintext)
def validate_key(key, sign=False, encrypt=False):