move encoding to a constant and have same encoding for both encryption and decryption

This commit is contained in:
revsuine 2024-11-06 20:47:12 +00:00
parent dc7bebde92
commit ff3fb5e431
Signed by: revsuine
GPG key ID: 3F257B68F5BC9339

View file

@ -38,6 +38,9 @@ import typing
# see: https://gnupg.readthedocs.io/en/latest/
import gnupg
# constants
ENCODING='utf-8' # default is latin-1 which fails w some unicode chars
def is_message_encrypted(message: email.message.Message) -> bool:
"""Determines whether or not an email message is encrypted.
@ -69,7 +72,7 @@ def encrypt(
return message.as_string()
gpg = gnupg.GPG()
# gpg.encoding = 'utf-8' # default is latin-1 which fails w some unicode chars
gpg.encoding = ENCODING
encrypted_content = gpg.encrypt(message.as_string(), recipients, armor=True)
if not encrypted_content:
raise ValueError(encrypted_content.status)
@ -108,7 +111,9 @@ def encrypt(
def decrypt(message: email.message.Message) -> str:
"""Decrypt the given message
Likely won't work on this server as I don't store private keys"""
return str(gnupg.GPG().decrypt(message.as_string()))
gpg = gnupg.GPG()
gpg.encoding = ENCODING
return str(gpg.decrypt(message.as_string()))
def main() -> None:
"""Program entry"""