Cryptographic hashes are used in day-day life like in digital signatures, message authentication codes, manipulation detection, fingerprints, checksums (message integrity check), hash tables, password storage and much more. They are also used in sending messages over network for security or storing messages in databases.
There are many hash functions defined in the “hashlib” library in python.
This article deals with explanation and working of MD5 hash.
This hash function accepts sequence of bytes and returns 128 bit hash value, usually used to check data integrity but has security issues.
Functions associated :
- encode() : Converts the string into bytes to be acceptable by hash function.
- digest() : Returns the encoded data in byte format.
- hexdigest() : Returns the encoded data in hexadecimal format.
The below code demonstrates the working of MD5 hash accepting bytes and output as bytes.
# Python 3 code to demonstrate the # working of MD5 (byte - byte) import hashlib # encoding GeeksforGeeks using md5 hash # function result = hashlib.md5(b'GeeksforGeeks') # printing the equivalent byte value. print("The byte equivalent of hash is : ", end ="") print(result.digest()) |
Output:
The byte equivalent of hash is : b'\xf1\xe0ix~\xcetS\x1d\x11%Y\x94\\hq'
Explanation : The above code takes byte and can be accepted by the hash function. The md5 hash function encodes it and then using digest(), byte equivalent encoded string is printed.
Below code demonstrated how to take string as input and output hexadecimal equivalent of the encoded value.
# Python 3 code to demonstrate the # working of MD5 (string - hexadecimal) import hashlib # initializing string str2hash = "GeeksforGeeks" # encoding GeeksforGeeks using encode() # then sending to md5() result = hashlib.md5(str2hash.encode()) # printing the equivalent hexadecimal value. print("The hexadecimal equivalent of hash is : ", end ="") print(result.hexdigest()) |
Output:
The hexadecimal equivalent of hash is : f1e069787ece74531d112559945c6871
Explanation : The above code takes string and converts it into the byte equivalent using encode() so that it can be accepted by the hash function. The md5 hash function encodes it and then using hexdigest(), hexadecimal equivalent encoded string is printed.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Difference between MD5 and SHA1
- Generating hash id's using uuid3() and uuid5() in Python
- Python | hash() method
- Python 3.6 dictionary implementation using hash tables
- Whirlpool Hash Function in Python
- Python Program to print hollow half diamond hash pattern
- Full domain Hashing with variable Hash size in Python
- Passwords and Cryptographic hash function
- Hash Functions in System Security
- SHA-256 Hash in Java
- RIPEMD Hash Function
- Blockchain Hash Function
- Cryptographic Hash Function in Java
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : andecker91


