Write a Python Program to Find Hash of File

Python Program to Find Hash of File

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program to Find Hash of File
Python Program to Find Hash of File

Write a Python Program to Find Hash of File

PROGRAM: Python Program to Find Hash of File

/* Python Program to Find Hash of File */

# Python program to find the SHA-1 message digest of a file

# importing the hashlib module
import hashlib

def hash_file(filepath):
“”””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(filepath,’rb’) as file:

# loop till the end of the file
chunk = 0
while chunk != b”:
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)

# return the hex representation of digest
return h.hexdigest()

message = hash_file(“track1.mp3”) # Use path of the file
print(message)

OUTPUT:-

633d7356947eec543c50b76a1852f92427f4dca9
 

Leave a Reply

Your email address will not be published. Required fields are marked *