Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Add classmethod hash_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
xymy committed Mar 9, 2020
1 parent 09fac59 commit 7be40ea
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions xycrypto/hashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ def hash(cls, data, **kwargs):
return ctx.finalize()

@classmethod
def hash_fileobj(cls, fileobj, *, chunk_size=0x100000, **kwargs):
"""Return hash of data from file object."""

def hash_iter(cls, iterable, **kwargs):
ctx = cls(**kwargs)
for chunk in iter(functools.partial(fileobj.read, chunk_size), b''):
for chunk in iterable:
ctx.update(chunk)
return ctx.finalize()

@classmethod
def hash_fileobj(cls, fileobj, *, chunk_size=0x100000, **kwargs):
"""Return hash of data from file object."""

it = iter(functools.partial(fileobj.read, chunk_size), b'')
return cls.hash_iter(it, **kwargs)

@classmethod
def hash_file(cls, filepath, **kwargs):
"""Return hash of data from a file."""
Expand Down

0 comments on commit 7be40ea

Please sign in to comment.