From 97f3290e020d842da4c969361884220c490c787c Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sun, 25 Sep 2022 18:38:47 -0700 Subject: [PATCH] general cleanup --- jrnl/Journal.py | 4 ++-- jrnl/encryption/BaseEncryption.py | 2 +- jrnl/encryption/BasePasswordEncryption.py | 10 +++++----- jrnl/encryption/Jrnlv2Encryption.py | 12 +++++++----- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 68808e18..33e6840c 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -79,7 +79,7 @@ class Journal: self.entries = list(frozenset(self.entries) | frozenset(imported_entries)) self.sort() - def _get_encryption_method(self): + def _get_encryption_method(self) -> None: encryption_method = determine_encryption_method(self.config["encrypt"]) self.encryption_method = encryption_method(self.name, self.config) @@ -380,7 +380,7 @@ class Journal: class PlainJournal(Journal): def _load(self, filename): - with open(filename, "r") as f: + with open(filename, "r", encoding="utf-8") as f: return f.read() def _store(self, filename, text): diff --git a/jrnl/encryption/BaseEncryption.py b/jrnl/encryption/BaseEncryption.py index 0bbec442..e4480bfa 100644 --- a/jrnl/encryption/BaseEncryption.py +++ b/jrnl/encryption/BaseEncryption.py @@ -39,7 +39,7 @@ class BaseEncryption(ABC): pass @abstractmethod - def _decrypt(self, text: bytes) -> str | None: + def _decrypt(self, text: bytes | str) -> str | None: """ This is needed because self.decrypt might need to perform actions (e.g. prompt for password) diff --git a/jrnl/encryption/BasePasswordEncryption.py b/jrnl/encryption/BasePasswordEncryption.py index 0f09284a..7d5927b3 100644 --- a/jrnl/encryption/BasePasswordEncryption.py +++ b/jrnl/encryption/BasePasswordEncryption.py @@ -9,13 +9,13 @@ from jrnl.prompt import prompt_password class BasePasswordEncryption(BaseEncryption): _attempts: int _max_attempts: int - _password: str | None + _password: str def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._attempts = 0 self._max_attempts = 3 - self._password = None + self._password = "" # Check keyring first for password. # That way we'll have it. @@ -31,12 +31,12 @@ class BasePasswordEncryption(BaseEncryption): self._password = value def encrypt(self, text): - if self.password is None: + if not self.password: self.password = create_password(self._journal_name) return self._encrypt(text) - def decrypt(self, text: bytes) -> str: - if self.password is None: + def decrypt(self, text: str) -> str: + if not self.password: self._prompt_password() while (result := self._decrypt(text)) is None: self._prompt_password() diff --git a/jrnl/encryption/Jrnlv2Encryption.py b/jrnl/encryption/Jrnlv2Encryption.py index 11747841..07e195bb 100644 --- a/jrnl/encryption/Jrnlv2Encryption.py +++ b/jrnl/encryption/Jrnlv2Encryption.py @@ -12,12 +12,10 @@ from .BasePasswordEncryption import BasePasswordEncryption class Jrnlv2Encryption(BasePasswordEncryption): - _salt: bytes - _key: bytes - def __init__(self, *args, **kwargs) -> None: # Salt is hard-coded self._salt: bytes = b"\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8" + self._key: bytes = b"" super().__init__(*args, **kwargs) @@ -49,8 +47,12 @@ class Jrnlv2Encryption(BasePasswordEncryption): .decode(self._encoding) ) - def _decrypt(self, text: bytes) -> str | None: + def _decrypt(self, text: str) -> str | None: try: - return Fernet(self._key).decrypt(text).decode(self._encoding) + return ( + Fernet(self._key) + .decrypt(text.encode(self._encoding)) + .decode(self._encoding) + ) except (InvalidToken, IndexError): return None