Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/starlette/_compat.py: 100%

3 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:12 +0000

1import hashlib 

2 

3# Compat wrapper to always include the `usedforsecurity=...` parameter, 

4# which is only added from Python 3.9 onwards. 

5# We use this flag to indicate that we use `md5` hashes only for non-security 

6# cases (our ETag checksums). 

7# If we don't indicate that we're using MD5 for non-security related reasons, 

8# then attempting to use this function will raise an error when used 

9# environments which enable a strict "FIPs mode". 

10# 

11# See issue: https://github.com/encode/starlette/issues/1365 

12try: 

13 

14 # check if the Python version supports the parameter 

15 # using usedforsecurity=False to avoid an exception on FIPS systems 

16 # that reject usedforsecurity=True 

17 hashlib.md5(b"data", usedforsecurity=False) # type: ignore[call-arg] 

18 

19 def md5_hexdigest( 

20 data: bytes, *, usedforsecurity: bool = True 

21 ) -> str: # pragma: no cover 

22 return hashlib.md5( # type: ignore[call-arg] 

23 data, usedforsecurity=usedforsecurity 

24 ).hexdigest() 

25 

26except TypeError: # pragma: no cover 

27 

28 def md5_hexdigest(data: bytes, *, usedforsecurity: bool = True) -> str: 

29 return hashlib.md5(data).hexdigest()