Created
December 2, 2019 10:16
-
-
Save vakhov/e8427ba5dc35b432c9e6b47ec5381e1b to your computer and use it in GitHub Desktop.
Hashing a file in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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