SHA3 hash code generation in Python

Earlier we have seen a briefing about hash codes in Python and about hash codes using SHA-2 algorithm. Now we will see how to generate hash using SHA3 hash code (SHA3-224, SHA3-256, SHA3-384, SHA3-512) algorithms in Python. To start with, let us see a simple example with detailed steps.

Example

# import the library module
import hashlib
  
# initialize a string
str = "www.MyTecBits.com"
  
# encode the string
encoded_str = str.encode()
  
# create sha3 hash objects initialized with the encoded string
obj_sha3_224 = hashlib.sha3_224(encoded_str)   # SHA3-224
obj_sha3_256 = hashlib.sha3_256(encoded_str)   # SHA3-256
obj_sha3_384 = hashlib.sha3_384(encoded_str)   # SHA3-384
obj_sha3_512 = hashlib.sha3_512(encoded_str)   # SHA3-512
   
# print in hexadecimal
print("\nSHA3-224 Hash: ", obj_sha3_224.hexdigest())
print("\nSHA3-256 Hash: ", obj_sha3_256.hexdigest())
print("\nSHA3-384 Hash: ", obj_sha3_384.hexdigest())
print("\nSHA3-512 Hash: ", obj_sha3_512.hexdigest())

Output:

SHA3-224 Hash:  edb22043819ac5aee3f769be84afcf046896417ba59cbd27e865d792

SHA3-256 Hash:  3f26789add7119ffe5d2958301134f80341a67a29e1d071f6a058ce7d788f3aa

SHA3-384 Hash:  ae9dd94c1979688d94e8bb30e347a3a69c49fd032a72e881c034a1b86a4b8c92906a290a5f0448c1cbb7bd5549d328eb

SHA3-512 Hash:  622c7c9cd701c015dd08d8566007e576a246011a9a8cd84ce1056691e45663244140d5d7af049c09817bccc2fe7bfd087241666f95eea35b35e8fc788eb6fa2c

Compact version of the above example

>>> import hashlib
>>> hashlib.sha224("www.MyTecBits.com".encode()).hexdigest()
'0fc6f02eb497804a9e03c54fafff824175de0fc6a26527b32fb2b69c'
>>> hashlib.sha256("www.MyTecBits.com".encode()).hexdigest()
'57e73ea5a340a53a0d42596342c1694a515784a06ac7cfc3ba70b98f767a42ea'
>>> hashlib.sha384("www.MyTecBits.com".encode()).hexdigest()
'06785d0eefc3a035362a0688979770366b45020c1cd4b9e8c14a06c5be4cec59face5c4c0a09ba82d5ee91c83d81f0bb'
>>> hashlib.sha512("www.MyTecBits.com".encode()).hexdigest()
'c5a4dea1f088e87cbba30b4877376e4a741cb00186f75b6d5db841cb7bff0a3a7d85952098d98e604573d514726029e9141194441ea00c87746d4ce8d10d05ef'

More options in Python SHA3

Now let us see the other commonly used options available in Python’s sha3 hashing algorithms.

Hash code in byte

As you have seen in the above examples returned the sha3 hash codes as a hexadecimal value using the hexdigest() method. If you need to get the resultant sha3 hash code in byte value, then use the digest() method. Here is an example.

>>> import hashlib
>>> hashlib.sha224("www.MyTecBits.com".encode()).digest()
b"\x0f\xc6\xf0.\xb4\x97\x80J\x9e\x03\xc5O\xaf\xff\x82Au\xde\x0f\xc6\xa2e'\xb3/\xb2\xb6\x9c"
>>> hashlib.sha256("www.MyTecBits.com".encode()).digest()
b'W\xe7>\xa5\xa3@\xa5:\rBYcB\xc1iJQW\x84\xa0j\xc7\xcf\xc3\xbap\xb9\x8fvzB\xea'
>>> hashlib.sha384("www.MyTecBits.com".encode()).digest()
b'\x06x]\x0e\xef\xc3\xa056*\x06\x88\x97\x97p6kE\x02\x0c\x1c\xd4\xb9\xe8\xc1J\x06\xc5\xbeL\xecY\xfa\xce\\L\n\t\xba\x82\xd5\xee\x91\xc8=\x81\xf0\xbb'
>>> hashlib.sha512("www.MyTecBits.com".encode()).digest()
b'\xc5\xa4\xde\xa1\xf0\x88\xe8|\xbb\xa3\x0bHw7nJt\x1c\xb0\x01\x86\xf7[m]\xb8A\xcb{\xff\n:}\x85\x95 \x98\xd9\x8e`Es\xd5\x14r`)\xe9\x14\x11\x94D\x1e\xa0\x0c\x87tmL\xe8\xd1\r\x05\xef'

Using update()

In the earlier examples we have created the hash object initialized with the encoded string or byte string. There is another way to append the byte string to the sha1 hash object using update() method. You can use the update() multiple times to append the byte string or any other byte date. This method comes in handy when you want to append data to the hash object based on multiple conditions. Here is an example for SHA3-224. You can use the same example for other SHA3 algorithms SHA3-256, SHA3-384 and SHA3-512.

import hashlib
   
# create a sha3-224 hash object
hash_object = hashlib.sha3_224()
  
# append the byte string
hash_object.update(b"www.")
hash_object.update(b"MyTecBits")
hash_object.update(b".com")
  
print("\n", hash_object.hexdigest(), "\n")

Output:

edb22043819ac5aee3f769be84afcf046896417ba59cbd27e865d792

Using new()

In the earlier examples we have created the hash abject using the sha3_224() or sha3_256() or sha3_384() or sha3_512() constructor methods. There is another way to initialize with one of the sha3 hash object. It is by using the new() method. In the new() method, you have to specify the name of the algorithm you want to use as its first parameter. In addition, you can also add the encoded string as an optional second parameter. Here is an example

import hashlib
 
str = "www.MyTecBits.com"
 
# create a sha3 hash objects
hash_sha3_224 = hashlib.new("sha3_224", str.encode())
hash_sha3_256 = hashlib.new("sha3_256", str.encode())
hash_sha3_384 = hashlib.new("sha3_384", str.encode())
hash_sha3_512 = hashlib.new("sha3_512", str.encode())
  
print("\nSHA3-224 Hash: ", hash_sha3_224.hexdigest())
print("\nSHA3-256 Hash: ", hash_sha3_256.hexdigest())
print("\nSHA3-384 Hash: ", hash_sha3_384.hexdigest())
print("\nSHA3-512 Hash: ", hash_sha3_512.hexdigest())

Output:

SHA3-224 Hash:  edb22043819ac5aee3f769be84afcf046896417ba59cbd27e865d792

SHA3-256 Hash:  3f26789add7119ffe5d2958301134f80341a67a29e1d071f6a058ce7d788f3aa

SHA3-384 Hash:  ae9dd94c1979688d94e8bb30e347a3a69c49fd032a72e881c034a1b86a4b8c92906a290a5f0448c1cbb7bd5549d328eb

SHA3-512 Hash:  622c7c9cd701c015dd08d8566007e576a246011a9a8cd84ce1056691e45663244140d5d7af049c09817bccc2fe7bfd087241666f95eea35b35e8fc788eb6fa2c

Reference

Related Articles & Tools

Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.