From 20326d84bff8153da2cb33e2fad1b78a7c04809c Mon Sep 17 00:00:00 2001 From: mrnoob790 <44310679+mrnoob790@users.noreply.github.com> Date: Wed, 7 Oct 2020 14:18:29 +0000 Subject: [PATCH] python program to find a hash file In this program, we open the file in binary mode. Hash functions are available in the hashlib module. We loop till the end of the file using a while loop. On reaching the end, we get empty bytes object. In each iteration, we only read 1024 bytes (this value can be changed according to our wish) from the file and update the hashing function. --- Find Hash of File | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Find Hash of File diff --git a/Find Hash of File b/Find Hash of File new file mode 100644 index 0000000..7181cc9 --- /dev/null +++ b/Find Hash of File @@ -0,0 +1,27 @@ +# Python program to find the SHA-1 message digest of a file + +# importing the 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 = file.read(1024) + h.update(chunk) + + # return the hex representation of digest + return h.hexdigest() + +message = hash_file("track1.mp3") +print(message)