Jump to content

Python 3: working with files and digital assets/File/Hashing

From Wikibooks, open books for an open world

Hashing of Files

[edit | edit source]
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