create a blake2b hash from a string in python

To create a blake2b hash from a string in python, you can use the blake2b module from the cryptography library.

Here's an example code snippet:

main.py
from cryptography.hazmat.primitives import hashes

message = "Hello, world!" # the string to be hashed

digest = hashes.Hash(hashes.BLAKE2b(64), backend=default_backend())
digest.update(message.encode('utf-8'))
hash_value = digest.finalize()

print(hash_value.hex())
267 chars
10 lines

In this code, we first create a hashes.Hash object, specifying the hashes.BLAKE2b algorithm with a 64-byte output size. We then update the hash object with our message string (encoded as UTF-8), and finalize it to get the hash value. Finally, we print out the hash value as a hex string.

Note that for security purposes, it's generally recommended to use a secure salt and key to hash your message string.

gistlibby LogSnag