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 Block hashing algorithm, which calculates the block hash of Bitcoin Block 125552. 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 blockexplorer.com:

Block #125552
BlockHash 00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d 

Last update: 2018.

Table of Contents

 About This Book

 Introduction of Bitcoin

 Bitcoin Blockchain

 Bitcoin Wallet

 Bitcoin Core

 Bitcoin Transaction

 Bitcoin-Qt - Bitcoin Core GUI

Bitcoin Data Structure

 Data Fields of Bitcoin Block

 Merkle Root of Bitcoin Block

 Verify Merkle Root with Python

 Data Structure of Bitcoin Block

 "getblock blockhash 0" - Serialized Hex Blcok Data

Block Hash Calculation Algorithm

 Block Hash Calculation in Python

 References

 PDF Printing Version