Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set MD5 usedforsecurity=False for Python versions which support it #7620

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/prefect/utilities/hashing.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import hashlib
import sys
from functools import partial
from pathlib import Path
from typing import Optional, Union

import cloudpickle

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.
Expand All @@ -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:
Expand All @@ -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
Expand Down