From f783d292023844c64428b27b8827846dfcb43be0 Mon Sep 17 00:00:00 2001 From: Aroy-Art <Aroy-Art@pm.me> Date: Tue, 25 Feb 2025 11:07:25 +0100 Subject: [PATCH] Add: utils func to compute md5 hash of files --- backend/utils/hash.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/utils/hash.py b/backend/utils/hash.py index 03ae563..2cfd211 100644 --- a/backend/utils/hash.py +++ b/backend/utils/hash.py @@ -1,3 +1,4 @@ +import hashlib from blake3 import blake3 from blurhash import encode @@ -51,6 +52,27 @@ def compute_string_hash_blake3(string, logger=None): return compute_blake3_hash(string, is_file=False, logger=logger) +def compute_md5_hash(file_path): + """ + Compute the MD5 hash of a file. + + Args: + file_path (str): Path to the file. + + Returns: + str: MD5 hash of the file. + """ + try: + hash_md5 = hashlib.md5() + with open(file_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + hash_md5.update(chunk) + return hash_md5.hexdigest() + except Exception as e: + tqdm.write(f"Error computing MD5 hash: {e}") + return None + + def compute_blur_hash(image_path, components_x=4, components_y=4, logger=None): """ Compute the BlurHash of an image.