mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-13 18:08:30 +02:00
little more cleanup
This commit is contained in:
parent
b3a662fd9f
commit
c52dcfef5a
3 changed files with 20 additions and 7 deletions
|
@ -9,17 +9,27 @@ class BaseEncryption(ABC):
|
||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def encrypt(self, text: str) -> str:
|
def encrypt(self, text: str) -> bytes:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def decrypt(self, text: str) -> str | None:
|
def decrypt(self, text: bytes) -> str | None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _decrypt(self, text: str) -> str:
|
def _encrypt(self, text: bytes) -> str:
|
||||||
"""
|
"""
|
||||||
This is needed because self.decrypt needs
|
This is needed because self.decrypt might need
|
||||||
to get a password on decryption failures
|
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
|
pass
|
||||||
|
|
|
@ -28,7 +28,10 @@ class BasePasswordEncryption(BaseEncryption):
|
||||||
if self._password is None:
|
if self._password is None:
|
||||||
self._prompt_password()
|
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)
|
encoded_text = text.encode(self._encoding)
|
||||||
while (result := self._decrypt(encoded_text)) is None:
|
while (result := self._decrypt(encoded_text)) is None:
|
||||||
self._prompt_password()
|
self._prompt_password()
|
||||||
|
|
|
@ -36,7 +36,7 @@ class Jrnlv2Encryption(BasePasswordEncryption):
|
||||||
key = kdf.derive(password)
|
key = kdf.derive(password)
|
||||||
self._key = base64.urlsafe_b64encode(key)
|
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))
|
return Fernet(self._key).encrypt(text.encode(self._encoding))
|
||||||
|
|
||||||
def _decrypt(self, text: bytes) -> str | None:
|
def _decrypt(self, text: bytes) -> str | None:
|
||||||
|
|
Loading…
Add table
Reference in a new issue