Secure Hash Algorithm (SHA) in Python

In this tutorial, we will learn about Secure Hash Algorithms (SHA) in Python. First, let’s check out the basics.

The hash function: Hash function is used in cryptography to secure a message by encoding it. It takes input of any length and maps it into a fixed size. Every message should have a unique hash value. A small change in the message should extensively change the hash value. Further, it goes without saying that the same message should always result in the same hash value.

Hash algorithms: There are many cryptographic algorithms available in python. To access it, python has a predefined library known as hashlib. To check the algorithms supported by your current interpreter you can use:


Also, you can use:


This returns all the algorithms supported on any platform. Implementing this in the code gives us,

import hashlib


{'sha512', 'DSA-SHA', 'SHA224', 'sha', 'ecdsa-with-SHA1', 'DSA', 'sha1', 'sha224', 'md5', 'MD5', 'SHA', 'SHA384', 'SHA1', 'whirlpool', 'SHA256', 'RIPEMD160', 'MD4', 'dsaWithSHA', 'sha256', 'dsaEncryption', 'SHA512', 'sha384', 'ripemd160', 'md4'}         
{'sha512', 'sha1', 'sha256', 'sha224', 'md5', 'sha384'}

Now that we know about hash functions and algorithms, let us look further into the SHA family.

This tutorial might help you: How to encode a string in MD5 using Python

SHA in Python

Started off in 1993, SHA was revised through a number of versions. SHA-0 was withdrawn a long time ago due to detection of many loopholes. Similarly, SHA-1 is also not recommended and hence is not in use anymore. Next, SHA-2 was introduced. Hash functions in SHA-2 are- SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256. Finally, SHA-3 was written and is currently the latest version of SHA.

SHA-1160 bits
SHA-256, SHA-384, SHA-22432 bits
SHA-51264 bits

The above table shows different SHA versions and their block sizes. Keeping these in mind, let us execute SHA in python. For doing so, we need haslib library and hexdigest() method.

Haslib library: This module provides access to many hash algorithms like md5, SHA-1, etc. Hashlib helps in generating message digest of the original message. (Message digest is a cryptographic hash value of the message resulted using hash algorithms). To use this, simply import it using:

import hashlib

hexdigest() function: This method returns the hashed message in hexadecimal format. Syntax:


Also, we need to encode the message to binary before hashing. We can do that by simply using b’string’ or using the encode() function. If we don’t, we might encounter an error like,

Unicode-objects must be encoded before hashing

How to implement Secure Hash Algorithm (SHA) in Python

Let us now check out the python program to implement different SHA algorithms available.

#import the module
import hashlib

#encode the message
text = hashlib.sha1(b'hello')

#convert it to hexadecimal format
encrypt = text.hexdigest()

#print it



The above code uses SHA-512. Similarly, we can try out different versions of SHA:

import hashlib

text1 = hashlib.sha1(b'hello')
encrypt1 = text1.hexdigest()

text2 = hashlib.sha256(b'hello')
encrypt2 = text2.hexdigest()

text3 = hashlib.sha384(b'hello')
encrypt3 = text3.hexdigest()

text4 = hashlib.sha224(b'hello')
encrypt4 = text4.hexdigest()





2 responses to “Secure Hash Algorithm (SHA) in Python”

  1. sirbowen says:

    Very interesting topic on cryptography with python, thank you for the explanation and example presented in your website.

  2. Michael says:

    wow! this is made me understand the algorithm. I will like to learn more

Leave a Reply

Your email address will not be published. Required fields are marked *