MD5 Hash Function: Implementation in Python

Hello coders!! In this article, we will be learning about MD5 in Python. We will discuss in detail its meaning, implementation, and application. Now without wasting any time, let’s get into the topic.

What is MD5?

MD5 stands for the message-digest algorithm. It is a hash function that produces a 128-bit hash value. This is used as a checksum to verify data integrity. It is suitable for non-cryptographic purposes like determining the partition for a particular key in a partitioned database.

MD5 hash in Python:

This hash function is available in the hashlib module of Python. It takes a sequence of bytes as input and returns the 128-bit hash value as output. The primary use of the hash function is to check data integrity, but it has security issues.

Associated Functions with md5:

  • encode():  to convert the string into bytes
  • digest():  to returns the encoded data in byte format
  • hexdigest():  to returns the encoded data in hexadecimal format

Example 1: Printing Byte Equivalent of the MD5 Hash in Python

import hashlib 

result = hashlib.md5(b'Python Pool') 
print("Hash Value : ", end ="")
print(result)
print("Equivalent Byte : ", end ="") 
print(result.digest()) 

Output & Explanation:

python md5 example
Output

In this code, we take byte input, which is acceptable by the hash function. We then encoded this value using the md5 hash function. Lastly, we generated the byte equivalent of the encoded string using the digest() function.

Example 2: Printing the Hexadecimal Equivalent of the MD5 Hash in Python

import hashlib 

result = hashlib.md5('Python Pool'.encode()) 
print("Hash Value : ", end ="")
print(result)
print("Hexadecimal Equivalent : ", end ="") 
print(result.hexdigest()) 

Output & Explanation:

Printing the Hexadecimal Equivalent of the MD5 Hash
Output

Here, we converted the string into its byte equivalent using the encode() function, making it acceptable by the hash function. We then used the md5 function to encode it, and lastly, using the hexdigest() function, its hexadecimal equivalent is displayed.

Example 3: Python MD5 File Checksum

md5_hash = hashlib.md5()

file = open("test.txt", "rb")
content = file.read()
md5_hash.update(content)

result = md5_hash.hexdigest()
print(result)

Output & Explanation

Example 3: Python MD5 File Checksum
Output

In this code, hashlib.md5() function is invoked to create an MD5 object. We have opened a file with ‘rb’ mode where rb stands for ‘read bytes.’ using the read() method, we read the file’s contents into a variable. The update() method updates the file contents. Finally, using the hexdigest() method, we have converted the hash into its hexadecimal equivalent.

Example 4: Encode a string in MD5 using Python

import hashlib 
string = "pythonpool.com"
encoded=string.encode()
result = hashlib.md5(encoded)
print("String : ", end ="")
print(string)
print("Hash Value : ", end ="")
print(result)
print("Hexadecimal equivalent: ",result.hexdigest())

Output & Explanation:

Encode a string
Output

In this example, we used the hashlib.md5() function to encode the string value into a hash value. We then used the hexdigest() method to get the hexadecimal equivalent of the generated hash value. Similarly, we can also use the digest() method to get the byte equivalent of the generated hash value.

Example 5: Calculating MD5 hash of a file in Python

import hashlib

with open("sample.txt","rb") as f:
    bytes = f.read()
    print("Bytes read from the file:",bytes)
    result = hashlib.md5(bytes)
    print("Hah Value: ",result)
    print("The hexadecimal equivalent: ")
    print(result.hexdigest())

Output & Explanation:

Calculate MD5 hash of a file
Output

In this code, we first created a sample text file. We then read the contents of that file in bytes. We converted the bytes into a hash value and then finally saw the hexadecimal equivalent for the same.

Applications:

  • Used in the software world to ensure that the transferred file is intact
  • It is also used electronic discovery by providing a unique identifier for each document that is exchanged during the legal discovery process

Advantages:

  • provides security input of any size can be taken to produce an output if a 128-bit
  • generation of a message digest of the original message is straightforward
  • a message digest of a message having any number of bits can be performed

Disdavtages:

  • It is prone to hash collision weakness
  • security over these collision attacks is not provided
  • quite slow in comparison to the optimized SHA Algorithm

Conclusion:

In this article, we discussed the md5 hash function in python. We saw various examples for the same. We also learned about its various applications.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments