Securing Your Data Using Hashlib Library in Python

Have you ever come across words like encryption, decryption, hashing, security? If you have never heard about it or want to know more about it, it is for you. Today, we will discuss a fantastic library in python known as hashlib, which is generally used for encoding.  

Encoding means changing the value of the original string into another string, generally for security purposes. ‘hashlib’ library uses hashing functions to encode the strings. This Library consists of many different functions which perform transforms a string to other string in different ways. Let us now go through different hashing functions that can be used for encrypting the original value into some other value. 

Why to hashlib in python? 

The best thing about hashing is that it is a one-way function. It means that if we applied a hashing function on a particular string, it could not be converted into the original string. And this is how it is different from encryption. This is the reason why hashing is so widely used in storing the passwords. Let suppose you are signing up on a website, and you saved your password on that website, and that password is common to all your other accounts. If there were no such means as hashing, that website owner could have used your password and may have taken advantage of it. But due to hashing, that value has been changed, and the owner cannot even change it back to the original value. 

Syntax of hashlib python- 

Hashlib is a built-in library. So, you can use it directly in your system just by importing it. There are three main functions which you should know before knowing more about this Library. 

  1. Update()– The string that you want to encrypt should be used as the argument in update function.  
  2. encode()– update() function requires encoded string, so we first need to apply encode() on the string, otherwise we will get ‘typeerror unicode objects must be encoded before hashing python hashlib‘.
  3. hexdigest()– This function is used to get the encrypted string. We can also use the digest(), but that does not give us a clean output.   

Algorithms in hashlib Library in python- 

There are many different functions/ algorithms available in the hashlib Library. Some algorithms are there in all the systems, and some are specific to your system.  

To know what your system supports all algorithms, use the ‘hashlib.algorithms_available.’ Attribute. 

To know what all algorithms are there in every system, use ‘hashlib.algorithms_guaranteed’. attribute. 

Let’s see how we can do this- 

# To know the algorithms available in your system, use algorithms_available
{'blake2b', 'blake2b512', 'blake2s', 'blake2s256', 'md4', 'md5', 'md5-sha1', 'mdc2', 'ripemd160', 'sha1', 'sha224', 'sha256', 'sha3-224', 'sha3-256', 'sha3-384', 'sha3-512', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', 'sha512', 'sha512-224', 'sha512-256', 'shake128', 'shake256', 'shake_128', 'shake_256', 'sm3', 'whirlpool'}

Note – The output will differ in your system.

# To know the algorithms available to all,
{'blake2s', 'sha384', 'sha3_256', 'sha1', 'shake_128', 'sha3_512', 'sha512', 'sha224', 'sha3_384', 'sha256', 'sha3_224', 'md5', 'shake_256', 'blake2b'}

a. new()

It is a generic constructor that takes the string name of the desired algorithm as its first parameter. It is slower so it should not be preferred.

# if we don't want to use encode(),we can use 'b' in front of string.
new1.update(b"Nobody inspects the spammish repetition")

b. SHA1 hashlib hash functions

It produces a 160-bit hash value (message digest). It is not considered safe and hence should not be used.

import hashlib
# using the sha1 hashing algo
hash_func = hashlib.sha1()
# string that we want to change
string="Python is the best"
# encoding the string to pass into update()
# Passing the string into update()
# Saving the new string using hexdigest()
hashlib python

c. md5 hash

This hash function splits the string into equal parts and then computes a 128 bit hash value of the entire string through chaining.

import hashlib
#using the md5 hash function.
hash_func = hashlib.md5()
string="Python is the best"

d. Now let us look at all hash functions together

import hashlib

#blake2s offers high speed, security, and simplicity.
hash_func1 = hashlib.blake2s()
#sha256 offers 256-bit key and is one of the strongest hash function available. 
hash_func2 = hashlib.sha256()
#sha512 is a 512 bit (64 byte) hash function
hash_func3 = hashlib.sha512()
string="Python is the best"



blake2s: b5eaf8a10673a974856a3faa500ab142a199e377e4f489ec67919166b4801cbc  
sha256: c651ccb2a90e0757f4d151f6d5583ce5fbf5171a0c31002d36a0776550ec95b6 

e. Using base64

Another way to encode binary hashes for non-binary environments.

import hashlib
import base64
encoded_string = base64.b64encode(string.encode('utf8'))

f. How to store the hashed data in a file?

import hashlib

def sha1(fname):
    hash_sha1 = hashlib.sha1()
    with open(fname, "rb") as f:
        # sometimes we cannot save the complete file and that is why we save 
        #the data in in chunks
        for chunk in iter(lambda:, b""):
    return hash_sha1.hexdigest()

# create a file
python hashlib

g. Can we decrypt data generated using hashlib in python?

We have told you many times that hashing is a one way process. We cannot decrypt/decode the data generated by hashing functions in hashlib library, by any legal means. Though people have come out will different illegal means to crack the hashed data generated by functions like md5, sha1, but as that is illegal, we should not focus on decrypting this type of data.

Must Read:


To secure our data (string), we can use the hashlib library in python. There are many hash functions available like sha1, sha2, md5 and more. SHA2 is generally regarded as the strongest algorithm. Also, we cannot estimate the original string once it has been changed.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments