Block Hash Calculation Algorithm

This section describes the Block Hash Calculation Algorithm.

Bitcoin uses the following algorithm to calculate the block hash:

First lets follow the Python example given in the Block hashing algorithm at https://en.bitcoin.it/wiki/Block_hashing_algorithm, which calculates the block hash of Bitcoin Block 125552, https://www.blockchain.com/btc-testnet/block /00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d. Note that I had to replace string decode() and encode() with binascii.unhexlify() and binascii.hexlify() on my Python installation.

C:\>python
>>> import hashlib
>>> import binascii

>>> header_hex = ("01000000" +
...  "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
...  "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
...  "c7f5d74d" +
...  "f2b9441a" +
...  "42a14695")
>>> header_bin = binascii.unhexlify(header_hex)
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()

>>> binascii.hexlify(hash)
b'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'

>>> binascii.hexlify(hash[::-1])
b'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

The result matches will with the block hash given in blockchain.com:

Block #125552
BlockHash 00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d 

Table of Contents

 About This Book

 Introduction of Bitcoin

 Bitcoin Blockchain

 Bitcoin Wallet

 Bitcoin Core

 Bitcoin Transaction

 Bitcoin-Qt - Bitcoin Core GUI

 Bitcoin Mining

 Bitcoin Consensus Rules

Bitcoin Block Data Structure

 Data Components of Bitcoin Block

 Data Properties of Bitcoin Block

 Merkle Root of Bitcoin Block

 Calculate Double-SHA256 Hash with Python

 Verify Merkle Root of 2 Transactions

 Verify Merkle Root of 7 Transactions

 Data Structure of Bitcoin Block

 "getblock blockhash 0" - Serialized Hex Block Data

Block Hash Calculation Algorithm

 Block Hash Calculation in Python

 Calculate Double-SHA256 Hash with Java

 Bitcoin Transaction Data Structure

 Bitcoin Blockchain APIs

 Copay - Bitcoin Wallet

 Archived Tutorials

 References

 Full Version in PDF/EPUB