little more cleanup

This commit is contained in:
Jonathan Wren 2022-09-24 06:00:24 -07:00
parent b3a662fd9f
commit c52dcfef5a
3 changed files with 20 additions and 7 deletions

View file

@ -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

View file

@ -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()

View file

@ -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: