WTF is Hashing in Blockchains?
@ShivaShiva Sai Kumar B
Blockchain Developer, Librocubicularist, Content writer.
In this article, we will see,
● What is Hashing?
● How Hashing is used in Blockchain?
● What is a Merkle Tree?
● How we can secure the data with Hashing?
1. What is Hashing?
Hashing is the process of taking the input string of any length and turning it into cryptographic fixed output. Hashing is not an “encryption” we cant retrieve the original data by decrypting the hash, it’s a one-way cryptographic function.
Do you know we can keep the whole data which is present on the internet in the fixed string length with the help of Hashing Algorithm. We use a mathematical algorithm called SHA-256 (Secure Hashing Algorithm -256 bits). SHA 256 is the successor of the SHA-1 which is of 160 bits.
2. How Hashing is used in Blockchain?
, every block has a hash of the previous block, the previous block is called as parent block for the present block and now consider a parent block has a present block and it will have a hash of previous block i.e parent block.
In the blockchain, every block has a hash of the previous block. When we change any data in the present block the hash of the block will be changed, this will affect the previous block because it has the address of the previous block.
For example, If we have only two blocks, one will be present block and one will be the parent block. The present block will be having the address of the parent block. If we need to change the data in the present block, we also need to change the parent block. It will be easy to change the data when there are only two blocks, but now when we come into reality in blockchain, there are 614272 blocks have been mined by
2020-01-24 12:32, and hash of 614272(th) block is
. If we want to change data in present block i.e 614272(th) block, the hash address of 614271 blocks have to be changed, but it is not possible to change the hashes of 614271 blocks, so this is how blockchain is called immutable and trustworthy of the data. The first block of a blockchain, known as a Genesis block
. You can see how many blocks are mined until now at Blockchain
I have created a Visualization of this process.
If we do a small change to any part of the input there will be a huge change to the output, see the examples below for more understanding.
Hashing is of the core fundamentals and foremost aspects of the immutable and defining potential of blockchain technology. It preserves the authenticity of the data that is recorded and viewed, and as such, the integrity of a blockchain as a whole.
It is one of the more technical aspects of the technology, however, understanding it is a solid step in understanding how blockchain functions and the immeasurable potential and value that it has.
3. How do Merkle trees work?
When there is a large body of data it will be hard to verify it and takes a lot of memory to store and secure it but with the help of Merkle tree, we can easily overcome all these problems.
Merkle tree is a fundamental part of the Blockchain Technology, it is in a structure where we can easily find out any change happens to a large amount of data and verifications of the data can be done efficiently. These are used by both Bitcoin and Ethereum.
As we can see in the above image, all the transactions are at the bottom and the top single hash is called Root hash or Merkle root.
Let us consider one example, there are 4 transactions with A, B, C, D. Now A and B hash will combine to form one hash and C and D another hash, AB hash and CD hash now combine to form one single hash called Root hash or Merkle Root ABCD.
The Root hash will have all the information of all the transactions. Merkle tree will repeatedly hash, pair of nodes till there is only one hash left called Root Hash.
Merkle tree is a binary tree, so need to have even number of leaf nodes, if the number of transactions is odd, the last hash will be duplicated once to create an even number of leaf nodes...
In the above image we can see the duplicate transaction hashing when their is an odd number of transactions, this is how merkle tree will duplicate for odd number of leafs.
All the data of the Transactions are summarised into single Root hash and that is stored in the block header, As we now any change in the data the whole hash function we will changes, if hash changes, so do Merkle root changes. Merkle tree helps us to maintain the integrity of the data.
Another advantage of Merkle tree is if you want to know the status of one particular transaction, we don’t need to download the entire blockchain, we just need to ask for vertical proof and ask for a certain branch of a tree and verify one particular transaction branch.
4. How can we Secure Data with Hashing?
Hashing drastically increases the security of the data. There is no way to decrypting the data because we are not encrypting it. As I mentioned already it’s a one-way cryptographic function. A cryptographic hash function needs to have several crucial qualities to be considered useful, these include:
1. Every hash is different from another.
2. Same Hash value will be always produced for the same message.
3. Impossible to decide input based on the hash value.
4. Even a small change to the input whole hash will be changed.
Hashing helps us to see if the data has tampered or not.
For example, you have downloaded a piece of important information, to see if the data is changed or not, you can run the data through the hashing algorithm and compare the hash of the data and hash of the received data.
If both the hashes are same the data is not changed and if the hash doesn’t match, the data is altered before you received it.
(Originally published here
Subscribe to get your daily round-up of top tech stories!