extend hashing method

This commit is contained in:
Maximilian Keßler 2022-02-08 23:33:52 +01:00
parent 80036d3dda
commit 288d2cdef4

View file

@ -1,12 +1,22 @@
import hashlib
from typing import Union, List, Set
from pathlib import Path
# https://stackoverflow.com/a/3431838/16371376
def md5(file: Path):
def md5(data: Union[Path, List[str], Set[str]]):
hash_md5 = hashlib.md5()
with open(file, "rb") as f:
for block in iter(lambda: f.read(4096), b""):
hash_md5.update(block)
return hash_md5.hexdigest()
if isinstance(data, Path):
file: Path = data
with open(file, "rb") as f:
for block in iter(lambda: f.read(4096), b""):
hash_md5.update(block)
else:
if isinstance(data, list):
set_data: Set[str] = set(data)
else:
set_data = data
hash_md5.update(str(set_data).encode('UTF-8'))
return hash_md5.hexdigest()