Skip to content

Instantly share code, notes, and snippets.

@vakhov
Created December 2, 2019 10:16
Show Gist options
  • Select an option

  • Save vakhov/e8427ba5dc35b432c9e6b47ec5381e1b to your computer and use it in GitHub Desktop.

Select an option

Save vakhov/e8427ba5dc35b432c9e6b47ec5381e1b to your computer and use it in GitHub Desktop.
Hashing a file in Python
def get_hash_file(file_name: str, buf_size: Optional[int] = None) -> Dict[str, str]:
BUF_SIZE = buf_size or 65536
md5 = hashlib.md5()
sha1 = hashlib.sha1()
with open(file_name, 'rb') as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
sha1.update(data)
return dict(md5=md5.hexdigest(), sha1=sha1.hexdigest())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment