mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-13 09:58:31 +02:00
general cleanup
This commit is contained in:
parent
41a5af533d
commit
97f3290e02
4 changed files with 15 additions and 13 deletions
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue