You might be wondering… What makes Bitcoin and its blockchain so secure? How can something intangible be the most secure form of money? Well, it's all thanks to hashing. Hashing is a form of cryptography that is widely used in the Bitcoin network. You can review cryptography in the previous modules.
A hash function is a special function where any length of data inputted will be computed, then the output would have a fixed length. It's similar to how zip codes work. Let's take the case below for example:
Balintawak, Quezon City - 1106
Poblacion, Makati - 1210
Zapote, Las Piñas - 1742
Even though these areas have different characteristics such as area size, population, location, climate, or ratio of males to females, all these areas have an equivalent code of four numbers long. That's how a hash function works — it turns any type of data into data with a fixed length.
You can think of a hash function like a tunnel where all the vehicles (input) entering are processed and computed. Once these vehicles come out of the tunnel, they turn into a specific car model (output), but with different paint jobs, windows, doors, or decals.
Now, let's apply it to the real thing. Found below are six texts processed through the hash function SHA 256, which is what Bitcoin uses. You can also try it out yourself here.
Scroll left to view full table
No matter how big or small of a change we did to the input, the output changed drastically while still maintaining its data length. The SHA-256 hash function also has several notable properties.
1. Deterministic: You always get the same output for a certain input.
2. Unpredictable: Changing any one thing in the input would completely change its output.
3. Collision-resistant: It’s highly unlikely to find two inputs with the same exact output.
4. Efficient: The process of hashing is quick.
5. Verifiable: It’s easy to verify a hash. All you need is the input and the hash function.
6. Irreversible: No one can find out what the input is by decrypting the output. It is a one-way street.
7. Consistent: The output is always going to be in a fixed size.
Why is it nearly impossible to find two inputs with the same exact output you might ask? Can’t you just guess what the hash output will be?
These hashes are almost impossible to predict simply due to the number of possibilities there are with SHA 256. To put things in perspective, there are 115 , 792, 089 , 237 , 316 , 195 , 423 , 570 , 985 , 008 , 687 , 907 , 853 , 269 , 984 , 665 , 640 , 564 , 039 , 457 , 584 , 007 , 913 , 129 , 639 , 936 or 2^256 possible outcomes.
That's a huge number, right? Fun fact: The number of possible outcomes with the hash function SHA-256 (2^256) is close to the estimated number of atoms in the universe... Yep! Dealing with hashes is no joke.
Now, it’s time to understand how hashing works with Bitcoin. In the next chapter, we'll focus on how hashing works in mining and plays a role in the Bitcoin blockchain.