Hashing a file in Python

前端 未结 6 1149
误落风尘
误落风尘 2020-11-27 10:55

I want python to read to the EOF so I can get an appropriate hash, whether it is sha1 or md5. Please help. Here is what I have so far:

import hashlib

inputF         


        
6条回答
  •  难免孤独
    2020-11-27 11:49

    I would propose simply:

    def get_digest(file_path):
        h = hashlib.sha256()
    
        with open(file_path, 'rb') as file:
            while True:
                # Reading is buffered, so we can read smaller chunks.
                chunk = file.read(h.block_size)
                if not chunk:
                    break
                h.update(chunk)
    
        return h.hexdigest()
    

    All other answers here seem to complicate too much. Python is already buffering when reading (in ideal manner, or you configure that buffering if you have more information about underlying storage) and so it is better to read in chunks the hash function finds ideal which makes it faster or at lest less CPU intensive to compute the hash function. So instead of disabling buffering and trying to emulate it yourself, you use Python buffering and control what you should be controlling: what the consumer of your data finds ideal, hash block size.

提交回复
热议问题