Hash functions are one of the most important pieces in creating a blockchain. These functions give blockchain its immutability and provide a way to verify data.
A hash function takes input data and returns a “hash” of the data. This hash is known as a digital fingerprint of the input data. There are many hash functions, Bitcoin uses SHA-256.
Hash functions are also one-way functions. Given input x, the hash function will return output y. However, given the output y, it is nearly impossible to know what the input, x, was. If x stays the same, the hash function will always return y. If anything about x changes, y will also change. This will become clearer in the example below.
Using Hash Functions
This site will generate an SHA-256 hash with whatever input the user gives.
Input: The month is October
Output (hash): A3272CB34F4A4721BA14AF6F3FFA1A27E093401C0F459067FAFF7F093CEE9A4C
That output is a unique representation of the input “The Month is October”. Now the input is going to slightly change with an exclamation point added at the end. The corresponding output will change as well.
Input: The month is October!
Just the addition of an exclamation point completely changed the output. If two output hashes are different, that means the two inputs are not the same. This feature of hash functions is incredibly valuable and important for blockchain.
How does this relate to blockchain?
As mentioned above, the hash is a digital fingerprint of some data, just like a real fingerprint, the hash is unique to that set of data. The word data here could be anything, some text, a picture, or in the case of blockchain and cryptocurrency, transactions.
Since public blockchains are distributed, such as Bitcoin or Ethereum, there needs to be a way to verify that someone’s copy of the blockchain is correct and has not been tampered with.
As seen above, any change to the input data will result in a change to the output hash. Distributed blockchains need consensus, nodes (more specifically miners or validators) need to agree on the current state of the blockchain. Hash functions provide a way to verify data on the blockchain.
— — — — —