Advertisement
DeaD_EyE

md5sum_snippet

Jun 9th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. import hashlib
  2.  
  3.  
  4. def gen_hash(file, chunk_size=512*1024**1, hash_algorithm='md5'):
  5.     """
  6.    file: str | pathobj
  7.    chunk_size: 512KiB | int
  8.    hash_algorithm: 'md5' | hashlib.algorithms_available
  9.    
  10.    Reads the file chunked and update the hash.
  11.    
  12.    returns: hash object
  13.    """
  14.     hasher = hashlib.new(hash_algorithm)
  15.     chunk = bytearray(chunk_size)
  16.     view = memoryview(chunk)
  17.     with open(file, 'rb') as fd:
  18.         while True:
  19.             last = fd.readinto(chunk)
  20.             if not last:
  21.                 break
  22.             hasher.update(view[:last])
  23.     return hasher
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement