SHA encoding using Python?


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 sha1 hash function then the result is: 0a4d55a8d778e5022fab701977c5d840bbc486d0,

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.

SHA1

The SHA is a group of algorithms like – SHA1, SHA224, SHA256, SHA384, SHA512. The SHA1 algorithm, considered more secure than md5 and hence is widely used in many areas.

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 a simple program to understand sha1 algorithm,

 Live Demo

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

Output

00d375a3693fde63e9540b91656c6ac5b5341f7c

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

 Live Demo

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

Output

b'\x00\xd3u\xa3i?\xdec\xe9T\x0b\x91elj\xc5\xb54\x1f|'

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.sha1(mystring.encode())
print(hash_obj.hexdigest())

Output

Enter string to hash: Hello, TutorialsPoint
b20d14ead3e50552fc1f1cd96696b111a163dffa

Updated on: 30-Jul-2019

736 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements