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")
| 155 | |
| 156 | |
| 157 | def 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 | |
| 191 | def download_url( |
searching dependent graphs…