66import hashlib
77from datetime import datetime
88from pathlib import Path
9- from typing import Any , Callable , Dict , List , Literal , Optional , Union
9+ from typing import Callable , Dict , List , Literal , Optional , Union
1010
1111from pydantic import BaseModel
1212
@@ -18,14 +18,28 @@ class Checksums(BaseModel):
1818 md5 : str
1919 sha256 : str
2020
21+ @staticmethod
22+ def get_hasher (func : Callable [..., hashlib ._Hash ]) -> hashlib ._Hash :
23+ # In Python 3.9+, some hash algorithms (like md5, sha1, sha256) may be disabled in FIPS-compliant systems
24+ # unless 'usedforsecurity=False' is specified. This flag allows the hash function to be used for non-security
25+ # purposes such as checksums, even in FIPS environments.
26+ try :
27+ return func (usedforsecurity = False )
28+ except TypeError :
29+ return func ()
30+
2131 @classmethod
2232 def generate (cls , file_ : Path ) -> Checksums :
2333 block_size : int = 65536
24- mapping : dict [str , Callable [[], Any ]] = {"md5" : hashlib .md5 , "sha1" : hashlib .sha1 , "sha256" : hashlib .sha256 }
34+ mapping = {
35+ "md5" : hashlib .md5 ,
36+ "sha1" : hashlib .sha1 ,
37+ "sha256" : hashlib .sha256 ,
38+ }
2539 results = {}
2640
2741 for algorithm , hashing_function in mapping .items ():
28- hasher = hashing_function ( )
42+ hasher = cls . get_hasher ( hashing_function )
2943 with file_ .absolute ().open ("rb" ) as fd :
3044 buf = fd .read (block_size )
3145 while len (buf ) > 0 :
0 commit comments