Hashing a file in Python

前端 未结 6 1145
误落风尘
误落风尘 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:43

    For the correct and efficient computation of the hash value of a file (in Python 3):

    • Open the file in binary mode (i.e. add 'b' to the filemode) to avoid character encoding and line-ending conversion issues.
    • Don't read the complete file into memory, since that is a waste of memory. Instead, sequentially read it block by block and update the hash for each block.
    • Eliminate double buffering, i.e. don't use buffered IO, because we already use an optimal block size.
    • Use readinto() to avoid buffer churning.

    Example:

    import hashlib
    
    def sha256sum(filename):
        h  = hashlib.sha256()
        b  = bytearray(128*1024)
        mv = memoryview(b)
        with open(filename, 'rb', buffering=0) as f:
            for n in iter(lambda : f.readinto(mv), 0):
                h.update(mv[:n])
        return h.hexdigest()
    

提交回复
热议问题