Python Program to Find Hash of File

Python Program to Find Hash of File: In this article, you’ll learn to find the hash of a file and display it.

Hash functions take an arbitrary amount of data and return a fixed-length bit string. They are widely used in cryptography for authentication purposes.

Python Program to Calculate a Hash of a File

Source Code

# Python rogram to find the SHA-1 message digest of a file
# import hashlib module
import hashlib
def hash_file(filename):
   """"This function returns the SHA-1 hash
   of the file passed into it"""
   # make a hash object
   h = hashlib.sha1()
   # open file for reading in binary mode
   with open(filename,'rb') as file:
       # loop till the end of the file
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk =
   # return the hex representation of digest
   return h.hexdigest()
message = hash_file("track1.mp3")