create a sha3_512 hash from a string in python

To create a SHA3-512 hash from a string in python, you can use the hashlib library from the standard library or the SHA3_512 hash function from the cryptography library. Here are examples using both libraries:

hashlib

main.py
import hashlib

string_to_hash = "Hello, World!"

hash_object = hashlib.sha3_512(string_to_hash.encode())
hex_dig = hash_object.hexdigest()

print(hex_dig)
156 chars
9 lines

Output:

main.py
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890ea853c5566f0c6b0b8cea31c9e26883b6de5782f4a3e0acbaed' 
97 chars
2 lines

cryptography

main.py
from cryptography.hazmat.primitives import hashes

string_to_hash = "Hello, World!"

digest = hashes.Hash(hashes.SHA3_512())
digest.update(string_to_hash.encode())
hex_dig = digest.finalize().hex()

print(hex_dig)
214 chars
10 lines

Output:

main.py
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890ea853c5566f0c6b0b8cea31c9e26883b6de5782f4a3e0acbaed
95 chars
2 lines

Both examples produce the same SHA3-512 hash for the given input string "Hello, World!". However, the cryptography library provides more flexibility and options for hashing.

gistlibby LogSnag