get md5 hash of string

How to Get MD5 Hash of String in Python

MD5 hash value calculation is a useful step in cryptography as well as user authentication. It basically takes a string and returns an encrypted alphanumeric string that is near impossible to decrypt. It is also used for verifying file transfers, password storage and more. Python provides an in-built Hashlib library that offers many useful hash functions that allow you to easily get the md5 hash of string. In this article, we will look at how to get MD5 Hash of string in python.


How to Get MD5 Hash of String in Python

MD5 hash function accepts a sequence of bytes and returns a 128-bit hash value. We need 3 functions for this purpose.

  • encode() : Converts a string into bytes to be acceptable by hash function.
  • digest() : Returns 128-bit encoded data in byte format.
  • hexdigest() : Returns encoded data in hexadecimal format.

Now we will look at a few examples of MD5 hash calculation.

import hashlib

sample = b'good morning'
encrypt = hashlib.md5(sample)
encrypt.digest()
'+\x84\x95\x00\xe4X]\xabA\x96\xec\x9aA^\xdf\x8f
print(encrypt.digest())
+トユ

In the above example, we have defined a string variable ‘sample’ with binary encoding. So we can directly pass this to hashlib.md5 function for encryption. We use digest function to display the encoded result.

Many times you may not have a binary string as input. In such cases, you need to convert it into binary sequence before you pass it to md5 hash function.

Here is an example where we use a plain text string and convert it to binary sequence using encode() function before passing it to md5 function.

import hashlib

sample = "good morning"
encrypt = hashlib.md5(sample.encode())
encrypt.digest()
encrypt.digest()
'+\x84\x95\x00\xe4X]\xabA\x96\xec\x9aA^\xdf\x8f'
encrypt.hexdigest()
'2b849500e4585dab4196ec9a415edf8f'

In the above example, we have converted a string into binary sequence and passed it to md5 hash function. Finally, we have displayed the encoded string as hex format.

Generally, this step is used as a part of a larger function or application. For example, here is an example where we find md5 has of a file data.txt, by looping through its contents and passing them to md5 hash function.

import hashlib

if __name__ == '__main__':
    file_name = 'data.txt'
    with open(file_name, 'rb') as f:
        bytes = f.read()
        hash_value = hashlib.md5(bytes).hexdigest()
        print(hash_value)

In this article, we have learnt how to encrypt string using md5 algorithm. We have also looked at two use cases – one where you can directly pass binary string for encryption and the other where you convert a plain string to binary sequence and then pass it for hashing.

Also read:

How to Split string by delimiter in Python
How to Read File Line by Line in Linux Shel Script
How to Recover Deleted Files in Linux
How to Use Key-Value Dictionary in Shell Script
How to Block Website in Linux

Leave a Reply

Your email address will not be published.