STINNER Victor added the comment:

> It makes sense to allow hashlib.update accept file like object
> to read from.

Not update directly, but I agree that an helper would be convinient.

Here is another proposition using unbuffered file and readinto() with 
bytearray. It should be faster, but I didn't try with a benchmark. I also wrote 
two functions, because sometimes you have a file object, not a file path.

---
import hashlib, sys

def hash_readfile_obj(obj, fp, buffersize=64 * 1024):
    buffer = bytearray(buffersize)
    while True:
        size = fp.readinto(buffer)
        if not size:
            break
        if size == buffersize:
            obj.update(buffer)
        else:
            obj.update(buffer[:size])

def hash_readfile(obj, filepath, buffersize=64 * 1024):
    with open(filepath, 'rb', buffering=0) as fp:
        hash_readfile_obj(obj, fp, buffersize)

def file_sha256(filepath):
    sha = hashlib.sha256()
    hash_readfile(sha, filepath)
    return sha.hexdigest()

for name in sys.argv[1:]:
    print("%s %s" % (file_sha256(name), name))
---

readfile() and readfile_obj() should be methods of an hash object.

----------
nosy: +haypo

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17436>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to