Block hashes are used to uniquely identify and maintain the integrity of each block. The SHA-256 algorithm is used to generate the hash of the block using the timestamp, data, and previous hash — the three properties of our Block class!

Let’s create the .generate_hash() method for the Block class.



In the .generate_hash() method, create the variable block_contents, which combines the Block timestamp, transactions, nonce, and previous hash.

Wrap each item in the str() method in order to convert them to a string type to prepare for hashing.

Return the result.


Create a variable block_hash.

Create a new hash with sha256() and block_contents and save the value to block_hash.

Remember to use the .encode() method to encode the string.

Update the method to return block_hash.


Return the text hash value by calling the hexdigest() method on block_hash.


Uncomment the line in __init__() that calls the generate_hash() function to complete the Block class.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?