utils.py 564 B

123456789101112131415161718192021222324252627
  1. import datetime
  2. import hashlib
  3. import os
  4. def timestamp():
  5. return int(datetime.datetime.now().timestamp())
  6. def file_hash(path: str, algo: str, block_size: int = 4096):
  7. fd = open(path, "rb")
  8. h = hashlib.new(algo)
  9. while True:
  10. data = fd.read(block_size)
  11. if len(data) > 0:
  12. h.update(data)
  13. else:
  14. break
  15. return h.hexdigest()
  16. def file_md5(path, block_size=4096):
  17. return file_hash(path, "md5", block_size)
  18. def file_sha256(path, block_size=4096):
  19. return file_hash(path, "sha256", block_size)