Python Hashlib With Examples

In this tutorial, we are going to learn about Python Hashlib module. We can use python hashlib module to generate message digest or secure hash from the source message. In our previous tutorial we learnt about Python Math.

Python Hashlib

For generating python secure hash message, we need to use hashlib module.

Python hashlib hashing function takes variable length of bytes and converts it into a fixed length sequence. This is a one way function. That means, you hash a message, you get a fixed length sequence. But you cannot get the original message from those fixed length sequence.

In cryptography, a hash algorithm is considered to be better if the original message cannot be decrypted from the hash message. Also changing one byte in the original message makes significant change in the message digest value.

Python secure hash values are used in storing password in encrypted form. So even the application owner won’t have access to user password, passwords are matched when user enters the password again and hash value is calculated and compared with the stored value.

Available Hashing Algorithms

We can use algorithms_available function to get the list of all the algorithms available in the system, including those available through OpenSSl. Duplicate algorithm names can be seen also.

Again, by using algorithms_guaranteed function you can see the algorithms present in the module. See the following code.


import hashlib
print(hashlib.algorithms_available)
print(hashlib.algorithms_guaranteed)

For my system, the output was like this below. It can differ based on the operating system configurations.


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

Python hashlib example

To use Python hashlib module, you just have to know few functions. By using hashlib.encryption_algorithm_name(b"message") function, you can hash the whole message at once.

Also, you can use the update() function to append byte message to the secure hash value. In both case, the output will be same. Finally, by using digest() function you can get the secure hash.

Note that, b is written in the left of the message to be hashed. This b indicates that that string is a byte string. Let’s look at the hashlib example for more clarity.


import hashlib  # import hashlib module
# initialize using sha256
print('nExample for SHA256')
m = hashlib.sha256()
# append string one after another
m.update(b"This is a")
m.update(b" great python tutorial.")
print('Output 1 :', m.digest())
# use the whole string at once
x = hashlib.sha256(b"This is a great python tutorial.")
print('Output 2 :', x.digest())
# initialize using md5
print('nExample for md5')
m = hashlib.md5()
# append string one after another
m.update(b"This is a")
m.update(b" great python tutorial.")
print('Output 1 :', m.digest())
# use the whole string at once
x = hashlib.md5(b"This is a great python tutorial.")
print('Output 2 :', x.digest())

So the output will be

python-hashlib

So, this is all about Python hashlib module. Hope that you learned well. See the official reference to learn more about it.

By admin

Leave a Reply

%d bloggers like this: