Blockchain Fundamentals – Hashing, Blocks

Introduction

To build a blockchain, we need to understand the components to be used in it. Please check the earlier tutorial explaining blockchain technology in a nutshell which serves as a refresher for this tutorial. The components/concepts – Hashing, Blocks, Blockchain are interlinked in the Blockchain framework. One of the efficient orders of understanding these concepts is;

Blockchain Framework

We shall develop our understanding of one topic on top of another as per the above framework.

Pre-requisites:

You can also watch me go over this blog tutorial in the video explainer:

Hashing

Hashing is a prevalent concept of cryptography. The idea behind hashing is to create a fixed-length unique string of numbers and mixed-case letters, often called digital fingerprints, for any piece of digital data. We call the generated digital fingerprint – “HASH” of the given input data. We can generate hash values by passing the data or information through a hash function or algorithm.

Hash function

Some of the cryptographic hash functions are:

  • MD5
  • SHA-256
  • SHA-512

Any given piece of data always generates the same hash value. Any slight modification of the data will generate a completely different hash value. The hash function is irreversible, so we cannot reproduce the original data from its hash value. Instead, we can use the generated hash value as a unique identifier of the data. The hash (unique identifier) allows us to reference its original data and access and process it efficiently.

Hashing is one of the most critical components of the Blockchain stack and forms one of the fundamental pillars of Blockchain technology. For example, the Bitcoin Blockchain protocol uses the SHA-256 hash algorithm for each block on the Blockchain. So, every block on the Blockchain gets a unique identifier.

  • SHA – Secure Hashing Algorithm.
  • 256 – Length of the output hash in binary representation.

Let’s pass a text content – “Cryptographic hash functions” through SHA-256 and check for its hash value output.

In the above figure, the generated hash value is in hexadecimal representation when expanding to binary representation generates 256 numbers of 0s and 1s.

Demonstration of Hash

As you understand how hash values are generated and work, let’s walk through a demonstration. Here, we’ll go through a website to experiment with hash values.
Go to the following website: https://andersbrownworth.com/blockchain/hash
Enter the text data in the data section and get the associated hash value as shown in the below figure:

Demonstration of Hashing

As you enter each letter along with spaces, the hash gets changed. The concept of blocks will now use the hashing concept. It’s essential to understand each of them in isolation to fit them in the above-discussed framework.

Block

There is a brief explanation about blocks in our previous tutorial, and we shall now explore it in detail. Blocks are one of the fundamental components of the Blockchain. Blocks are building blocks of the entire Blockchain. A Block is a container that holds a list of transactions that needs to be added to the Blockchain. As we let the transactions run through the ledger, we could get an enormous list of transactions. This enormous list makes it very difficult to manage and work with them. To give us more control, we can split up the enormous list of transactions into finite ones. We can understand this from the below figure;

Block creation from infinite ledger

As transactions fill the block, we shall add the block into the Blockchain, which we will explore in the following tutorial. These blocks split up the infinite ledger into smaller byte-sized data pieces. So, we can use the blocks to manage the entire system more efficiently. Let’s investigate the details of a single block more closely. Blocks store the transactions in their body, and along with transactions, they hold some other information which we refer to as block attributes. Building on top of hashing concept, the first attribute is the Hash of the block. As you might’ve guessed, we get the hash of a given block on passing the block’s data through the hashing function. Bitcoin uses SHA256 hashing function as shown in the below figure;

Hashing the Block using SHA256 algorithm

If the data inside the block changes slightly, the hash function will generate a completely new hash value. So, if a single transaction changes on the block, the hash value will change, letting everyone know whenever a block gets tampered with, making the blockchain a secure way to store information.
The second block attribute is the Block Header. Block Header contains the details about the structure of the data inside the block.

Block Header

Exclusively, the Block header stores four pieces of information that are crucial for the system:

  1. Previous Block’s Hash: We store the previous block’s hash in the current block header. We use the hash to refer to a given block. So, this connection links the blocks together. Thus, allowing us to know which block comes before and after any block on the chain.
  2. Time: Whenever a computer or node that validate a block creates a block, the block header stores its creation time. It helps us know when a set of transactions took place and helps us catch if anyone is attempting to do a transaction twice. So, we can avoid the double-spending problem mentioned in the previous tutorial using the time-stamps.
  3. Merkle Root: It is also a hash representing every transaction inside the block. To get the Merkle Root, we should follow the following steps;
    • Each transaction is hashed using the hashing technique discussed above.
    • The pairs of the hashes of transactions within the block are repeatedly hashed together
    • These pairs are again hashed together over and over again until we get a single hash value.
    • The final hash value is a Merkle Root of that block.
    • If you’re a little confused from the above steps, have a look at the below figure; We can use this Merkle root to manage the transactions using their hash values if some problem occurs.
  4. Nonce: Nonce is out of scope for now. It will be explained in detail in future tutorials when we cover the concept of mining. Just know for now that it is an arbitrary number that can be generated and used only once for any given block. The Blockchain system requests for a hash value that starts with a certain number of zeros. Computers that validate the blocks guess for this Nonce over and over. A correct Nonce can combine with the block’s data resulting in the demanded hash value with leading zeros. As the system requests more zeros, the difficulty to generate the Nonce increases, which we refer to as block’s difficulty.

Along with block header and hash, the third block attribute of any given block is the Block Size. Block size is the system parameter that states the amount of space a block holds the information. Block size helps to decide how long it takes for a block to fill up and how many blocks will be on the chain. Once decided, it’ll be the same for every block on a given chain. Size can only be changed if the developer decides to update the system with differed block sizes. So, if the block size is 1MB, as it reaches its limit, the block is full, and it’s time to create a new block.

Demonstration of Block

We’ve understood about the block, its functionality, and its role in the whole system. We shall go through a simple demonstration on the block using the following website: https://andersbrownworth.com/blockchain/block

Let’s interact with the block;

Stage 1: Before entering any data in the block

We shall simulate the process of mining using the above website. First, let’s add some data to the block and press the Mine button at the bottom to generate a block.

Stage 2: After entering some data in the block

As we press the Mine button, the block color turns green, Nonce gets changed, and the hash value gets updated, creating a new block. As we change the data, the hash gets updated and turns the block’s color to red, referring to the block’s data being tampered.

Stage 3: Block’s data is changed/tampered

Conclusions and further steps

We have understood about Hashing and Blocks in detail. We shall see how they fit into the whole system in later tutorials. We shall look after the Blockchain concept in the following tutorial, which builds on the block concept.