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 eac3a15
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions python/hash_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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()
#return hash.digest().hex()
return hash.hexdigest()



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

0 comments on commit eac3a15

Please sign in to comment.