Hashing in Blockchain

Fayosi Oyende
5 min readFeb 18, 2022

--

WHAT IS A HASH?

Hashing (Hash) is a common term used when discussing blockchain technology, it helps protect the security of transactions made on the blockchain. A hash refers to a mathematical function that turns any random input of data into an encrypted output with a fixed length. That is, regardless of the amount of data or file size (the data could be a word, a sentence, a longer text or an entire file), its unique hash will always be the same size.

Any data that has been converted into a hash, cannot be reversed. What this means is that once the hash value has been created, it is impossible to guess the length of the original input.

Key points:

  • A hash is used for security purposes and it helps secure transactions made on the blockchain network
  • A hash converts any random data of any into an encrypted output of a fixed size.
  • A hash, once converted, is difficult to reverse.

HOW DO THEY WORK ?

As we earlier mentioned, A Hash is a cryptographic function that turns an input string of any length into an output of a fixed length. In the context of cryptocurrencies, the transactions are taken as input and run through a hashing algorithm which gives an output of a fixed length. Bitcoin uses (SHA-256) Secure Hashing Algorithm.

Here’s an example of how Hashes work:

Photo credit: blockgeeks.com

As shown in the image above, the data input produced hash values of the same length and structure; this comes in handy especially when dealing with a lot of data or transactions. Asides cryptocurrencies, the most common usage of hash functions is in the storage of passwords.

CRYPTOGRAPHIC HASH FUNCTIONS

While hash functions are data structures that are commonly used in computing systems for authenticating information and checking the integrity of information, they are considered cryptographically weak because even though they are not easily decipherable, they can be solved.

However, Cryptographic hash functions are a special class of hash functions that have different properties that make it ideal for cryptography. They have certain attributes that make them perfect for encryption.

Here are some attributes a cryptographic function must have to be useful:

COMPUTATIONALLY EFFICIENT: Firstly, they should be able to return the hash of an input in a short period of time. To be useful the ordinary computer must have the ability to process mathematical labor quickly.

Nowadays, many computers that can be found in people’s homes can perform these functions in a fraction of a second.

DETERMINISTIC: This means that for any given input, the hash function must always give the same result. This attribute is necessary, as hash functions are used to verify the authenticity of a digital signature on a transaction. If they don’t return the same output, It would be impossible to verify the hash of a specific input thereby defeating the purpose of a hash function.

In other words, hash output has to be the same every time otherwise, it will be impossible to keep track of the output.

COLLISION RESISTANT: Cryptographic functions must be collision resistant, this means that no two input hashes should give the same output hash. In other words, it should be extremely unlikely to find two different inputs that produce the same output. For the most part they must have their own unique hash.

The inputs to a hash function can be of any length, thus there are several possible inputs that can be entered into a hash function. However, outputs are of a fixed and structured length. Thus, there is a limited number of outputs that a hash function can produce.

Since the number of inputs are basically infinite, but the outputs are limited, it is highly probable that more than one input will produce the same output.

No hash function is collision free but the aim is to make the possibility of two inputs having the same outputs so unlikely that it does not pose a risk.

PRE-IMAGE RESISTANT: This attribute means that the given output of a hash function must be undecipherable, it should not reveal any information about the input. This is useful to ensure security against hackers as the hash value must conceal any clues about what the original input was.

PUZZLE-FRIENDLY: For a hash to be puzzle-friendly, means that the distribution form in which the input is chosen should be as wide as possible, so wide that it should be infeasible to find a value that will give the output of the hash value.

HASHING AND CRYPTOCURRENCY

Cryptocurrency is a digital payment system which is built on Blockchain technology and doesn’t rely on third party institutions like banks or Mastercard to verify transactions. Blockchain assists in preventing fraudulent transactions by using a unique encrypted value in the form of a series of numbers and letters that do not resemble the original value, which is called a hash.

The process of creating new cryptocurrency is called mining. The mining process also confirms transactions on the cryptocurrency network and makes them trustworthy; Cryptocurrency mining involves working with a hash.

Cryptocurrency mining uses PoW (Proof of Work) to confirm transactions and produce new blocks to the chain.

SOLVING THE HASH

Solving the hash starts with the data available in a block header; every block on the blockchain is assigned an original identifier, the block is identified by information included in the header of the block.

The block header consists of such details as:

  • The version number of the Blockchain
  • A timestamp
  • Hash pointers (The hash used in the previous block)
  • Nonce (Number that miners are solving for)
  • Hash of a Merkle Root (Hash of all the hashes of all the transactions that are a part of a block)

The process starts with the data available in the block header and is basically solving a complex mathematical problem. The miner focuses on the nonce (number used only once) which is a string of numbers.

Before this, the miner will need to carry out a trial and error process to decide which string to use as a nonce; this is because the nonce is a random string. It is highly unlikely that a miner will successfully come up with the correct nonce one the first try.

Once a nonce is identified, miners will then focus on the nonce of the previous block hashed content. To be considered successful, the new hash needs to be less than or equal to the target hash. Then the miner gets a reward, and the block is added to the blockchain.

GLOSSARY

Polynomial time: A polynomial function is a function such as a quadratic, a cubic, a quartic, and so on, involving only non-negative integer powers of x.

PoW (Proof of work): PoW is what is required from nodes on a network to provide evidence that they have expended computational power (i.e. work) in order to achieve consensus in a decentralized manner and to prevent bad actors from overtaking the network.

--

--