From 6c20b8c5342cda999bb511a7ffaf716cf1b1c319 Mon Sep 17 00:00:00 2001 From: Wout Bouckaert Date: Thu, 15 Aug 2024 21:00:39 -0600 Subject: [PATCH] Fix get_path_from_hash. Add required functions to crypto helper. --- app/classes/helpers/cryptography_helper.py | 37 ++++++++++++++++++++++ app/classes/shared/backup_mgr.py | 5 +-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/classes/helpers/cryptography_helper.py b/app/classes/helpers/cryptography_helper.py index ae0b0afb..5b2d05fb 100644 --- a/app/classes/helpers/cryptography_helper.py +++ b/app/classes/helpers/cryptography_helper.py @@ -11,4 +11,41 @@ class CryptoHelper: @staticmethod def bytes_to_b64(input_bytes: bytes) -> str: + """ + Converts input bytes to base64 encoded string. + + Args: + input_bytes: Input bytes for conversion. + + Returns: String of base64 encoded bytes. + + """ + # base64.b64encode(input_bytes).decode("UTF-8") appends a trailing new line. + # That newline is getting pulled off of the string before returning it. return base64.b64encode(input_bytes).decode("UTF-8").rstrip("\n") + + @staticmethod + def b64_to_bytes(input_str: str) -> bytes: + """ + Converts base64 encoded string to bytes. + + Args: + input_str: Base64 bytes encodes as a string. + + Returns: Bytes from base64 encoded string. + + """ + return base64.b64decode(input_str) + + @staticmethod + def bytes_to_hex(input_bytes: bytes) -> str: + """ + Converts input bytes to hex encoded string. + + Args: + input_bytes: Bytes to be encoded as hex string. + + Returns: Bytes encoded as hex string. + + """ + return input_bytes.hex() diff --git a/app/classes/shared/backup_mgr.py b/app/classes/shared/backup_mgr.py index 8b7afc4b..8e799291 100644 --- a/app/classes/shared/backup_mgr.py +++ b/app/classes/shared/backup_mgr.py @@ -345,7 +345,6 @@ class BackupManager: Returns: None """ - print(manifest) # Create file path for depends file depends_file_path = ( backup_repository / "manifest_files" / f"{backup_id}.depends" @@ -408,4 +407,6 @@ class BackupManager: # Repo path: /path/to/backup/repo/ # Hash: 1234...890 # Example: /path/to/backup/repo/data/12/34...890 - return repository / "data" / str(file_hash[:2]) / str(file_hash[-126:]) + file_hash = helper.crypto_helper.b64_to_bytes(file_hash) + file_hash = helper.crypto_helper.bytes_to_hex(file_hash) + return repository / "data" / file_hash[:2] / str(file_hash[-126:])