MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / check_hash

Function check_hash

monai/apps/utils.py:157–188  ·  view source on GitHub ↗

Verify hash signature of specified file. Args: filepath: path of source file to verify hash value. val: expected hash value of the file. hash_type: type of hash algorithm to use, default is `"md5"`. The supported hash types are `"md5"`, `"sha1"`, `"sha25

(filepath: PathLike, val: str | None = None, hash_type: str = "md5")

Source from the content-addressed store, hash-verified

155
156
157def check_hash(filepath: PathLike, val: str | None = None, hash_type: str = "md5") -> bool:
158 """
159 Verify hash signature of specified file.
160
161 Args:
162 filepath: path of source file to verify hash value.
163 val: expected hash value of the file.
164 hash_type: type of hash algorithm to use, default is `"md5"`.
165 The supported hash types are `"md5"`, `"sha1"`, `"sha256"`, `"sha512"`.
166 See also: :py:data:`monai.apps.utils.SUPPORTED_HASH_TYPES`.
167
168 """
169 if val is None:
170 logger.info(f"Expected {hash_type} is None, skip {hash_type} check for file {filepath}.")
171 return True
172 actual_hash_func = look_up_option(hash_type.lower(), SUPPORTED_HASH_TYPES)
173
174 actual_hash = actual_hash_func(usedforsecurity=False) # allows checks on FIPS enabled machines
175
176 try:
177 with open(filepath, "rb") as f:
178 for chunk in iter(lambda: f.read(1024 * 1024), b""):
179 actual_hash.update(chunk)
180 except Exception as e:
181 logger.error(f"Exception in check_hash: {e}")
182 return False
183 if val != actual_hash.hexdigest():
184 logger.error(f"check_hash failed {actual_hash.hexdigest()}.")
185 return False
186
187 logger.info(f"Verified '{_basename(filepath)}', {hash_type}: {val}.")
188 return True
189
190
191def download_url(

Callers 8

test_resultMethod · 0.90
test_hash_type_errorMethod · 0.90
download_urlFunction · 0.85
extractallFunction · 0.85

Calls 5

look_up_optionFunction · 0.90
_basenameFunction · 0.85
infoMethod · 0.80
readMethod · 0.45
updateMethod · 0.45

Tested by 6

test_resultMethod · 0.72
test_hash_type_errorMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…