As a Python enthusiast, I’m always on the lookout for powerful and practical techniques to incorporate into my projects. One such technique that has proven invaluable in ensuring data integrity is creating an MD5 hash of a string. Whether it’s for password storage, data validation, or checksum verification, MD5 hashing has become a go-to method for developers worldwide. In this blog post, I’m excited to guide you through the process of creating an MD5 hash of a string in Python. So, let’s dive in and discover the wonders of MD5 hashing!
MD5 is (atleast when it was created) a standardized 1-way function that takes in data input of any form and maps it to a fixed-size output string, irrespective of the size of the input string.
Though it is used as a cryptographic hash function, it has been found to suffer from a lot of vulnerabilities.
The hash function generates the same output hash for the same input string. This means that, you can use this string to validate files or text or anything when you pass it across the network or even otherwise. MD5 can act as a stamp or for checking if the data is valid or not.
For example –
Input String | Output Hash |
---|---|
hi | 49f68a5c8493ec2c0bf489821c21fc3b |
debugpointer | d16220bc73b8c7176a3971c7f73ac8aa |
computer science is amazing! I love it. | f3c5a497380310d828cdfc1737e8e2a3 |
MD5 hash of a String in Python
MD5 hash can be created using the python’s default module hashlib
. There are many more hash functions defined in the hashlib
library.
The process of creating an MD5 hash in python is very simple. First import hashlib, then encode your string that you want to hash i.e., converts the string into the byte equivalent using encode(), then pass it through the hashlib.md5()
function. We print the hexdigest
value of the hash m
, which is the hexadecimal equivalent encoded string.
Working code example-
import hashlib
text = 'Hello!'
m = hashlib.md5(text.encode('UTF-8'))
print(m.hexdigest())
Output of the above code-
d41d8cd98f00b204e9800998ecf8427e
The value you see here d41d8cd98f00b204e9800998ecf8427e
is the MD5 hash of the string Hello!
.
The functions used in the above code-
- encode() : Converts the string into bytes to be acceptable by hash function.
- hexdigest() : Returns the encoded data in hexadecimal format.
You can also update the value of the string and check it as well if needed. This can be used to strengthen the hash logic in your workflow where you can append strings in certain order and see if your hash matched the source hash.
import hashlib
text = 'Hello!'
m = hashlib.md5()
print(m.hexdigest())
m.update(b"Have Fun!")
print(m.hexdigest())
m.update(text.encode('UTF-8'))
print(m.hexdigest())
Output of the above code-
24187a7aa74385955a8546c99e0c2b6a
As you see, the MD5 hash of a string using Python is as simple as this code.
The above code just produced MD5 hash of the string alone, but, to strengthen the security you can also generate MD5 hash with salt as well.
In case you are looking to create MD5 hash of a file or a blob check out the article.
NOTE : Please do not use this to hash passwords and store it in your databases, prefer SHA-256 or SHA-512 or other superior cryptographic hash functions for the same.
I’m glad that you found the content useful. And there you have it! We’ve successfully explored the world of MD5 hashing in Python. By learning how to create an MD5 hash of a string, you’ve added a powerful tool to your Python arsenal. From securing passwords to verifying data integrity, MD5 hashing offers a range of applications in various domains. As you continue your programming journey, remember the importance of data integrity and the role that MD5 hashing plays in ensuring it. Keep exploring, keep learning, and may your Python projects be secure and robust! Happy Coding.