Skip to content
Snippets Groups Projects
Commit 49d2e2b4 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Merge branch 'get-file-partial-hash' into 'master'

[download] allow get_file to only evaluate part of the hash

See merge request !127
parents 174954b9 c53384c4
No related branches found
No related tags found
1 merge request!127[download] allow get_file to only evaluate part of the hash
Pipeline #49013 passed
......@@ -149,13 +149,13 @@ def validate_file(fpath, file_hash, algorithm="auto", chunk_size=65535):
"""
# Code from https://github.com/tensorflow/tensorflow/blob/v2.3.1/tensorflow/python/keras/utils/data_utils.py#L312
# Very useful
if (algorithm == "sha256") or (algorithm == "auto" and len(file_hash) == 64):
hasher = "sha256"
else:
file_hash = str(file_hash)
if (algorithm == "md5") or (algorithm == "auto" and len(file_hash) == 32):
hasher = "md5"
else:
hasher = "sha256"
if str(_hash_file(fpath, hasher, chunk_size)) == str(file_hash):
if _hash_file(fpath, hasher, chunk_size).startswith(file_hash):
return True
else:
return False
......@@ -190,7 +190,7 @@ def _hash_file(fpath, algorithm="sha256", chunk_size=65535):
# Code from https://github.com/tensorflow/tensorflow/blob/v2.3.1/tensorflow/python/keras/utils/data_utils.py#L312
# Very useful
if (algorithm == "sha256") or (algorithm == "auto" and len(hash) == 64):
if algorithm == "sha256":
hasher = hashlib.sha256()
else:
hasher = hashlib.md5()
......@@ -199,7 +199,7 @@ def _hash_file(fpath, algorithm="sha256", chunk_size=65535):
for chunk in iter(lambda: fpath_file.read(chunk_size), b""):
hasher.update(chunk)
return hasher.hexdigest()
return str(hasher.hexdigest())
def get_file(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment