Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- def gen_hash(file, chunk_size=512*1024**1, hash_algorithm='md5'):
- """
- file: str | pathobj
- chunk_size: 512KiB | int
- hash_algorithm: 'md5' | hashlib.algorithms_available
- Reads the file chunked and update the hash.
- returns: hash object
- """
- hasher = hashlib.new(hash_algorithm)
- chunk = bytearray(chunk_size)
- view = memoryview(chunk)
- with open(file, 'rb') as fd:
- while True:
- last = fd.readinto(chunk)
- if not last:
- break
- hasher.update(view[:last])
- return hasher
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement