Python Examples

Python Program to Find Hash of the FileĀ 

Python file hash program uses Hash method. Hash is a method available in the Python library. In programming languages, the hash method is used to get integer values that can be used to compare dictionary keys. The comparison can be done through the feature of dictionary lookup.Ā 

As Python is an object-oriented language, each element that is used in the program is consideredĀ an object and assigned a unique integer value.Ā 

python file hashĀ  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā Ā Source

The syntax of the hash method as used in Python hash fileĀ  is as follows:Ā 

hash (object)

In the method, the only parameter that needs to be passed is the object. The object is the one for which the hash value needs to be obtained by the user. The hash value obtained can be in the form of an integer, a float number, or an integer.Ā 

Hash functions are mainly used to receive any amount of data. The function then returns a string or an integer of a fixed length. This output that is received by the hash function is referred to as a digest message.Ā 

Thus, hash functions can be used for identification and authentication.Ā 

Source Code to Find HashĀ 

While creating the program for a Python hash file, we provide it with a value through which it can be authenticated in the future. The hash value provided will be generated by the function and will be a string, float, or integer of a determined length.Ā 

As the size of a file can be quite large, the complete file is not entered into the memory all at once. Instead, the file is divided into pieces. The code below shows how a Python hash file is done.Ā 

                    

import hashlibĀ 

def hashing (name_of_file):

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā h = hashlib.sha1 ()

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā with open (name_of_file, ā€˜rbā€™) as file:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā piece = 0

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while chunk != b ā€˜ ā€˜:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā piece = file.read (1024)

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā h.update (piece)

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return h.hexdigest ()

output1 = hashing (ā€œimage1.jpgā€)

print (output1)

In the above program, the selected file is opened in binary mode. At the beginning of the program, the hashlib module is imported. The module contains the hash functions.Ā 

To hash the file, a WHILE loop is used so that the end of the file is reached. Once the end of the selected file is reached, it returns empty bite objects.Ā 

Each iteration of the WHILE loop will read only 1024 bytes. The value of the number of bytes that will be read through each iteration can be changed as per the wish of the user and the size of the file.Ā 

The digest message, that is, the output that is returned by the hash function is returned in the form of a hexadecimal digit. Thus, the hexdigest () method is used. You can run any file yourself and find the digest message that you obtain for each file.Ā 

Note that the program above uses SHA 1. Another hashing algorithm can be used for Python file hash as well. One such example of a hashing algorithm that is used in cryptography is MD5. The code for this algorithm will remain the same as well. However, the digest message received will be different.Ā 

The code below shows how the MD5 algorithm can be used to hash a file in Python. One should note that the MD5 hashing algorithm is also available in the hashlib module of Python.Ā Ā 

                    

import sysĀ 

import hashlib

SIZE = 32768

md_5 = hashlib.md5 ()

with open (ā€˜code.pyā€™, ā€˜rbā€™) as f:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while True:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā data = f.read (SIZE)

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if not data:

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break

Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā md5.update (data)

print (ā€œMD5:ā€, format (md5.hexdigest()))

The program also first imports the hashlib and sys modules. The selected file is converted into binary form after which it is broken into chunks of 32 kb. The file is then fed to the hashing function and the digest message is obtained.Ā Ā 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.