All Posts Posts

How to Calculate a Bitcoin Block Hash Manually

In this post we describe how to calculate an verify a Bitcoin block hash manually. We need to follow five easy steps to calculate the block hash.

If you want to try it out, you can use our online bitcoin block hash calculator.

Input Values of a Bitcoin Block Hash

But before we start with the calculation we take a look at the input values.

The SHA-256 hash function takes six inputs in order to calculate the block hash.

Input Explanation
Version Tells us the block version number. Is also used to participate in polls about protocol changes
Previous Block Hash (hashPrevBlock) 256-bit hash of the previous block header
Merkle Root hash (hashMerkleRoot) 256-bit hash of the Merkle root. The Merkle root is based on all transactions in a block.
Time Block timestamp as seconds since 1970-01-01T00:00 UTC
Bits (difficulty) Target to be met by the block hash
Nonce 32-bit number used to find the correct block hash in PoW

Where to get the data from?

Blockchain data are public. If you install a node you will be able to request the data from your node.

But there are also public services called block explorers which provide the data. Here is a list of block explorers you can use:

  • com

However, there is on caveat. The format in which the data are provided can be pretty messy. Some block explorers display the data in hex values others in decimal values. This requires a little cleanup before the actual calculation can begin. Some block explorers even give imprecise data like a truncated date without the seconds.

We take example data from the block number 600,000.

Input Value (original) Value in hexadecimal
Version 0x20000000 0x20000000
Previous Block Hash (hashPrevBlock) 00000000000000000003ecd827f336c6971f6f77a0b9fba362398dd867975645 00000000000000000003ecd827f336c6971f6f77a0b9fba362398dd867975645
Merkle Root hash (hashMerkleRoot) 66b7c4a1926b41ceb2e617ddae0067e7bfea42db502017fde5b695a50384ed26 66b7c4a1926b41ceb2e617ddae0067e7bfea42db502017fde5b695a50384ed26
Time 2019-10-19 02:04:21 as timestamp: 1571443461 0x5DAA5305
Bits (difficulty) 0x1715a35c 0x1715a35c
Nonce 0x3f93ada7 0x3f93ada7

Step 1: Converting into little-endian

All values need to be converted into little-endian hex format. Usually block explorers or hex calculators provide the data in big-endian. Little-endian means that the least significant byte is on the left side. (In big-endian it is on the right side)

big-endian to little-endian

Input Value in little-endian hexadecimal
Version 00000020
Previous Block Hash (hashPrevBlock) 45569767D88D3962A3FBB9A0776F1F97C636F327D8EC03000000000000000000
Merkle Root hash (hashMerkleRoot) 26ED8403A595B6E5FD172050DB42EABFE76700AEDD17E6B2CE416B92A1C4B766
Time 0x0553AA5D
Bits (difficulty) 0x5CA31517
Nonce 0xA7AD933F

Input data from Bitcoin block 600000.

Step 2: Concatenation

The next step is to concatenate all little-endian formatted hash values in the right order. The symbol || means concatenation. As result we receive our rawHex.

rawHex = Version || Previous Block Hash || Merkle Root hash || Time || Bits || Nonce

rawHex = 0000002045569767d88d3962a3fbb9a0776f1f97c636f327d8ec0300000000000000000026ed8403a595b6e5fd172050db42eabfe76700aedd17e6b2ce416b92a1c4b7660553aa5d5ca31517a7ad933f

Step 3: Convert into bits

The concatenated hash value now needs to be converted into binary format. Most hash calculators and libraries distinguish between string inputs and hexadecimal inputs.

Let us consider an example.

The string ā€œ2cā€ would have a binary representation of 0011001001100011. This is derived from the ASCII table.

Character 2 c
Binary ASCII representation 00110010 01100011

If we interpret ā€œ2cā€ as hexadecimal value its binary representation would be: 101100. And this is exactly what we need.

This step is usually done internal with many hash libraries. The resulting binary number of our rawHex would be too large to display here.

Step 4: Apply SHA-256 twice

On our bit representation of the rawHex we apply the SHA-256 now twice. This means, we first calculate the SHA-256 of the binary representation of our rawHex and then take the result (again as binary) and input it into our SHA-256 function again.

2 x SHA-256: 915fcd96d1c84298a8fbfb9c13a9f7b4760e9056683107000000000000000000

Step 5: Convert to little-endian

The last step is to convert the output of the last step into little endian hexadecimal format. Once this is done, we have your block hash.


Pitfalls when calculating a Bitcoin block hash

If you want to calculate or verify a Bitcoin block hash manually, you need to take care of the format of your input values.

Version: The version can be given as 1, 2, 0x00000002, 0x20000000, or in different values. If the version is given as decimal values like 1 or 2 it needs to be converted into a padded hexadecimal value first (0x00000002). If it is given as hex-value like this 0x20000000, it can be used as input value right away (it still needs to be converted to little-endian thought).

Date: make sure you use the correct date. Some browsers might give you wrong date values, because they apply the wrong time zone. And some block explorers cut off the seconds from their dates.

Further links

Another explanation can be found here and here.