[docs]@staticmethoddefget_available_digests()->list[str]:"""Returns list of fixed-size digest algorithms in :mod:`hashlib`. Uses :data:`hashlib.algorithms_guaranteed` because :data:`hashlib.algorithms_available` may result in runtime errors due to deprecated algorithms being hidden by OpenSSL. :return: guaranteed algorithms in :mod:`hashlib` that produce a fixed-size digest """returnsorted(algoforalgoinhashlib.algorithms_guaranteedifhashlib.new(algo).digest_size!=0)
[docs]@classmethoddefverify_digest(cls,path:str|os.PathLike[str],algo:str,digest:str)->None:"""Verify file digest and raise :class:`ValueError` in case of mismatch. :param path: input file path :param algo: hash algorithm name accepted by :func:`hashlib.new` :param digest: hexadecimal digest string to verify :raises ValueError: ``digest`` has incorrect length or fails verification """hash_obj=hashlib.new(algo)digest_name=hash_obj.name.upper()log.debug("Computing %s-bit %s for %s",hash_obj.digest_size*8,digest_name,path)ifhash_obj.digest_size*2!=len(digest):raiseValueError(f"Expected {digest_name} for {path} has length != "f"{hash_obj.digest_size} B")withopen(path,"rb")aspath_obj:whilechunk:=path_obj.read(cls.FILE_CHUNK_BYTES):hash_obj.update(chunk)ifhash_obj.hexdigest().lower()!=digest.lower():raiseValueError(f"{digest_name} mismatch for {path}")log.info("Successfully verified %s of %s",digest_name,path)