use BytesIO as input when encrypting

This commit is contained in:
Son NK 2020-04-02 21:29:16 +02:00
parent ed8caa237a
commit 3550447a66
3 changed files with 11 additions and 5 deletions

View file

@ -1,3 +1,5 @@
from io import BytesIO
import gnupg
from app.config import GNUPGHOME
@ -19,8 +21,8 @@ def load_public_key(public_key: str) -> str:
raise PGPException("Cannot load key") from e
def encrypt(data: str, fingerprint: str) -> str:
r = gpg.encrypt(data, fingerprint, always_trust=True)
def encrypt_file(data: BytesIO, fingerprint: str) -> str:
r = gpg.encrypt_file(data, fingerprint, always_trust=True)
if not r.ok:
raise PGPException("Cannot encrypt")

View file

@ -397,7 +397,9 @@ def prepare_pgp_message(orig_msg: Message, pgp_fingerprint: str):
second = MIMEApplication("octet-stream", _encoder=encoders.encode_7or8bit)
second.add_header("Content-Disposition", "inline")
# encrypt original message
encrypted_data = pgp_utils.encrypt(orig_msg.as_string(), pgp_fingerprint)
encrypted_data = pgp_utils.encrypt_file(
BytesIO(orig_msg.as_bytes()), pgp_fingerprint
)
second.set_payload(encrypted_data)
msg.attach(second)

View file

@ -1,4 +1,6 @@
from app.pgp_utils import load_public_key, gpg, encrypt
from io import BytesIO
from app.pgp_utils import load_public_key, gpg, encrypt_file
pubkey = """-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: Keybase OpenPGP v1.0.0
@ -101,5 +103,5 @@ def test_load_public_key():
def test_encrypt():
fingerprint = load_public_key(pubkey)
secret = encrypt("abcd", fingerprint)
secret = encrypt_file(BytesIO(b"abcd"), fingerprint)
assert secret != ""