This time we will speak about a hash function. In order to understand completely what a hash function is, we need to dive a little deeper into this topic.
Cryptocurrency is not encrypted
Often, many people (even specialized magazines) tell that cryptocurrency is somehow connected with cryptography, that all information is encrypted – this is wrong.
In fact, there is no encryption in cryptocurrencies. Encryption operations are used to load CPU and waste electrical power (intentionally!).
In this article, I want to describe how a hash function works and what it is. A hash function is a function that gives absolutely unpredictable outputs at some input, but these outputs should be reproducible. So if you have the same input, you have the same result. If inputs are different by a tiniest bit – result will differ unrecognizably.
Let’s take for example hash function Base58.
Hash of “Hello” is “9Ajdvzr”.
Hash of “Hello!” is “d3yC1LKr”.
If somebody sends me “9Ajdvzr”, I know they meant “Hello”. But only because i did hash “Hello” myself. If I hadn’t, I would have no chance in the world to decrypt the hash result. It is called “non-reversible result”.
This feature of hash-functions is used for storing passwords by Facebook or Google. They do not store your passwords clearly. They store hash-functions of passwords, and when you try to log in, they compare hash-function of what you typed in with a hash-function of your real password.
Hash-function is irreversible. If you obtain hash-function of someone else’s password, you cannot do anything with it to reverse the original password. So even if somebody steals all the databases of Google, they will not have access to your real passwords, which is pretty cool!
Deeper in hashing
Let’s take for example a hash function EHA (Example Hashing Algorithm, just invented by me). Now I will not go into details, let’s just imagine that we have some kind of file. As you know, in digital space any file is essentially a set of bytes. A byte can represent both an image and numbers. In fact, let’s imagine we have the word “CAR”. We split it into into a set of symbols (which are encoded in computer as numbers):
So what we do next? Let’s take three numbers (the first three on each line), and add them.
And for the last two numbers on each line, we glue them together, and also we write three ones in the beginning, middle and the end of this. 10101 in binary is 21 in decimal.
Now we combine the left part (where we have 2) and the right part (where we have 21) by raising the right part to the power of left part. This will be 21^2 = 441 in decimal, or 11011001 in binary. We replace the original number with the new one.
Then we do the same for the second line, and so on
It’s time to ask: why we do all this nonsense?
We spend computational power, and in the end wet get some crazy new number. What is the point? It will not allow us to restore the information back, to restore the original word. Meantime we can compare the result of hash function EHA here and elsewhere. If the results are the same, then the original word was also the same. Finally, if the words are the same, we get some nonsense number here, and another person elsewhere gets the same nonsense number. It works so that if at least one letter was different, the operation of raising to power would give absolutely unpredictable result.
Now imagine the SHA-256 function (used in Bitcoin) where the same thing like adding numbers to each other and raising to power happens 256 times in a row. That is how it happens: at first we did some kind of insane nonsense, then put another insane nonsense on top, and so on and so forth 256 times. Thus, the result of hash function does not allow us to restore the original file. But as I have pointed earlier, if hash is the same here and somewhere else, it is considered that the hashed content was the same, too.
Actually, this is how hash function works. This is a very important knowledge. You know, 99% of people (I consider maybe even 99.99%) do not understand hash functions, and therefore do not understand the essence of Bitcoin. Now you are in a privileged minority. See you in the next chapters!