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.Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 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.Ā Ā
Leave a Reply