diff --git a/src/prefect/utilities/hashing.py b/src/prefect/utilities/hashing.py index 02732d69fab9..204acf76dea7 100644 --- a/src/prefect/utilities/hashing.py +++ b/src/prefect/utilities/hashing.py @@ -1,4 +1,6 @@ import hashlib +import sys +from functools import partial from pathlib import Path from typing import Optional, Union @@ -6,8 +8,13 @@ from prefect.serializers import JSONSerializer +if sys.version_info[:2] >= (3, 9): + _md5 = partial(hashlib.md5, usedforsecurity=False) +else: + _md5 = hashlib.md5 -def stable_hash(*args: Union[str, bytes], hash_algo=hashlib.md5) -> str: + +def stable_hash(*args: Union[str, bytes], hash_algo=_md5) -> str: """Given some arguments, produces a stable 64-bit hash of their contents. Supports bytes and strings. Strings will be UTF-8 encoded. @@ -27,7 +34,7 @@ def stable_hash(*args: Union[str, bytes], hash_algo=hashlib.md5) -> str: return h.hexdigest() -def file_hash(path: str, hash_algo=hashlib.md5) -> str: +def file_hash(path: str, hash_algo=_md5) -> str: """Given a path to a file, produces a stable hash of the file contents. Args: @@ -41,7 +48,7 @@ def file_hash(path: str, hash_algo=hashlib.md5) -> str: return stable_hash(contents, hash_algo=hash_algo) -def hash_objects(*args, hash_algo=hashlib.md5, **kwargs) -> Optional[str]: +def hash_objects(*args, hash_algo=_md5, **kwargs) -> Optional[str]: """ Attempt to hash objects by dumping to JSON or serializing with cloudpickle. On failure of both, `None` will be returned