Cryptographic Hash Functions in Blockchain

Cryptographic Hash Functions in Blockchain

5 mins read9.9K Views Comment
clickHere
Updated on Dec 18, 2023 17:40 IST

The cryptographic hash function in Blockchain is a way to secure the message block and is used to connect the blocks in a chain. Briefly, In the blockchain, each block contains its own block hash and a hash of its previous block. It helps them to form a cryptographically secured linear chain of blocks. Let's understand more!

The article below explains how cryptographic hash functions are used in blockchain to secure your message. You’ll go through the properties of cryptographic hash functions. Afterwards, the SHA-256 hash algorithm (used by Bitcoin) will be explained.

How does Blockchain Work?
How does Blockchain Work?
Have you wondered how blockchain technology functions? At its core, blockchain is a distributed ledger that records transactions across many computers so that registered transactions cannot be altered retroactively. This...read more

Table of Content

What is Hashing?

Hashing is converting an original piece of data into a digest or hash. The process uses cryptographic hash functions for the irreversible conversion of the message.

hashing

What are Cryptographic Hash Functions?

The hash function applies to the message to generate a message digest, a fixed-size hexadecimal output. 

  • It takes an arbitrary size input as a message. INPUT M: The Message
  • Uses a Cryptographic hash function H to encrypt the message M.
  • Generates the output called Digest. 

H(M) = Digest

Cryptographic hash functions are irreversible. That means it’s a 1-way function, and one can’t generate the message back using the digest.

There are a bunch of cryptographic hash functions. For example, SHA-224, SHA-256, SHA-512, KECCAK-256, Whirlpool, etc.

Properties of Cryptographic Hash Functions

The process of hashing secures the message and makes the computation easy. It compresses the message into hash/digest, which is efficient for computation and communication. Briefly, 

Above all, below are the properties of cryptographic hash functions.

Deterministic

Cryptographic hash functions are deterministic. It always generates the same hash for the same input data. However, even if a single letter changes, the hash changes dramatically. 

For instance:

M: Hello →  SHA-256(M) →  185F8DB32271FE25F561A6FC938B2E2643……..

M: HellO → SHA-256(M) → 4FF7975B53DB6C029D88F6AC67BD78D1……….

Notice the difference in the output of both messages. 

Moreover, the slightest change in either function or message can result in significant changes in the digest. This property is known as the Avalanche effect.

Nearly Collision Free

Hash functions are one way. For a given X, it’s easy to calculate H(X)=D. In contrast, using D, one can’t find X.

However, there’s a little possibility of getting H(X)=H(Y); where X is not equal to Y. Yet, it’s difficult to find X and Y using the same hash.

For instance, if a hash function produces N bits of output. An attacker needs to compute 2^(N/2) hash operations on random input to search for the second match of the output. 

Hence, for every 256 hash functions, the attacker must compute 2^128 hash operations. Even if one computation takes 1 microsecond, it will require approximately 10^25 years to match the output. 

Note: The phrase used is “difficult to find” is because the collision can be possible. However, it depends on the cryptographic function used and the digest size.

Information Hiding

Since the digest of a message is irreversible, and it’s nearly impossible to find the same hash for different messages. Therefore, the only way to verify the message is to hash the possible message. Then, check if its digest matches the original message’s digest. 

Hiding helps to commit a message and then check it later. Here, the commit means to hash and store the digest of the message. 

2022_03_image-25.jpg

Test your inputs on: http://www.blockchain-basics.com/HashFunctions.html

SHA-256 Algorithm

SHA-256 stands for Secure Hash Algorithm that generates a 256-bit digest of a message. This is one of the most renowned hashing algorithms used by several cryptocurrencies. Another popular application of this algorithm is Digital Signing Verification.

Let’s dig into the steps to process a message using the SHA-256 Algorithm.

Step 1: Pad the message such that the length of the message size is a multiple of 512. 

Here, pad the message means adding some extra bits to the original message. Therefore, the total length is exactly 64 bits less than a multiple of 512. 

For instance, the length of message M is L. It means L+P+64 =n*512, where L is the length of the message and P is padded bits.

Note: The bits padded to the message should start with ‘1’. And the rest bits must be ‘0’ till we are exactly 64 bits less than the multiple of 512.

Step 2: Add length bits (Padding Length)

We padded bits to the original message in Step 1. Now, we can append the length bits ie, expressed as 64 bits previously, to make it exactly equal to a multiple of 512.

To calculate the 64 bits, L(the original length of the message) mod 2³²

So, to get the entire message block, L+P+[L mod 2³²] = n*512

Step 3: Initialize Chaining Variable

The entire message is broken into n blocks of 512 bits. Such as M1, M2, M3……….Mn.

We need a few buffer values to use further. These 0-63 hexadecimal values are called Keys and are denoted by ‘k’ afterwards.

 

Step 4: Process each Block

  • The entire message is broken into n blocks of 512 bits.
  • Each 512-bit block is broken into 16 sub-blocks of 32 bits each.
  • There are 64 rounds of operations. And the output generated will be input to the next round of operations.
sha
Courtesy: Article

The K[i] in all the rounds are already initialized. W[i] is calculated individually for each block depending on the number of iterations processed.

The entire cycle repeats itself till it reaches ‘n’. The number of 512-bit blocks.

Finally, The last result will consider as SHA-256 digest. 

2022_03_image-26.jpg

Consensus Mechanisms in Blockchain
Consensus Mechanisms in Blockchain
Consensus mechanisms in blockchain impose a set of rules to keep members (or nodes) synchronized in a decentralized network.

Conclusion

The article explained the basics of Cryptographic hashing, hash function, and properties. Later, it explains the SHA-256 hash algorithm which is popularly used in several applications. Hope you enjoyed the learned from the article.

Blockchain Interview Questions
Blockchain Interview Questions
The below article goes through the most common blockchain interview questions asked. The questions are divided into 3 categories of basic, intermediate, and advanced level blockchain questions. 

FAQs

What is Cryptography?

Cryptography is a process to encrypt and decrypt a message. Encryption, message to ciphertext. Decryption is converting the ciphertext to the message. The process intends to secure and compress the original message.

What are the other cryptographic hash functions?

MD5, SHA-1, SHA-512, KECCAK-256, Whirlpool, etc

What are the applications of Cryptographic hash functions?

Digital Signing for Verification, SSL for browser, Password security, and integrity check.

Why are Cryptographic Hash Functions Important in Blockchain?

  • They ensure data integrity: Any change in input data, even a minor one, results in a completely different hash, making tampering evident.
  • They enable secure and anonymous transactions: Hash functions are key in preserving the privacy of the blockchain users.
  • They are foundational to the linking of blocks: Each block in a blockchain is identified by its hash and contains the hash of the previous block, creating a secure chain.

How are Hash Functions used in Blockchain?

Hash functions in blockchain are used for creating the block's unique fingerprint. Each block contains a hash of its transactions and the hash of the previous block. This chaining of blocks with hashes ensures the immutability of the blockchain: if any data in a previous block changes, its hash changes, thereby invalidating all subsequent blocks.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio