Hash Functions - Examples
In this section we shall provide a few examples about calculating cryptographic hash functions in Python.
Calculating Cryptographic Hash Functions in Python
We shall use the standard Python library hashlib
. The input data for hashing should be given as bytes sequence (bytes object), so we need to encode the input string using some text encoding, e.g. utf8
. The produced output data is also a bytes sequence, which can be printed as hex digits using binascii.hexlify()
as shown below:
Run the above code example: https://repl.it/@nakov/Hashes-SHA2-SHA3-BLAKE2-RIPEMD-in-Python.
The expected output from the above example looks like this:
Calculating Keccak-256
hashes (the hash function used in the Ethereum blockchain) requires non-standard Python functions. In the below example we use the pycryptodome
package available from PyPI: https://pypi.org/project/pycryptodome.
First install "pycryptodome" (https://www.pycryptodome.org)
Now write some Python code to calculate a Keccak-256 hash:
Run the above code example: https://repl.it/@nakov/Keccak256-in-Python.
The output from the above examples is:
Last updated