2021-09-13 09:52:50 +00:00
|
|
|
import datetime
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def timestamp():
|
|
|
|
return int(datetime.datetime.now().timestamp())
|
|
|
|
|
|
|
|
|
|
|
|
def file_hash(path: str, algo: str, block_size: int = 4096):
|
|
|
|
h = hashlib.new(algo)
|
2022-06-26 12:00:03 +00:00
|
|
|
with open(path, "rb") as fd:
|
|
|
|
while True:
|
|
|
|
data = fd.read(block_size)
|
|
|
|
if len(data) > 0:
|
|
|
|
h.update(data)
|
|
|
|
else:
|
|
|
|
break
|
2021-09-13 09:52:50 +00:00
|
|
|
return h.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
def file_md5(path, block_size=4096):
|
|
|
|
return file_hash(path, "md5", block_size)
|
|
|
|
|
|
|
|
|
|
|
|
def file_sha256(path, block_size=4096):
|
|
|
|
return file_hash(path, "sha256", block_size)
|