TeachMeBitcoin

Custom Python Hash Verifier

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python Block Hash Verifier

In this final technical guide, we will verify the identity of a real Bitcoin block. We will take a raw 80-byte header, perform the Double-SHA256 calculation, and handle the Endianness conversion to match a Block ID from a block explorer.

The Python Block Verifier

import hashlib

def calculate_block_id(header_hex):
    # 1. Convert Hex Header to Bytes
    header_bytes = bytes.fromhex(header_hex)

    # 2. Perform Double-SHA256
    first_pass = hashlib.sha256(header_bytes).digest()
    second_pass = hashlib.sha256(first_pass).digest()

    # 3. Handle Endianness (Reverse the bytes)
    block_id = second_pass[::-1].hex()

    return block_id

# --- Simulation (Block 125,552) ---
# This is a real 80-byte header from the Bitcoin blockchain
# Structure: Version (4) | Prev (32) | Merkle (32) | Time (4) | Bits (4) | Nonce (4)
raw_header = (
    "01000000" + # Version 1
    "81cd6495d3550e553c3066d40026a090ef666993a46610000000000000000000" + # Prev Hash
    "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" + # Merkle Root
    "c7f5d74d" + # Timestamp
    "f2b9441a" + # Bits
    "42a14695"   # Nonce
)

print(f"--- Verifying Block Header ---")
print(f"[*] Input Raw Header: {raw_header[:20]}...")

# Calculate
computed_id = calculate_block_id(raw_header)

print(f"\n[*] Computed Block ID:")
print(f"    {computed_id}")

# Verification against real data
expected_id = "00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d"
# Note: Expected ID for this specific header is 125,552
print(f"\n[*] Verification:")
if computed_id.endswith("bd1d"): # Partial check for brevity in display
    print("    [SUCCESS] The header is authentic and points to Block 125,552!")
else:
    print("    [FAILED] Hash mismatch. Header data may be corrupted.")

How to Run the Verifier

  1. Ensure you have Python 3 installed.

  2. Copy the code into a file named block_verifier.py.

  3. Run it using python3 block_verifier.py.

Technical Takeaways

  1. Immutability Verification: This script is exactly what a full node does when it receives a new block. It hashes the header to see if it matches the promised Work and the promised Identity.

  2. Hex to Byte Conversion: Real protocol data is binary. We use hex in scripts only for human readability.

  3. The Reversal: Without the [::-1] line, the hash would look completely different and wouldn't match any block explorer, even though the math is correct.

Congratulations! You have completed the Block Structure technical masterclass. You now understand the deep mechanics of the 80 bytes that power the Bitcoin blockchain.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!