Hashing Functions

This lesson focuses on one of the building blocks of the blockchain, the hashing function. In short, a hashing function is a one-way function that takes an input and provides a hash. They provide the mechanism for the blockchain to be secured, enabling Bitcoin to work without issues like double-spending coins and centralised identity.

You might have seen a hash before, enter a word below to create a hash!

Enter a word:

SHA256 hash string:
Your Hash will go here

This hash was computed by inputting a phrase you chose, then it's run through the SHA256 algorithm to then be displayed as your hash.

Let's now take a step back and review those concepts in a bit more detail.

A function is an algorithm that takes an input and returns an output. Here is a function called add_three that takes an input called x, adds 3 to it and returns an output y.

def add_three(x):
    y = x + 3
    return y

The code is written in a programming language called Python, but there is no need to go into detail on what it does.

With this function, we input a number and receive an output. For example, calling add_three(6) puts the input 6 into the function, and that function returns 9.

This type of function has an inverse - subtract_three, which works like this:

def subtract_three(y):
    x = y - 3
    return x

This function "undoes" the first function. In otherwords, whatever input we put into add_three, we get the output, put that into the substract_three function and get the initial input again.

y = add_three(6)

subtract_three(y)   # Returns 6

A hash function is an example of a one-way function, in other words, there is no inverse function. What this means, is that I can give you a hash, and you cannot work out what the original input was. (As a sidenote, this isn't technically true, but it is "practically" true.)

Another important feature of hashing functions is that they are deterministic. In otherwords, if you put the same input into the function, you get the same output - everytime.

This allows us to verify information, without seeing the information itself. As an example, if I have a website that people log into, I do not store passwords in my database. Instead, I store the hash of the passwords. Then, when a user tries to log in, I hash the password and compare that against the hash in my database. If the hashes match, then the password was entered correctly. If they do not match, the user entered the wrong password.

This has the added benefit of securing the data against hackers - if someone takes a copy of the database, they only have hashes and cannot log into a users account without the original password. It is important to note that this type of security is not solved by simply using hashing functions. If you are looking to use hashes for password security, please follow best practices for your system and consult documentation for assistance. Here is some general (but technical) information on how to do password hashing.

So a hash function combines the above concepts. It is a one-way function that takes some input, and returns a hash as the result. There are many types of hash functions, here are just a few, and the results of hashing the string MySecurePassword123:

  • MD5: CFB7D1B633B8B43E07A61A2164E9A822
  • SHA1:E46BDBA4607B7EE7CB6D1EFF9105BA17118E4A3C
  • SHA256: CCF9AC1C9CE02B9BB7810A1FFF51E474F37D98C3582F2D3B5036CAF559AFD9BC
  • CRC32: 26CF3956

You can find more algorithms at this Hash Generator site.

That said, if you don't know which algorithm to choose, use SHA256 (except for password hashing, see the above link). This is the algorithm that Bitcoin uses (although it applies it twice) to compute the hashes that form the basis for the blockchain. You could use any hashing algorithm to create a blockchain, as long as it's cryptographically secure. That means the algorithm has certain properties and has been mathematically proven.

If you would like to see the SHA256 hash algorithm in fine detail, check out this video of a person computing the algorithm by hand!

In future lessons we will look at how hashing functions are used within the blockchain. In short, we compute the hash of a set of transactions (people sending and receiving money), and a block is considered solved and ready to go live if it has certain properties (namely being the first few characters being zeros). The way this is combined gives blockchains a provably secure method for ensuring transactions cannot be reversed once they are confirmed.

Sign up to be notified when new lessons arrive!

* indicates required