From ce4df8ee236db0eeff4467be1e5c733f362dccee Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 1 Oct 2022 14:25:23 -0700 Subject: [PATCH] Fix obscure Windows line ending issue with decode See https://bugs.python.org/issue40863 --- jrnl/Journal.py | 2 +- jrnl/encryption/BasePasswordEncryption.py | 2 +- jrnl/encryption/Jrnlv2Encryption.py | 8 ++------ jrnl/encryption/NoEncryption.py | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 2dfc1f34..22f49c35 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -157,7 +157,7 @@ class Journal: return f.read() def _store(self, filename, text): - with open(filename, "w", encoding="utf-8") as f: + with open(filename, "wb") as f: f.write(text) def _parse(self, journal_txt): diff --git a/jrnl/encryption/BasePasswordEncryption.py b/jrnl/encryption/BasePasswordEncryption.py index 34a04542..af0ba88d 100644 --- a/jrnl/encryption/BasePasswordEncryption.py +++ b/jrnl/encryption/BasePasswordEncryption.py @@ -30,7 +30,7 @@ class BasePasswordEncryption(BaseEncryption): def password(self, value: str) -> None: self._password = value - def encrypt(self, text: str) -> str: + def encrypt(self, text: str) -> bytes: if not self.password: self.password = create_password(self._journal_name) return self._encrypt(text) diff --git a/jrnl/encryption/Jrnlv2Encryption.py b/jrnl/encryption/Jrnlv2Encryption.py index 8baffda5..f66a4f16 100644 --- a/jrnl/encryption/Jrnlv2Encryption.py +++ b/jrnl/encryption/Jrnlv2Encryption.py @@ -40,12 +40,8 @@ class Jrnlv2Encryption(BasePasswordEncryption): key = kdf.derive(password) self._key = base64.urlsafe_b64encode(key) - def _encrypt(self, text: str) -> str: - return ( - Fernet(self._key) - .encrypt(text.encode(self._encoding)) - .decode(self._encoding) - ) + def _encrypt(self, text: str) -> bytes: + return Fernet(self._key).encrypt(text.encode(self._encoding)) def _decrypt(self, text: bytes) -> str | None: try: diff --git a/jrnl/encryption/NoEncryption.py b/jrnl/encryption/NoEncryption.py index f8e0dd3d..13edcec3 100644 --- a/jrnl/encryption/NoEncryption.py +++ b/jrnl/encryption/NoEncryption.py @@ -7,8 +7,8 @@ class NoEncryption(BaseEncryption): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - def _encrypt(self, text: str) -> str: - return text + def _encrypt(self, text: str) -> bytes: + return text.encode(self._encoding) def _decrypt(self, text: bytes) -> str: return text.decode(self._encoding)