DEV Community

Cover image for How Blockchain Generates Each Block's Hash
NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

How Blockchain Generates Each Block's Hash

A lot of people have adopted the blockchain way of solving problems. One of the reasons is because of the high security that it offers.

Generally, a blockchain contains blocks of transactions. Each block is connected to the block before it and the block after it. What connects them is the unique hash of each block.

In this tutorial, I will teach you how blockchains generate the hash of every block.

For an in-depth dive into how blockchain works, check this article: Blockchain for Beginners Tutorial – Learn to Code Smart Contracts with JavaScript and Solidity or watch this video series below:

Prerequisite

You need basic knowledge of Javascript to be able to follow through on this tutorial.

How to generate a block's hash

There are a lot of libraries for generating a block's hash. But we will use the SHA256 library for this tutorial. SHA256 is the most popular and is used by many renowned companies.

The SHA256 library takes any data given to it and returns a 64-character long string. Every string passed to the SHA256 library will always return the exact 64-character long string every time.

You can check out this link and play around with the UI to see how it works.

Blockchains do not use just any hash generated because of security reasons. It specifies what the first few characters must look like for the hash to be accepted. This means that the hash will have to be generated several times, and a record of what changes on each iteration will be kept for reference purposes.

For example, a blockchain may specify that the only acceptable hash must contain three zeros at the beginning.

To calculate the hash, we need to add a number known as a nonce to the string being hashed. The nonce usually starts from zero and is incremented every time the hash is generated until a hash beginning with three zeros is found. Then the hash and the nonce will be stored for reference purposes.

The code below will calculate the hash for "man":

SHA256("man").toString();
Enter fullscreen mode Exit fullscreen mode

However, we may run the function several times to get a string with three zeros at the beginning. Since the process will always return the same result, we need to add a number to the string and increment it until we get the hash we want.

The code we'd use for that will look like this:

let hash = "";
let nonce = 0;

while (hash.substring(0, 3) !== "000") {
  nonce++;
  hash = SHA256("man" + nonce).toString();
}

console.log(nonce);
console.log(hash);
Enter fullscreen mode Exit fullscreen mode

This code will produce 000d6575d4670dae39df9944e54c27dc4837beab1db23e2de264a7c1a3f38b1a after 5707 times instead of 48b676e2b107da679512b793d5fd4cc4329f0c7c17a97cf6e0e3d1005b600b03.

This level of security measures taken to build blockchain applications makes them very reliable and acceptable.

And That is how each block's hash in a blockchain is generated!

Conclusion

It is easy to relate to something that you understand the background. That is why I decided to explain what goes on behind the scene in blockchains when it comes to generating a block's hash.

We used the SHA256 library to demonstrate how blockchains ensure security for their network by the way they generate the hash of each block.

I encourage you to read this article if you still need to do so. The article explains how blockchain works using Javascript and how to write smart contracts with Solidity.

Top comments (0)