mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-13 18:08:30 +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.entries = list(frozenset(self.entries) | frozenset(imported_entries))
|
||||||
self.sort()
|
self.sort()
|
||||||
|
|
||||||
def _get_encryption_method(self):
|
def _get_encryption_method(self) -> None:
|
||||||
encryption_method = determine_encryption_method(self.config["encrypt"])
|
encryption_method = determine_encryption_method(self.config["encrypt"])
|
||||||
self.encryption_method = encryption_method(self.name, self.config)
|
self.encryption_method = encryption_method(self.name, self.config)
|
||||||
|
|
||||||
|
@ -380,7 +380,7 @@ class Journal:
|
||||||
|
|
||||||
class PlainJournal(Journal):
|
class PlainJournal(Journal):
|
||||||
def _load(self, filename):
|
def _load(self, filename):
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
def _store(self, filename, text):
|
def _store(self, filename, text):
|
||||||
|
|
|
@ -39,7 +39,7 @@ class BaseEncryption(ABC):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def _decrypt(self, text: bytes) -> str | None:
|
def _decrypt(self, text: bytes | str) -> str | None:
|
||||||
"""
|
"""
|
||||||
This is needed because self.decrypt might need
|
This is needed because self.decrypt might need
|
||||||
to perform actions (e.g. prompt for password)
|
to perform actions (e.g. prompt for password)
|
||||||
|
|
|
@ -9,13 +9,13 @@ from jrnl.prompt import prompt_password
|
||||||
class BasePasswordEncryption(BaseEncryption):
|
class BasePasswordEncryption(BaseEncryption):
|
||||||
_attempts: int
|
_attempts: int
|
||||||
_max_attempts: int
|
_max_attempts: int
|
||||||
_password: str | None
|
_password: str
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._attempts = 0
|
self._attempts = 0
|
||||||
self._max_attempts = 3
|
self._max_attempts = 3
|
||||||
self._password = None
|
self._password = ""
|
||||||
|
|
||||||
# Check keyring first for password.
|
# Check keyring first for password.
|
||||||
# That way we'll have it.
|
# That way we'll have it.
|
||||||
|
@ -31,12 +31,12 @@ class BasePasswordEncryption(BaseEncryption):
|
||||||
self._password = value
|
self._password = value
|
||||||
|
|
||||||
def encrypt(self, text):
|
def encrypt(self, text):
|
||||||
if self.password is None:
|
if not self.password:
|
||||||
self.password = create_password(self._journal_name)
|
self.password = create_password(self._journal_name)
|
||||||
return self._encrypt(text)
|
return self._encrypt(text)
|
||||||
|
|
||||||
def decrypt(self, text: bytes) -> str:
|
def decrypt(self, text: str) -> str:
|
||||||
if self.password is None:
|
if not self.password:
|
||||||
self._prompt_password()
|
self._prompt_password()
|
||||||
while (result := self._decrypt(text)) is None:
|
while (result := self._decrypt(text)) is None:
|
||||||
self._prompt_password()
|
self._prompt_password()
|
||||||
|
|
|
@ -12,12 +12,10 @@ from .BasePasswordEncryption import BasePasswordEncryption
|
||||||
|
|
||||||
|
|
||||||
class Jrnlv2Encryption(BasePasswordEncryption):
|
class Jrnlv2Encryption(BasePasswordEncryption):
|
||||||
_salt: bytes
|
|
||||||
_key: bytes
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(self, *args, **kwargs) -> None:
|
||||||
# Salt is hard-coded
|
# Salt is hard-coded
|
||||||
self._salt: bytes = b"\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8"
|
self._salt: bytes = b"\xf2\xd5q\x0e\xc1\x8d.\xde\xdc\x8e6t\x89\x04\xce\xf8"
|
||||||
|
self._key: bytes = b""
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -49,8 +47,12 @@ class Jrnlv2Encryption(BasePasswordEncryption):
|
||||||
.decode(self._encoding)
|
.decode(self._encoding)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _decrypt(self, text: bytes) -> str | None:
|
def _decrypt(self, text: str) -> str | None:
|
||||||
try:
|
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):
|
except (InvalidToken, IndexError):
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Add table
Reference in a new issue