def chunked_hasher(hash_object: HASH, file_handle) -> str: """Calculate the hash of a file without reading it into memory completely""" while True: data = file_handle.read(BUF_SIZE) if not data: break hash_object.update(data) return hash_object.hexdigest()
def get_sha1_hash_from_dir(directory: Union[str, Path], hash: Hash = None) -> str: assert Path(directory).is_dir() if hash is None: hash = hashlib.sha1() for path in sorted(Path(directory).iterdir()): hash.update(path.name.encode()) if path.is_file(): hash = get_sha1_from_file(path, hash) elif path.is_dir(): hash = get_sha1_hash_from_dir(path, hash) return hash.hexdigest()