From 74787b51baeef90e683727c953576ebd4807ccbb Mon Sep 17 00:00:00 2001 From: Wout Bouckaert Date: Sat, 10 Aug 2024 19:29:42 -0600 Subject: [PATCH] Update calculate_blake2b_hash to use boolean comparison rather than string Change output_format parameter from a string to boolean output_as_bytes for comparison efficiency. Default behavior will still output a hex string. --- app/classes/shared/file_helpers.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/classes/shared/file_helpers.py b/app/classes/shared/file_helpers.py index b10049e8..426a8d2b 100644 --- a/app/classes/shared/file_helpers.py +++ b/app/classes/shared/file_helpers.py @@ -152,13 +152,15 @@ class FileHelpers: @staticmethod def calculate_blake2b_hash( - value_to_hash: pathlib.Path | bytes, output_format: str = "hex" + value_to_hash: pathlib.Path | bytes, output_as_bytes: bool = False ) -> str or bytes: """ Calculates blake2b hash from either file at specified Path, or from supplied bytes. Output will either be a hex string or bytes. Hex string is the default. + :param value_to_hash: Path to file or bytes to hash. - :param output_format: Output format, "hex" for hex in str, "bytes" for bytes. + :param output_as_bytes: Boolean to output as bytes. Defaults to False. + Otherwise, will output as hex string. output. Defaults to "hex". :return: blake2b hash string or bytes. """ @@ -203,9 +205,9 @@ class FileHelpers: ) from why # Return hash in specified format. - if output_format == "hex": - return blake2.hexdigest() - return blake2.digest() + if output_as_bytes: + return blake2.digest() + return blake2.hexdigest() @staticmethod def calculate_file_hash_sha256(file_path: str) -> str: