python 摘要算法应用 MD5 SHA256

参考博客:https://blog.csdn.net/qq_42951560/article/details/125080544

import hashlib

def MessageDigestAlgorithm(fpath: str, algorithm: str) -> str:
    f = open(fpath, 'rb')
    hash = hashlib.new(algorithm)
    for chunk in iter(lambda: f.read(2**20), b''):
        hash.update(chunk)
    f.close()
    return hash.hexdigest()


if __name__ == '__main__':
    # algorithm = "md5"
    algorithm = "sha256"

    file_path = "D:/software/uiso/system_iso/cn_windows_10_consumer_editions_version_20h2_updated_april_2021_x64_dvd_ace7e59c.iso"

    hexdigest = MessageDigestAlgorithm(file_path, algorithm)
    print(f'{algorithm}: {hexdigest}')

文章目录