Exercise: Hash Something

Let's see hashing in action. In this exercise, we'll try out hashing data of varying lengths, which will allow us to verify that the outputs are deterministic and uniform in length.

In this exercise, we'll learn how to hash data in Python using the SHA256 hashing algorithm. We'll then verify that the hash function produces unique outputs or digests, is deterministic(the same input will always produce the same output), and that the hash digest will always be of a uniform length despite variations in the input length.

Python comes with a built-in hash function, hash(). However, this function uses the SipHash algorithm, https://www.python.org/dev/peps/pep-0456/, and we want to use SHA256 as that is what Bitcoin uses. So we'll need to import the hashlib library, https://docs.python.org/3/library/hashlib.html, to gain that functionality.

Let's get started by importing the hashlib library.

import hashlib

 

Create a string to hash and print it out.

mystring = 'Python is fun!'
print('Your string is:', mystring)

 

And then hash it.

myhash = hashlib.sha256(mystring.encode())

 

What we did here is first encode our string and then pass it along to the sha256 hashing function from the hashlib library.

Now let's print out the results, in hexadecimal, to see what the output, or digest is.

print('Your SHA256 hash is:', myhash.hexdigest())

 

Now that we can create a hash digest, let's validate for ourselves that SHA256 will create a unique hash digest for our input. Let's change the value for mystring by just one character. For example, let's now hash 'Python is fun.' We should receive a completely different digest from the first.

We can also change the value of mystring back to the original value to validate that SHA256 is deterministic, meaning that a specific input will return the same digest every time.

And let's run one more test. Let's be sure that SHA256 will always give us a digest of the same length. Add the below line to your code to see what the current length of your hash digest is.

print('The length of your hash is:', len(myhash.hexdigest()))

 

Now change the value of mystring such that it is either much longer or much shorter than the original value and run the script again to check the length of the digest. It should be 64 characters every time.


Source: Saylor Academy
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Tuesday, October 5, 2021, 12:59 PM