An Interactive Guide to the Blockchain, Part II – Hash Functions

Written by Hannes Westermann, Researcher at the Cyberjustice Laboratory and PhD Student at Université de Montréal

Yesterday, we explored the problems that a blockchain has to solve in order to create a decentralized currency (cryptocurrency). Today, we will start delving into the technical side of the blockchain, starting with one of the most important concepts – the cryptographic hash function.

Cryptographic hash functions are crucial in the process of creating cryptocurrencies and they are the central element of the so called « cryptomining ». Cryptomining can be compared  to the historic situation when gold was used as currency (or directly linked to paper currencies), and where the creation of additional money happened through gold mining. Instead of using heavy machinery to dig and process gold-rich ground, cryptomining uses powerful computers that create new cryptocoins through the complex and time-consuming calculations of cryptographic hash functions.

Cryptographic Hash Functions

Hash functions play a very important role in the blockchain but are also used in places all over the internet and your computer.

But what exactly is a hash function? Essentially, a hash function attempts to translate a piece of data (such as a sentence, an image or a film) into a short, uniformly sized value. For example, using a particular hash function, the words “The sun shines” might be hashed to the value “h5468456j9”.

But what makes this special? And how can it be used?

To explain these points, let us define a very simple hash function that we can calculate with a piece of paper. For this hash function, take each letter of a message, take the number of their place in their alphabet, and add them together.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

For example, the word “apple” would have the hash 1+16+16+12+5 = 50. Try to calculate the hash for the word “justice”!

This is a very simple but complete hash function. Usually hash functions are calculated by computers. Here below is a widget where you can enter any text and have it hashed to SHA-256, which is a commonly used hash function. (Note that it is usually longer – for visual simplicity I here cut the result to 10 characters.

Enter your text to be hashed: SHA-256 (hash of « {{toHash}} »)
{{hashed}}

You will notice a few things playing with this:

  • The hash is different for each text you enter. If you change one character, the entire hash changes.
  • The length of the text does not matter – the length of the hash is always the same.
  • It is very fast to transform a message into its hash.
  • It is impossible to go backwards from the hash to the original text. If I told you I wanted you to find a word where the hash starts with 5 zeros, how would you do it? The only way to achieve this for cryptographic hash functions is to try all possible words, until you by chance stumble on one where the hash starts with 5 zeros.

Proof-of-work

For our purposes, the most relevant use-case of a hash is « proof-of-work ». This is a technique that can be used to demonstrate to someone else that you have performed certain calculations.

Say, for example, that Bob wants to make sure that Alice has spent 1 second working on something. One way to do this is to ask her to find a number, so that the sentence “I am done”, together with some number, results in a hash starting with 3 zeros.

Since it is impossible to go backwards from a hash, Alice will have to try a lot of numbers until she arrives at a hash with 3 zeros. First, she will calculate the hash for « I am done1 », then « I am done2 », etc. In this case, the first number that matches is 7128 (« I am done7128 »), meaning she will have to calculate over 7000 hashes! However, Bob just has to run the hash function one time to verify that she has carried out 7128 calculations, finding that the resulting hash starts with 3 zeros.

Try to find a number that together with the word “blockchain” starts with a zero below (or cheat by pressing the button for the computer to find a number where the hash starts with 3 zeros).

text number (added to hash) hash (of « {{toHash}} »)
{{term}} {{hashed}}

Sometimes, it can be very useful to require someone to show that they have done some calculations. For example, it can be used to combat spam emails! If everyone who sent an email had to make sure the hash of their email and some number starts with 5 zeros, sending mass email would be much more expensive. However, legitimate emails are rarely sent to tens of thousands of users – therefore legitimate users would not be heavily impacted. This idea has been implemented in hashcash.

Proof-of-work is also a very important part of the blockchain. We will discuss how it is used in the following posts.

Conclusion

In this blog post, we learnt about hashing. Cryptographic hash functions are functions which take some data and generate a fixed-length representation of that data. While it is very quick to hash something, it is very slow to “unhash” something, by going from the hash to the input data. This makes hashes very useful for a number of tasks. For example, it can be used as a kind of puzzle, which shows that you have done a number of calculations. This mechanism is one of the key mechanisms of the blockchain and is the kind of functions that are used for cryptomining.

In the next post, we will see how hashes can be used to create chains of blocks of data (the blockchain!) which can guarantee that none of the data in previous blocks has been changed. This is the technology at the core of blockchain technologies, including bitcoin.

Read more

« Cryptographic Hash Functions Definition », (Investopedia, 2020)

Ce contenu a été mis à jour le 9 juin 2020 à 13 h 34 min.