Hashing a file in Python

前端 未结 6 1173
误落风尘
误落风尘 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条回答
  •  萌比男神i
    2020-11-27 11:31

    Here is a Python 3, POSIX solution (not Windows!) that uses mmap to map the object into memory.

    import hashlib
    import mmap
    
    def sha256sum(filename):
        h  = hashlib.sha256()
        with open(filename, 'rb') as f:
            with mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) as mm:
                h.update(mm)
        return h.hexdigest()
    

提交回复
热议问题