feat(wallpaper_changer): add FileInfo class, remove overloading of decript func, move to src
add FIleInfo class for naming_table entries remove overloading of decrypt function, return None instead
This commit is contained in:
1 parent
ecb7ae032e
commit
a72a38f37f
1 file changed
+22
-27
+22
-27
@@ -1,3 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
from os import PathLike
|
||||
@@ -11,6 +13,11 @@ from wallpaper_changer.logging_utils import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@dataclass
|
||||
class FileInfo:
|
||||
image_name: str
|
||||
encrypted_file: str
|
||||
image_hash: str
|
||||
|
||||
class EncryptionManager:
|
||||
"""Encrypt and decrypt files using the legacy OpenSSL enc file format."""
|
||||
@@ -42,6 +49,7 @@ class EncryptionManager:
|
||||
raise FileNotFoundError(f"Naming-table source file does not exist: {source_path}")
|
||||
if source_path.resolve() == destination_path.resolve():
|
||||
raise ValueError("Naming-table source and destination must be different")
|
||||
logger.info(f"Encrypting {destination_path}")
|
||||
self.encrypt(source_path, destination_path, key)
|
||||
source_path.unlink(missing_ok=True)
|
||||
|
||||
@@ -57,6 +65,9 @@ class EncryptionManager:
|
||||
raise FileNotFoundError(f"Naming-table source file does not exist: {source_path}")
|
||||
if source_path.resolve() == destination_path.resolve():
|
||||
raise ValueError("Naming-table source and destination must be different")
|
||||
|
||||
logger.info(f"Decrypting {destination_path}")
|
||||
|
||||
self.decrypt(source_path, destination_path, key)
|
||||
source_path.unlink(missing_ok=True)
|
||||
|
||||
@@ -78,56 +89,40 @@ class EncryptionManager:
|
||||
|
||||
def load_naming_table(
|
||||
self, src: str | PathLike[str], key: str | bytes | PathLike[str]
|
||||
) -> list[tuple[str, str]]:
|
||||
) -> list[FileInfo]:
|
||||
encrypted_path = Path(src)
|
||||
if not encrypted_path.exists():
|
||||
return []
|
||||
|
||||
content = self.decrypt(encrypted_path, None, key)
|
||||
content,_ = self.decrypt(encrypted_path, None, key)
|
||||
if content is None:
|
||||
raise ValueError(f"Failed to read the naming table: {encrypted_path}")
|
||||
|
||||
naming_table = []
|
||||
for line in content.decode("utf-8").splitlines():
|
||||
if line.strip() and ";" in line:
|
||||
original_name, encrypted_name = line.split(";", 1)
|
||||
naming_table.append((original_name, encrypted_name))
|
||||
original_name, encrypted_name, file_hash = line.split(";", 2)
|
||||
naming_table.append(FileInfo(original_name, encrypted_name, file_hash))
|
||||
return naming_table
|
||||
|
||||
def store_naming_table(
|
||||
self,
|
||||
naming_table: list[tuple[str, str]],
|
||||
naming_table: list[FileInfo],
|
||||
dst: str | PathLike[str],
|
||||
key: str | bytes | PathLike[str],
|
||||
) -> None:
|
||||
content = "".join(
|
||||
f"{original_name};{encrypted_name}\n"
|
||||
for original_name, encrypted_name in naming_table
|
||||
f"{item.image_name};{item.encrypted_file};{item.image_hash}\n"
|
||||
for item in naming_table
|
||||
).encode("utf-8")
|
||||
self.encrypt_data(content, dst, key)
|
||||
|
||||
@overload
|
||||
def decrypt(
|
||||
self,
|
||||
src: str | PathLike[str],
|
||||
dst: None,
|
||||
key: str | bytes | PathLike[str],
|
||||
) -> bytes: ...
|
||||
|
||||
@overload
|
||||
def decrypt(
|
||||
self,
|
||||
src: str | PathLike[str],
|
||||
dst: str | PathLike[str],
|
||||
key: str | bytes | PathLike[str],
|
||||
) -> None: ...
|
||||
|
||||
def decrypt(
|
||||
self,
|
||||
src: str | PathLike[str],
|
||||
dst: str | PathLike[str] | None,
|
||||
key: str | bytes | PathLike[str],
|
||||
) -> bytes | None:
|
||||
) -> tuple[bytes | None, str]:
|
||||
encrypted = Path(src).read_bytes()
|
||||
header_size = len(self._MAGIC) + self._SALT_SIZE
|
||||
ciphertext = encrypted[header_size:]
|
||||
@@ -144,11 +139,11 @@ class EncryptionManager:
|
||||
padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
|
||||
unpadder = padding.PKCS7(self._BLOCK_SIZE).unpadder()
|
||||
plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
|
||||
|
||||
hash = hashlib.sha256(plaintext).hexdigest()
|
||||
if dst is None:
|
||||
return plaintext
|
||||
return plaintext, hash
|
||||
Path(dst).write_bytes(plaintext)
|
||||
return None
|
||||
return None,hash
|
||||
|
||||
@classmethod
|
||||
def _derive_key_and_iv(
|
||||
Reference in new issue
Block a user