What Is Hash Function

Hashing is an essential thing in cryptography and it is often used in blockchain to make data secure and to protect against tampering. In this article, we will learn - what hash function is, what are the properties of a hash function, and how it is used in blockchain.
Hashing is the process of converting plain input text to encrypted hash value using the cryptographic algorithm. The message to be hashed is called input, the algorithm which processes it is called the hash function, and the encrypted output is known as hash values or digest.
What Is Hash Function

What is Hash function?

Hash Function is a cryptographic function that takes a variable length of input and produces a fixed length output. Meaning that, it takes an input text — no matter how long or small it is — but the output of the hash function will always be in a fixed length. When you deal with very large data, it’s difficult to manage and process it. However, the fixed length hash makes it manageable and easily traceable.
There are many hashing formulas available but any cryptographic hash function requires some qualities to be considered useful.

Unique Hash Values: Different inputs never generate the same output

With the hash function, it is impossible to generate the same hashed output from the different input text. By giving input X, for example, the hash function generates output Y, but it is not possible to generate output Y with any other input text expect X.
What Is Hash Function
The hash function is faster than the typical symmetric encryption; regardless of what length of input you give to the function, it will always provide output in a reasonable time.
Fixed Length Output
As described earlier, the hash function generates a fixed length of output regardless of the length of the input you provide. As the output of the hash function is tiny than the actual data, it also knows as compression function.
A slight change in the input will give a different output
Even with a small change in input, the output will be completely changed. Here in the following example, we’re passing a different length of inputs, and you can see that the output from the hash function is in fixed length.
What Is Hash Function
One way only (Pre-image resistance)
The hash function is one way only, meaning that from the output hash value, there is no way - or is computationally hard- to determine the actual input value, in another word - output value of the hash function cannot be reversed. Even if your hashed data would leak out; it’d be still secured because of this property.
It differs from encryption because encryption is a two-way operation; you can always decrypt the encrypted value with the right secret.
For example, here we have the input value “abc” and the hash function has generated the hashed value “ba7816bf8f01cfea414140de5dae 2223b0031a396177a9cb410ff61f20015ad”, now from this hashed value, it is difficult to get the actual input.

How Hash Function is used in Blockchain

Hashes are used to represent the current state of the blockchain and it ensures blockchain immutability. Every transaction contains some information like sender address, receiver address, amount timestamp and so on; this information combines and goes to the hash function, which generates a unique hash value called transaction hash or transaction ID. This hash is used to verify and can be confirmed that a particular transaction has happened on a blockchain or not.
The first block in the blockchain is known as a genesis block; which contains information about transactions; combine all the transaction creates a new hash. When the second block is created, the hash of the genesis block is added with all the transactions in a new block—this combination is used to generate a new respective hash. 
This process is repeated with all the blocks being added to the chain. Using a previous hash to generates a new hash creates dependency, thus if someone wants to change a single piece of information, they have to change all the information till current time —which takes a lot of computational and is strenuous. This makes blockchain secure and immutable.
Different blockchain uses different hash functions, bitcoin blockchain, for example, uses SHA-256 hash function to secure the data.
Here is a code example of how to Compute SHA256 Hash in C#