MD5 hash encoding using Python?

PythonServer Side ProgrammingProgramming

One of the major concern of all IT companies in the security of there data. Multiple hashing techniques are there to project and check our data.

What is Hash

Hash is a function which takes variable length sequence of bytes as input and converts it to a fixed length sequence. However, to get your original data(input bytes) back is not easy. For example, x is your input and f is the f is the hashing function, then calculating f(x) is quick and easy but trying to obtain x again is a very time-consuming job.

The return value from a hash function is called hash, checksum, hash value or message digest.

We can summarize above as below −

Suppose you want to convert the “Hello World” message to md5 hash function then the result is a,

In real life scenario, hash functions are used heavily in cryptographic algorithms, in digital signatures, fingerprints, to store password and many more areas. As a python programmer, we need hash functions to check the duplicity of data or files, to check data integrity when you transmit data over a public network, storing the password in a database etc.

Two mostly used hash functions or algorithms are −

  • MD5 - MD5 or message digest algorithm will produce a 128-bit hash value. There are a couple of security issues with the md5 algorithm that is why we mainly used it to check data integrity.

  • SHA - There are multiple algorithms comes under SHA group of the algorithm, which is developed by U.S Federal Information processing standard. These algorithms are much more secure than md5 and hence widely used in several areas including cryptographic applications. The message generated by these algorithms ranges from 160 bits to 512 bits.

MD5

The Python standard library includes a module called hashlib, which contains most of the popular hashing algorithms. Most of the algorithms are implemented inside hashlib module however, if you have OpenSSL installed, hashlib can be used to work with these algorithms too.

First, if you want to use any hashing algorithm, import the hashlib module −

import hashlib

Now to check if the required algorithm or what all algorithms are currently available in hashlib module −

>>> print(hashlib.algorithms_available)
{'sha3_256', 'sha3_224', 'sha1', 'blake2b', 'sha512', 'dsaEncryption', 'dsaWithSHA', 'DSA', 'md5', 'sha384', 'sha224', 'sha3_384', 'ecdsa-with-SHA1', 'DSA-SHA', 'SHA1', 'md4', 'SHA256', 'MD4', 'sha3_512', 'whirlpool', 'sha256', 'shake_256', 'SHA', 'RIPEMD160', 'shake_128', 'SHA512', 'ripemd160', 'SHA224', 'sha', 'blake2s', 'SHA384', 'MD5'}

The above list of algorithms available through hashlib module includes the algorithms through OpenSSL.

However, to check the list of algorithms always available, we can check it through algorithms_guaranteed.

>>> print(hashlib.algorithms_guaranteed)
{'sha3_512', 'sha256', 'sha3_256', 'shake_256', 'sha3_224', 'sha1', 'blake2b', 'sha512', 'md5', 'shake_128', 'sha384', 'sha224', 'sha3_384', 'blake2s'}

Let’s create one simple program through md5 algorithm −

Code

 Live Demo

import hashlib
hash_obj = hashlib.md5(b'Hello, Python!')
print(hash_obj.hexdigest())

Output

a0af7810eb5fcb84c730f851361de06a

The code above takes the “Hello, Python!” string as input and prints the HEX digest of that string. Hexdigest returns a HEX string representing the hash.

However, if you want the sequence of bytes returned, you should use hash_obj.diget(), like −

 Live Demo

import hashlib
hash_obj = hashlib.md5(b'Hello, Python!')
#To generate, sequence of bytes
print(hash_obj.digest())

Output

b'\xa0\xafx\x10\xeb_\xcb\x84\xc70\xf8Q6\x1d\xe0j'

If you notice, we have used ‘b’ before any string literals, this is to generate the bytes from the string as hashing function only accept a sequence of bytes as a parameter.

In case you want to pass the string from the console, don’t forget to encode the string in a sequence of bytes −

 Live Demo

import hashlib
mystring = input('Enter string to hash: ')
hash_obj = hashlib.md5(mystring.encode())
print(hash_obj.hexdigest())

Output

Enter string to hash: Hello, TutorialsPoint
9a5d3fad65690dcf44adaec67226abe7
raja
Published on 20-Feb-2019 08:34:49
Advertisements