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:

hashlib.algorithms_available

Also, you can use:




hashlib.algorithms_guaranteed

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

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

Output:

{'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.

Version Size
SHA-1 160 bits
SHA-256, SHA-384, SHA-224 32 bits
SHA-512 64 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:

string.hexdigest()

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
print(encrypt)

OUTPUT:

9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

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()
print(encrypt1)

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

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

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

OUTPUT:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f
ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193

 

 


Leave a Reply

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