Skip to content

Commit

Permalink
add python/hash_file.py
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed May 19, 2024
1 parent bf24539 commit 36d4f38
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions python/hash_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python3



import hashlib



def get_hash(file_path=None, data=None, algo="sha1"):
# https://stackoverflow.com/questions/22058048/hashing-a-file-in-python
hash_func = getattr(hashlib, algo)
if data:
return hash_func(data).digest()
assert file_path
# BUF_SIZE is totally arbitrary, change for your app!
BUF_SIZE = 65536 # lets read stuff in 64kb chunks!
hash = hash_func()
with open(file_path, 'rb') as f:
while data := f.read(BUF_SIZE):
#md5.update(data)
hash.update(data)
return hash.digest()



if __name__ == "__main__":
import sys
file_path = sys.argv[1]
hash = get_hash(file_path).hex()
print(f"{hash} {file_path}")

0 comments on commit 36d4f38

Please sign in to comment.