SHA-2 hash code generation in Python

Earlier we have seen a briefing about hash codes in Python and about hash codes using SHA1 algorithm. Now we will see how to generate hash using SHA-2 hash code (SHA224, SHA256, SHA384, SHA512) algorithms in Python. To start with, let us see a simple example with detailed steps.

# import the library module
import hashlib
 
# initialize a string
str = "www.MyTecBits.com"
 
# encode the string
encoded_str = str.encode()
 
# create sha-2 hash objects initialized with the encoded string
hash_obj_sha224 = hashlib.sha224(encoded_str)   # SHA224
hash_obj_sha256 = hashlib.sha256(encoded_str)   # SHA256
hash_obj_sha384 = hashlib.sha384(encoded_str)   # SHA384
hash_obj_sha512 = hashlib.sha512(encoded_str)   # SHA512
  
# print
print("\nSHA224 Hash: ", hash_obj_sha224.hexdigest())
print("\nSHA256 Hash: ", hash_obj_sha256.hexdigest())
print("\nSHA384 Hash: ", hash_obj_sha384.hexdigest())
print("\nSHA512 Hash: ", hash_obj_sha512.hexdigest())

Output:

SHA224 Hash:  0fc6f02eb497804a9e03c54fafff824175de0fc6a26527b32fb2b69c

SHA256 Hash:  57e73ea5a340a53a0d42596342c1694a515784a06ac7cfc3ba70b98f767a42ea

SHA384 Hash:  06785d0eefc3a035362a0688979770366b45020c1cd4b9e8c14a06c5be4cec59face5c4c0a09ba82d5ee91c83d81f0bb

SHA512 Hash:  c5a4dea1f088e87cbba30b4877376e4a741cb00186f75b6d5db841cb7bff0a3a7d85952098d98e604573d514726029e9141194441ea00c87746d4ce8d10d05ef 

Compact version of the above example

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

More options in Python SHA-2

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

Hash code in byte

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

>>> import hashlib
>>> hashlib.sha224(b"www.MyTecBits.com").digest()
b"\x0f\xc6\xf0.\xb4\x97\x80J\x9e\x03\xc5O\xaf\xff\x82Au\xde\x0f\xc6\xa2e'\xb3/\xb2\xb6\x9c"
>>> hashlib.sha256(b"www.MyTecBits.com").digest()
b'W\xe7>\xa5\xa3@\xa5:\rBYcB\xc1iJQW\x84\xa0j\xc7\xcf\xc3\xbap\xb9\x8fvzB\xea'
>>> hashlib.sha384(b"www.MyTecBits.com").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(b"www.MyTecBits.com").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 SHA224. You can use the same example for other sha2 algorithms SHA256, SHA384 and SHA512.

import hashlib
  
# create a sha224 hash object
hash_object = hashlib.sha224()
 
# 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:

0fc6f02eb497804a9e03c54fafff824175de0fc6a26527b32fb2b69c

Using new()

In the earlier examples we have created the hash abject using the sha224() or sha256() or sha384() or sha512() constructor method. There is another way to initialize with one of the sha-2 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 sha-2 hash objects
hash_sha224 = hashlib.new("sha224", str.encode())
hash_sha256 = hashlib.new("sha256", str.encode())
hash_sha384 = hashlib.new("sha384", str.encode())
hash_sha512 = hashlib.new("sha512", str.encode())
 
print("\nSHA224 Hash: ", hash_sha224.hexdigest())
print("\nSHA256 Hash: ", hash_sha256.hexdigest())
print("\nSHA384 Hash: ", hash_sha384.hexdigest())
print("\nSHA512 Hash: ", hash_sha512.hexdigest())

Output:

SHA224 Hash:  0fc6f02eb497804a9e03c54fafff824175de0fc6a26527b32fb2b69c

SHA256 Hash:  57e73ea5a340a53a0d42596342c1694a515784a06ac7cfc3ba70b98f767a42ea

SHA384 Hash:  06785d0eefc3a035362a0688979770366b45020c1cd4b9e8c14a06c5be4cec59face5c4c0a09ba82d5ee91c83d81f0bb

SHA512 Hash:  c5a4dea1f088e87cbba30b4877376e4a741cb00186f75b6d5db841cb7bff0a3a7d85952098d98e604573d514726029e9141194441ea00c87746d4ce8d10d05ef

Reference


Leave your thoughts...

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