What is a hash function?
A hash function is a mathematical equation that enables many forms of encryption, such as digital signatures, and includes everything from HTTPS to payments made on websites. Cryptographic hash functions are widely used in blockchain technology. The term hash function may sound a little intimidating, but it’s actually pretty easy to understand; a cryptographic hash function is just a mathematical equation, just like the functions you learned in high school, such as a linear function written as y=mX+b. A cryptographic hash function is somewhat similar. A hash function is essentially a formula with a set of special properties that make it useful for encryption.
Hash functions take an input and produce a fixed-length output. The output value is a summary of the input data. For example, think of a paper document that you keep crumple up to the point where you can no longer even read the contents inside. It is also almost impossible to recover the input value without knowing the original data. Let’s look at a simple example of a hash function:
This is a very simple example of how a hash function works and gives us a brief understanding of how it works. As mentioned, it is a mathematical algorithm that converts input data of arbitrary size into an output of fixed size. A hash function is designed to be a one-way function and is impossible to reverse. However, in recent years, several hash algorithms have been compromised. For example, this happened to MD5, a well-known hash function, and it is now easily reversible. An ideal cryptographic hash function should have the following properties:
• The calculation of the hash value should be fast for any type of data.
• It should be impossible to derive the input value from the output value.
• It should be impossible to find two identical outputs, even by chance.
• Any small change (even a single character) in the input should change the output value.
In fact, hashes are the backbone of a cryptocurrency on a blockchain. A blockchain is a distributed database that is formed by linking blocks of information. A blockchain processes only valid transactions and prevents fraudulent transactions. The encrypted codes are a series of numbers and letters that do not resemble the original data and are called hashes. A hash is created using an algorithm and is essential for managing the blockchain.
Another example of a hash function:
In World War II, German forces used the Enigma machine to communicate and transmit information to each other. This machine would convert the received information into code and send it to its destination. In this way, no one could understand the conversation of the Germans; for example, the US military would listen to those conversations, but they would not understand.
The hash in the block is exactly like this. You see a code that is made up of letters and numbers; but those letters and numbers came out of complex mathematical functions and contained a world of information inside.
Let's say you and I sign a smart contract on the Ethereum blockchain. If that contract is not hashed or encrypted, someone may change it. So, as a user, we need to take the necessary measures to prevent the information on the blockchain from being changed. One of these important measures to maintain the security of the blockchain is hashing.
Do all devices and all blockchains have the same type of encryption or hashing algorithm? The answer is no. Hashing is different in the blockchain.
How does a hash work?
The foundation and main pillar of cryptocurrencies or digital currency is the blockchain, which is a global ledger. Blockchains only contain valid transactions, which prevents fraudulent transactions and re-spending of currency.
One way to prevent fraudulent transactions is to use a series of numbers and letters that have no resemblance to the original data, called a hash.
Hash functions produce outputs with different combinations; but the output sizes for each hash algorithm are always fixed. For example, the SHA-256 algorithm can only produce a 256-bit output, while SHA-1 always produces a 160-bit expression.
For example, if we were to hash the word donyaye-trade using the SHA-256 function, we would get an output similar to the following:
9c31874e7a69ad566c600f096b6088c500b03ff08c16c99bb0ec2bd0563e2b1d
However, if we were to hash using the SHA-1 function, the output would be similar to the following:
49191a9fcf33fee7324a2827e0a342f4cc55193e
This means that depending on the hash function, we may get different outputs.
Note that a slight change in the input text will result in a different output. But since we are using SHA-256, the outputs will always have a fixed size.
Consider the following example:
donyaye-trade:
9c31874e7a69ad566c600f096b6088c500b03ff08c16c99bb0ec2bd0563e2b1d
Donyayetrade:
e3ab384431e89010c6093b39014ca071c87c3cbe8723fc6a28430d81fd257be5
As mentioned, blocks feed their information to hashing machines, which convert the input data into a fixed combination.
The size of the blocks is important in the hashing process. For example, for the SHA-1 function, the size of the input data (which is the block size) must be a maximum of 512 bits. If the input data is 1024 bits long, the hashing operation is performed twice. That is, the block of information is divided into two parts and each part is hashed separately.
Of course, this is not very natural and each block gives an input proportional to the hash function.
The encryption of blocks is the same. To hash a block, the hash of the previous block.
It is necessary. That is, in the output of the hash of the second block, the hash of the first block is effective.
This process continues until the end.
That is, in the hash of the hundredth block, the hash of block 99 is effective. In the hash of block 257, the hash of block 256 is effective.
That is why if a data in a block changes, the hash of all the blocks after it must also change. This is impossible and never happens.
The process of hashing or hashing
As mentioned, for hashing data, we need complex mathematical functions. These mathematical functions convert input data into indistinguishable outputs. This type of encryption increases security. Because no device or computer can reach the input data through the output phrase.
In blockchain, there is another concept called nonce. Nonce is a series of numbers and is very important in generating a new block hash. Because if the nonce and hash code of the new block are not the same as the nonce and hash code of the old block, the hashing process will not be successful.
If the new hash is equal to the target hash, then it is accepted as the correct answer to the problem and the node is rewarded and the block is added to the blockchain.
You might tell yourself that today's miners can reach the new hash and receive the reward in the shortest possible time, but in reality this is not the case. Devices must use their hardware power to guess the nonce and hash correctly and generate it. This process will take time and will result in high power consumption.
The miner potentially has to try a very large number of nonce options to arrive at the correct nonce.
In blockchain and hashing, there is a concept called difficulty. Difficulty refers to the degree of difficulty of creating a hash. The higher the difficulty level in the network, the more time it is likely to take to correctly guess the code and solve those complex functions. It is worth noting that miners are only rewarded if they are the first node to produce a block hash.