Author

Topic: [GUIDE] Hash function (Read 55 times)

legendary
Activity: 1596
Merit: 1288
December 15, 2021, 11:05:10 AM
#1

Disclaimer: The information's contained in this explanation is the result of my understanding of hashing and may contain some errors. Search and confirm about it.


Table of contents

      1. Introduction
      2. Bitcoin Hashing
      3. How does a hash function work?
      4. SHA-1, SHA-2 and SHA-256 Hash Algorithms
      5. Why do we need it?
      6. Hash function properties
      7. Hash function & Mining




Introduction

If you spent some Bitcoins or did some research to find out how it works you will notice the word "cryptographic hash algorithms," "Bitcoin hash" or some technical vocabulary like "SHA-1, SHA 256, MD5, BLAKE, and RIPEMD."
There is no doubt that you are familiar with the hash functions, even if you do not realize it, you use it daily and every time you create a user account on a website, your password acts as an entry point for the hash function.



Bitcoin Hashing

The hash function is a calculation that aims to generate a fixed-size output from a variable-sized input after it performs an operation (known as hash functions (implemented as hashing algorithms)) on it.
We will always get the same hash from the same data, but modifying the data even a single bit will completely change the hash.
usually written as hexadecimal.
Mostly designed as one-way functions, it is very easy to create the output from the input, but relatively difficult to go in the opposite direction.



How does a hash function work?

Let's go back to the web account example, when you create an account, the password is run through a hash function, and the hash hash digest is stored in the server. When you want to log-in back, he same hash function is run on, and the server checks to see if the result matches the stored value.

The same applies to Bitcoin. The possible output sizes for each hash algorithm are always constant. For example, SHA-256 produces 256-bit output, while SHA-1 always produces 160-bits digest.

Let's try it with some examples:

Code:
Input: Husires
Output: 198d93f2c0bff9767d4cdc047f2191b0921d81e410c10c0744311fadfdb516f9
Input: husires
Output: 3121cef89f206bef49882eb1aeb63a9e1d3e35075c27df817f5096dc84c2511a
What did you conclude from the examples above?



SHA-1, SHA-2 and SHA-256 Hash Algorithms

SHA stands for Secure Hashing Algorithm, numbers explain the differences between them in (how the resulting hash is created from the original data) and in the bit-length of the signature.

SHA-1 is a 160-bit hash.
SHA-2 comes in various lengths, but the most famous is 256 for "SHA-2," "SHA-256" or "SHA-256 bit."

Note that large bit length does not automatically mean that the hash algorithm produces safer hashes.

A bit has two possible values: 0 or 1.  the number of possible values raised to the number of bits gives us unique hashes which will be 2256 possible combinations for SHA-256.

Of course it can be solved but it needs a large amount of processing power which means that new bitcoins are mined over a long period of time, not all at once and thus the network still takes  approximately 10 minutes to solve a block.



Why do we need it?

It can be used in many areas such as large file analysis, data management, and information security applications, but we will limit the explanation to bitcoin and some of the roles it plays in the mining process and the creation of new addresses and keys.

Many cryptocurrencies depend on it in link and condense groups of transactions into blocks, and cryptographic links between each block.
Likewise, to break a fragmentation function, it requires countless brute force attempts. In order for a person to "return" to the coding hash function, they would need to guess what the input was by trial and error in order for the corresponding output to be produced.



Hash function properties

To be considered effectively safe, it must have these characteristics:

  • Collision resistance: It is essential that different inputs do not have the same hash output.
  • Preimage resistance: finding another message that can give the same hash result

Collision resistance: The reason for their occurrence comes from the idea that the potential inputs are infinite, while the potential outputs are limited.
SHA-1 was the primary algorithm (no longer safe because collisions have been found) but SHA-2 is the new standard.

Note that: SHA1 was never commonly used in Bitcoin.
A new generation of SHA-3 algorithms may be used at some point on the road

Preimage resistance: it is similar to finding the result to x+y=10 you have absolutely no way of guessing the correct x and y even when you know the result is 10. what you can do is to find other values that can give the same result like 1 and 9 or 4 and 6,...
Source:https://bitcointalk.org/index.php?topic=5265882.msg54911150#msg54911150


Other Properties:

  • Privacy: It is necessary to hide the input information. It should not be easy, or even possible, to know information about the inputs just by looking at the output
  • Randomness: Randomization enables some features that we will explain in detail below, such as: Proof Of Work


Hash function & Mining

We have many uses such as checking balances, segmenting transactions within a block to form Merkle Tree and others. Output hash begins with a certain number of zeros. The number of zeros determines the difficulty of mining, and varies according to the network hash rate (Bitcoin protocol easy to maintain average mining block time close to 10 minutes by adjusting mining difficulty.)

Thus there is no need to find collisions.





Sources
Code:
https://www.youtube.com/watch?v=0WiTaBI82Mc
https://www.youtube.com/watch?v=mMxkxwPSfvo
https://en.wikipedia.org/wiki/Cryptographic_hash_function
https://www.thesslstore.com/blog/difference-sha-1-sha-2-sha-256-hash-algorithms/
https://xorbin.com/tools/sha256-hash-calculator
https://bitcoinexchangeguide.com/bitcoin-hash-functions/
https://academy.binance.com/security/what-is-hashing

Jump to: