跳转到内容

Python 3:文件和数字资产/文件/哈希

来自维基教科书,开放世界中的开放书籍

文件的哈希值

[编辑 | 编辑源代码]
pip install pillow
pip install requests
%pip install pillow
%pip install requests
import hashlib
def hash_file_sha1(
    location_of_file: str
) -> str:
    value: str | None = None
    hash_algorithm = hashlib.sha1()

    with open(
        location_of_file, 
        'rb'
    ) as file:
        chunk = 0

        while chunk != b'':
            chunk = file.read(1024)
            hash_algorithm.update(chunk)

    value = str(
        hash_algorithm.hexdigest()
    )

    return value
华夏公益教科书