From c52dcfef5aa74b75f25e1a53291b022f93d2a4a3 Mon Sep 17 00:00:00 2001 From: Jonathan Wren Date: Sat, 24 Sep 2022 06:00:24 -0700 Subject: [PATCH] little more cleanup --- jrnl/encryption/BaseEncryption.py | 20 +++++++++++++++----- jrnl/encryption/BasePasswordEncryption.py | 5 ++++- jrnl/encryption/Jrnlv2Encryption.py | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/jrnl/encryption/BaseEncryption.py b/jrnl/encryption/BaseEncryption.py index 859ed3ad..34f59bcb 100644 --- a/jrnl/encryption/BaseEncryption.py +++ b/jrnl/encryption/BaseEncryption.py @@ -9,17 +9,27 @@ class BaseEncryption(ABC): self._config = config @abstractmethod - def encrypt(self, text: str) -> str: + def encrypt(self, text: str) -> bytes: pass @abstractmethod - def decrypt(self, text: str) -> str | None: + def decrypt(self, text: bytes) -> str | None: pass @abstractmethod - def _decrypt(self, text: str) -> str: + def _encrypt(self, text: bytes) -> str: """ - This is needed because self.decrypt needs - to get a password on decryption failures + This is needed because self.decrypt might need + to perform actions (e.g. prompt for password) + before actually encrypting. + """ + pass + + @abstractmethod + def _decrypt(self, text: bytes) -> str: + """ + This is needed because self.decrypt might need + to perform actions (e.g. prompt for password) + before actually decrypting. """ pass diff --git a/jrnl/encryption/BasePasswordEncryption.py b/jrnl/encryption/BasePasswordEncryption.py index 93999320..f6ad4c1c 100644 --- a/jrnl/encryption/BasePasswordEncryption.py +++ b/jrnl/encryption/BasePasswordEncryption.py @@ -28,7 +28,10 @@ class BasePasswordEncryption(BaseEncryption): if self._password is None: self._prompt_password() - def decrypt(self, text: str) -> str: + def encrypt(self, text: str) -> bytes: + return self._encrypt(text) + + def decrypt(self, text: bytes) -> str: encoded_text = text.encode(self._encoding) while (result := self._decrypt(encoded_text)) is None: self._prompt_password() diff --git a/jrnl/encryption/Jrnlv2Encryption.py b/jrnl/encryption/Jrnlv2Encryption.py index a189e373..13d2b362 100644 --- a/jrnl/encryption/Jrnlv2Encryption.py +++ b/jrnl/encryption/Jrnlv2Encryption.py @@ -36,7 +36,7 @@ class Jrnlv2Encryption(BasePasswordEncryption): key = kdf.derive(password) self._key = base64.urlsafe_b64encode(key) - def encrypt(self, text: str) -> bytes: + def _encrypt(self, text: str) -> bytes: return Fernet(self._key).encrypt(text.encode(self._encoding)) def _decrypt(self, text: bytes) -> str | None: