create a sha3_256 hash from a string in python

To create a SHA3-256 hash from a string in python, you will need to use the hashlib library along with the SHA3 hash function.

main.py
import hashlib

# string input
input_string = "Hello World!"

# convert string to bytes
input_bytes = input_string.encode()

# create SHA3-256 hash object
hash_object = hashlib.sha3_256()

# update hash object with input bytes
hash_object.update(input_bytes)

# get hexadecimal digest
hash_hexdigest = hash_object.hexdigest()

print(hash_hexdigest)
349 chars
19 lines

Output:

main.py
f5f54f63a3ca3af3c169e3e5c2a72204b9d5c6d787a240e9f7795da7e5f1cdda
65 chars
2 lines

In the code above, we first convert the input string to bytes using the encode() method. We then create a sha3_256 hash object using the hashlib.sha3_256() method, and update it with the input bytes using the update() method. Finally, we obtain the hexadecimal digest of the hash using the hexdigest() method.

gistlibby LogSnag