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

Commit

Permalink
Fix classmethod hash_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
xymy committed Mar 16, 2020
1 parent bf09c82 commit 10efac5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
27 changes: 16 additions & 11 deletions xycrypto/hashes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abc
import functools
import hashlib
import itertools
import os

__all__ = [
Expand Down Expand Up @@ -86,18 +85,24 @@ def hash_file(cls, filepath, **kwargs):
def hash_dir(cls, dirpath, **kwargs):
"""Return hash of data from directory."""

with os.scandir(dirpath) as it:
result = itertools.repeat(0)
for entry in it:
if entry.is_dir():
value = cls.hash_dir(entry, **kwargs)
else:
value = cls.hash_file(entry, **kwargs)
result = bytes(x ^ y for x, y in zip(result, value))
if not isinstance(result, bytes):
raise RuntimeError('empty directory')
try:
digest_size = getattr(cls, 'digest_size')
except AttributeError:
digest_size = getattr(cls(**kwargs), 'digest_size')

def _hash_dir(cls, dirpath, **kwargs):
result = b'\x00' * digest_size
with os.scandir(dirpath) as it:
for entry in it:
if entry.is_dir():
value = cls.hash_dir(entry, **kwargs)
else:
value = cls.hash_file(entry, **kwargs)
result = bytes(x ^ y for x, y in zip(result, value))
return result

return _hash_dir(cls, dirpath, **kwargs)

@classmethod
def hash_fs(cls, path, **kwargs):
"""Return hash of data from filesystems."""
Expand Down
24 changes: 13 additions & 11 deletions xycrypto/hmac.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import itertools
import os
from hmac import compare_digest

Expand Down Expand Up @@ -102,18 +101,21 @@ def hash_file(cls, hash_cls, key, filepath, **kwargs):
def hash_dir(cls, hash_cls, key, dirpath, **kwargs):
"""Return hash of data from directory."""

with os.scandir(dirpath) as it:
result = itertools.repeat(0)
for entry in it:
if entry.is_dir():
value = cls.hash_dir(hash_cls, key, entry, **kwargs)
else:
value = cls.hash_file(hash_cls, key, entry, **kwargs)
result = bytes(x ^ y for x, y in zip(result, value))
if not isinstance(result, bytes):
raise RuntimeError('empty directory')
digest_size = getattr(cls(hash_cls, key, **kwargs), 'digest_size')

def _hash_dir(cls, hash_cls, key, dirpath, **kwargs):
result = b'\x00' * digest_size
with os.scandir(dirpath) as it:
for entry in it:
if entry.is_dir():
value = cls.hash_dir(hash_cls, key, entry, **kwargs)
else:
value = cls.hash_file(hash_cls, key, entry, **kwargs)
result = bytes(x ^ y for x, y in zip(result, value))
return result

return _hash_dir(cls, hash_cls, key, dirpath, **kwargs)

@classmethod
def hash_fs(cls, hash_cls, key, path, **kwargs):
"""Return hash of data from filesystems."""
Expand Down

0 comments on commit 10efac5

Please sign in to comment.