<p>The data to be hashed is</p>
<ul>
<li>Version</li>
<li>Previous Block Header Hash</li>
<li>Merkle Root</li>
<li>Timestamp</li>
<li>Difficulty</li>
<li>Nonce</li>
</ul>
<p>The <a href="https://en.bitcoin.it/wiki/Difficulty" rel="nofollow noreferrer">difficulty</a>, which is loosely an inverse of the target, is in a compact representation known as &quot;bits&quot;.</p>
<p>The <a href="https://bitcoin.stackexchange.com/q/10479/13866">Merkle root</a> is a hash produced in a tree-wise fashion from hashes of transaction data.</p>
<p>Most of the data must be in little-endian form.</p>
<p>A good explanation in detail can be found at</p>
<ul>
<li><a href="https://medium.com/hackergirl/how-to-calculate-the-hash-of-a-block-in-bitcoin-8f6aebb0dc6d" rel="nofollow noreferrer">https://medium.com/hackergirl/how-to-calculate-the-hash-of-a-block-in-bitcoin-8f6aebb0dc6d</a> which provides an example in Python.</li>
<li><a href="https://dlt-repo.net/how-to-calculate-a-bitcoin-block-hash-manually/" rel="nofollow noreferrer">https://dlt-repo.net/how-to-calculate-a-bitcoin-block-hash-manually/</a></li>
</ul>
<hr />
<p>Related:</p>
<ul>
<li><a href="https://bitcoin.stackexchange.com/q/70336/13866">What is the difference between Root Hash and Block Hash?</a></li>
<li><a href="https://en.bitcoin.it/wiki/Block_hashing_algorithm" rel="nofollow noreferrer">Block Hashing Algorithm</a> (wiki)</li>
<li><a href="https://developer.bitcoin.org/reference/block_chain.html" rel="nofollow noreferrer">Block Chain</a> (developer reference)</li>
</ul>

CC BY-SA 4.0
2021-07-30T16:33:04.317Z
2022-10-09T09:00:37.603Z
2022-10-09T09:00:37.603Z
RedGrittyBrick
<p>Agree with @RedGrittyBrick. And I would like to make it more clear.The version  number is also included.</p>
<p>All six params are simply concatenated by the order listed below, but be careful about the datatype(int or string) and the byte-order(little-endian or big-endian), the correct order is:</p>
<ol>
<li>Version</li>
<li>Previous Block Header Hash</li>
<li>Merkle root</li>
<li>Timestamp</li>
<li>Bits (Difficulty)</li>
<li>Nonce</li>
</ol>
<p>Here is a Python program with comments to validate a block hash with height 2</p>
<pre><code>from hashlib import sha256
import struct

# below are the params of Block height 2
ver = 1  # 4 Bytes (32 bits)
prev_block = &quot;00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048&quot;  # 32 Bytes (256 bits)
mrkl_root =  &quot;9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a7a1cde251e54ccfdd5&quot;  # 32 Bytes (256 bits)
time = 1231469744   # 4 Bytes (32 bits)
bits = 486604799    # 4 Bytes (32 bits)
nonce = 1639830024  # 4 Bytes (32 bits)

def deal_str(big_endian):
    return bytes.fromhex(big_endian)[::-1]    # [::-1] will reverse the bytes to little-endian

def deal_int(n):
    # &lt; means &quot;Little-endian&quot;
    # L means &quot;unsigned long&quot;
    return struct.pack(&quot;&lt;L&quot;, n)

header_bin = deal_int(ver) + deal_str(prev_block) + deal_str(mrkl_root) + deal_int(time) + deal_int(bits) + deal_int(nonce)

hash = sha256(sha256(header_bin).digest()).digest()

cur_block = hash[::-1].hex()   #reverse back to big-endian,  which is block hash.
print(cur_block)  # block hash of block height 2: 000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd

</code></pre>

CC BY-SA 4.0
2021-08-03T11:15:53.823Z
2021-08-03T11:15:53.823Z
mikemelon
<p>Exactly which items from a block are included in the data that is hashed to produce the block hash?</p>

<block><hash><block-header>
What data is hashed to produce the block hash?
RedGrittyBrick
# What data is hashed to produce the block hash?

(Asked by: username_0 on 2021-07-30)

Exactly which items from a block are included in the data that is hashed to produce the block hash?

**Tags:** block, hash, block-header


---

## 2 answers

### Answer by username_0 on 2021-07-30. Score: 2

The data to be hashed is

* Version
* Previous Block Header Hash
* Merkle Root
* Timestamp
* Difficulty
* Nonce

The [difficulty](https://en.bitcoin.it/wiki/Difficulty), which is loosely an inverse of the target, is in a compact representation known as "bits".

The [Merkle root](https://bitcoin.stackexchange.com/q/10479/13866) is a hash produced in a tree-wise fashion from hashes of transaction data.

Most of the data must be in little-endian form.

A good explanation in detail can be found at

* <https://medium.com/hackergirl/how-to-calculate-the-hash-of-a-block-in-bitcoin-8f6aebb0dc6d> which provides an example in Python.
* <https://dlt-repo.net/how-to-calculate-a-bitcoin-block-hash-manually/>

---

Related:

* [What is the difference between Root Hash and Block Hash?](https://bitcoin.stackexchange.com/q/70336/13866)
* [Block Hashing Algorithm](https://en.bitcoin.it/wiki/Block_hashing_algorithm) (wiki)
* [Block Chain](https://developer.bitcoin.org/reference/block_chain.html) (developer reference)


---

### Answer by username_1 on 2021-08-03. Score: 1

Agree with @username_0. And I would like to make it more clear.The version number is also included.

All six params are simply concatenated by the order listed below, but be careful about the datatype(int or string) and the byte-order(little-endian or big-endian), the correct order is:

1. Version
2. Previous Block Header Hash
3. Merkle root
4. Timestamp
5. Bits (Difficulty)
6. Nonce

Here is a Python program with comments to validate a block hash with height 2

```
from hashlib import sha256
import struct

# below are the params of Block height 2
ver = 1  # 4 Bytes (32 bits)
prev_block = "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"  # 32 Bytes (256 bits)
mrkl_root =  "9b0fc92260312ce44e74ef369f5c66bbb85848f2eddd5a7a1cde251e54ccfdd5"  # 32 Bytes (256 bits)
time = 1231469744   # 4 Bytes (32 bits)
bits = 486604799    # 4 Bytes (32 bits)
nonce = 1639830024  # 4 Bytes (32 bits)

def deal_str(big_endian):
    return bytes.fromhex(big_endian)[::-1]    # [::-1] will reverse the bytes to little-endian

def deal_int(n):
    # < means "Little-endian"
    # L means "unsigned long"
    return struct.pack("<L", n)

header_bin = deal_int(ver) + deal_str(prev_block) + deal_str(mrkl_root) + deal_int(time) + deal_int(bits) + deal_int(nonce)

hash = sha256(sha256(header_bin).digest()).digest()

cur_block = hash[::-1].hex()   #reverse back to big-endian,  which is block hash.
print(cur_block)  # block hash of block height 2: 000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd

```


---

