insecure_hashlib.py 1008 B

1234567891011121314151617181920212223242526272829303132
  1. # Taken from https://github.com/mlflow/mlflow/pull/10119
  2. #
  3. # DO NOT use this function for security purposes (e.g., password hashing).
  4. #
  5. # In Python >= 3.9, insecure hashing algorithms such as MD5 fail in FIPS-compliant
  6. # environments unless `usedforsecurity=False` is explicitly passed.
  7. #
  8. # References:
  9. # - https://github.com/mlflow/mlflow/issues/9905
  10. # - https://github.com/mlflow/mlflow/pull/10119
  11. # - https://docs.python.org/3/library/hashlib.html
  12. # - https://github.com/huggingface/transformers/pull/27038
  13. #
  14. # Usage:
  15. # ```python
  16. # # Use
  17. # from huggingface_hub.utils.insecure_hashlib import sha256
  18. # # instead of
  19. # from hashlib import sha256
  20. #
  21. # # Use
  22. # from huggingface_hub.utils import insecure_hashlib
  23. # # instead of
  24. # import hashlib
  25. # ```
  26. import functools
  27. import hashlib
  28. md5 = functools.partial(hashlib.md5, usedforsecurity=False)
  29. sha1 = functools.partial(hashlib.sha1, usedforsecurity=False)
  30. sha256 = functools.partial(hashlib.sha256, usedforsecurity=False)