extend hashing method
This commit is contained in:
parent
80036d3dda
commit
288d2cdef4
1 changed files with 15 additions and 5 deletions
|
@ -1,12 +1,22 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
|
from typing import Union, List, Set
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# https://stackoverflow.com/a/3431838/16371376
|
# https://stackoverflow.com/a/3431838/16371376
|
||||||
|
|
||||||
|
|
||||||
def md5(file: Path):
|
def md5(data: Union[Path, List[str], Set[str]]):
|
||||||
hash_md5 = hashlib.md5()
|
hash_md5 = hashlib.md5()
|
||||||
|
if isinstance(data, Path):
|
||||||
|
file: Path = data
|
||||||
with open(file, "rb") as f:
|
with open(file, "rb") as f:
|
||||||
for block in iter(lambda: f.read(4096), b""):
|
for block in iter(lambda: f.read(4096), b""):
|
||||||
hash_md5.update(block)
|
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()
|
return hash_md5.hexdigest()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue