clean up logging statements

This commit is contained in:
Jonathan Wren 2022-11-05 16:01:09 -07:00
parent dcdee69117
commit 45b05b9d34
No known key found for this signature in database
5 changed files with 14 additions and 8 deletions

View file

@ -12,17 +12,17 @@ from jrnl.messages import MsgText
class BaseEncryption(ABC):
def __init__(self, journal_name: str, config: dict):
logging.debug("BaseEncryption.__init__ start")
logging.debug("start")
self._encoding: str = "utf-8"
self._journal_name: str = journal_name
self._config: dict = config
def encrypt(self, text: str) -> bytes:
logging.debug("BaseEncryption.encrypt start")
logging.debug("encrypting")
return self._encrypt(text)
def decrypt(self, text: bytes) -> str:
logging.debug("BaseEncryption.decrypt start")
logging.debug("decrypting")
if (result := self._decrypt(text)) is None:
raise JrnlException(
Message(MsgText.DecryptionFailedGeneric, MsgStyle.ERROR)

View file

@ -15,7 +15,7 @@ from jrnl.prompt import prompt_password
class BasePasswordEncryption(BaseEncryption):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
logging.debug("BasePasswordEncryption.__init__ start")
logging.debug("start")
self._attempts: int = 0
self._max_attempts: int = 3
self._password: str = ""
@ -34,12 +34,13 @@ class BasePasswordEncryption(BaseEncryption):
self._password = value
def encrypt(self, text: str) -> bytes:
logging.debug("encrypting")
if not self.password:
self.password = create_password(self._journal_name)
return self._encrypt(text)
def decrypt(self, text: bytes) -> str:
logging.debug("BasePasswordEncryption decrypt start")
logging.debug("decrypting")
if not self.password:
self._prompt_password()

View file

@ -15,12 +15,13 @@ from jrnl.encryption.BasePasswordEncryption import BasePasswordEncryption
class Jrnlv1Encryption(BasePasswordEncryption):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
logging.debug("Jrnlv1Encryption.__init__ start")
logging.debug("start")
def _encrypt(self, _: str) -> bytes:
raise NotImplementedError
def _decrypt(self, text: bytes) -> str | None:
logging.debug("decrypting")
iv, cipher = text[:16], text[16:]
password = self._password or ""
decryption_key = hashlib.sha256(password.encode(self._encoding)).digest()

View file

@ -19,7 +19,7 @@ class Jrnlv2Encryption(BasePasswordEncryption):
self._key: bytes = b""
super().__init__(*args, **kwargs)
logging.debug("Jrnlv2Encryption.__init__ start")
logging.debug("start")
@property
def password(self):
@ -43,9 +43,11 @@ class Jrnlv2Encryption(BasePasswordEncryption):
self._key = base64.urlsafe_b64encode(key)
def _encrypt(self, text: str) -> bytes:
logging.debug("encrypting")
return Fernet(self._key).encrypt(text.encode(self._encoding))
def _decrypt(self, text: bytes) -> str | None:
logging.debug("decrypting")
try:
return Fernet(self._key).decrypt(text).decode(self._encoding)
except (InvalidToken, IndexError):

View file

@ -7,11 +7,13 @@ from jrnl.encryption.BaseEncryption import BaseEncryption
class NoEncryption(BaseEncryption):
def __init__(self, *args, **kwargs):
logging.debug("NoEncryption.__init__ start")
super().__init__(*args, **kwargs)
logging.debug("start")
def _encrypt(self, text: str) -> bytes:
logging.debug("encrypting")
return text.encode(self._encoding)
def _decrypt(self, text: bytes) -> str:
logging.debug("decrypting")
return text.decode(self._encoding)