Open In App

SHA3 in Python

A cryptographic hash function is an exceptional class of hash function that has certain properties that make it appropriate for use in cryptography. It is a numerical algorithm that maps information of self-assertive size to a piece line of a fixed size (a hash function) which is intended to likewise be a one-way output function, that is, a function which is infeasible to revert.

hashlib module

To calculate the cryptographic hash value in Python, “hashlib” Module is used. The hashlib gives the following cryptographic hash functions to discover the hash output of a text as follows:



This module actualizes a typical interface to various secure hash and message digest calculations. Included are the FIPS secure hash calculations SHA1, SHA224, SHA256, SHA384, and SHA512 just as RSA’s MD5 calculation (characterized in Internet RFC 1321). Earlier calculations were called message digests, but today it is secure hash.

Python has a bountiful help for hash code calculations through the library module hashlib. You can utilize the “hashlib.algorithms_available” to get the rundown of all accessible hash calculations in your variant of Python.



Hashlib provides the following constant attributes:




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

Output :

{‘blake2b’, ‘shake_256’, ‘sha512’, ‘sha3_224’, ‘sha384’, ‘sha3_512’, ‘sha3_256’, ‘sha3_384’, ‘md5’, ‘sha256’, ‘sha224’, ‘sha1’, ‘blake2s’, ‘shake_128’}
 

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

Constant attributes

Methods in hashlib

SHAKE variable-length digests

SHA – a brief

The Secure Hash Algorithms are a group of cryptographic hash functions proposed by the National Institute of Standards and Technology (NIST) and include:

SHA-3 implementation 

Secure Hash Algorithm-3 additionally called Keccak, is a unidirectional method for creating computerized prints of the given length according to the standards as 224, 256, 384, or 512 pieces from input information of any size, created by a gathering of creators drove by Yoan Dimen in 2008 and embraced in 2015 as the new FIPS standard. The calculation works by methods for the blending capacity in with compression to the choose size “cryptographic sponge”.

Examples:

Input: HelloWorld

Output[sha3_224]: c4797897c58a0640df9c4e9a8f30570364d9ed8450c78ed155278ac0

Input: HelloWorld
Output[sha3_256]: 92dad9443e4dd6d70a7f11872101ebff87e21798e4fbb26fa4bf590eb440e71b
Input: HelloWorld

Output[sha3_384]: dc6104dc2caff3ce2ccecbc927463fc3241c8531901449f1b1f4787394c9b3aa55a9e201d0bb0b1b7d7f8892bc127216

Input: HelloWorld

Output[sha3_512]: 938315ec7b0e0bcac648ae6f732f67e00f9c6caa3991627953434a0769b0bbb15474a429177013ed8a7e48990887d1e19533687ed2183fd2b6054c2e8828ca1c
 

The following programs show the implementation of SHA-3 hash in Python-3.8 using different methods: Implementation of sha3_224 using the update method




import sys
import hashlib
 
if sys.version_info < (3, 6):
    import sha3
 
# initiating the "s" object to use the
# sha3_224 algorithm from the hashlib module.
s = hashlib.sha3_224()
 
# will output the name of the hashing algorithm currently in use.
print(s.name)
 
# will output the Digest-Size of the hashing algorithm being used.
print(s.digest_size)
 
# providing the input to the hashing algorithm.
s.update(b"GeeksforGeeks")
 
print(s.hexdigest())

Output
sha3_224
28
11c044e8080ed87b3cf0643bc5880a38ae62dd4562390700000b1191

Implementation of sha3_256 using encode method




# import the library module
import sys
import hashlib
 
if sys.version_info < (3, 6):
    import sha3
 
# initialize a string
str = "GeeksforGeeks"
 
# encode the string
encoded_str = str.encode()
 
# create sha3-256 hash objects
obj_sha3_256 = hashlib.sha3_256(encoded_str)
 
# print in hexadecimal
print("\nSHA3-256 Hash: ", obj_sha3_256.hexdigest())

Output:

SHA3-256 Hash:  b05a48e99c60983b96b5a69efad8bb44e586702d484d43e592ab639ef64641ff

Implementation of sha3_384




# import the library module
import sys
import hashlib
 
if sys.version_info < (3, 6):
    import sha3
 
# initialize a string
str = "GeeksforGeeks"
 
# encode the string
encoded_str = str.encode()
 
# create sha3-384 hash objects
obj_sha3_384 = hashlib.sha3_384(encoded_str)
 
# print in hexadecimal
print("\nSHA3-384 Hash: ", obj_sha3_384.hexdigest())

Output:

SHA3-384 Hash:  b92ecaaafd00472daa6d619b68010b5f66da7c090e32bd4f5a6b60899e8de3e2c859792ec07a33775cfca8d05c64f222

Implementation of sha3_512 using the new method




import sys
import hashlib
 
if sys.version_info < (3, 6):
    import sha3
 
str = "GeeksforGeeks"
 
# create a sha3 hash object
hash_sha3_512 = hashlib.new("sha3_512", str.encode())
 
print("\nSHA3-512 Hash: ", hash_sha3_512.hexdigest())

Output:

SHA3-512 Hash:  3706a96a8fa96b3fc5ff30cbca36ce666042e2d07762022a78a2ec82439848fc3695e83336ab71f47dddbc24b96454df2a437e343801a4e13faab89e8d0fda61

Applications of Hashing-Algorithms:


Article Tags :