<p>After a lot of research, I've come up with the following Javascript function that calculates the valid hash of any block provided you input the 6 required parameters that are officially used to generate the hash of a Bitcoin block header. I think it may help people understand how a block header is <strong>correctly hashed</strong>.</p>
<p>Parameters of this example are set with Bitcoin <a href="https://bitcoinexplorer.org/block-height/722460#JSON" rel="nofollow noreferrer">Block #722,460</a>.</p>
<pre class="lang-javascript prettyprint-override"><code>/**
 * @dev This function takes the version, timestamp, bits or nonce and returns
 * its representation as a hexadecimal string in little-endian byte order
 * 
 */
function toLittleEndianHex(value) {
  // Create a new 4-byte little-endian DataView with an ArrayBuffer
  const littleEndian = new DataView(new ArrayBuffer(4));
  // Set the value of the DataView to the version, timestamp, bits or nonce in little-endian byte order
  littleEndian.setUint32(0, value, true);
  // Convert the DataView into a Uint8Array
  const byteArray = new Uint8Array(littleEndian.buffer);
  // Convert each byte into a 2-character hexadecimal string, and push them into an array
  const hexArray = [];
  for (let i = byteArray.length - 1; i &gt;= 0; i--) {
    hexArray.push(byteArray[i].toString(16).padStart(2, &quot;0&quot;));
  }
  // Reverse the order of the hexadecimal strings in the array and join them into a single string
  return hexArray.reverse().join(&quot;&quot;);
}

/**
 * @dev This function takes a hexadecimal string and returns its 
 * representation as a Uint8Array
 * 
 */
function hexStringToUint8Array(hexString) {
  // Convert the hexadecimal string into an array of 2-character substrings, representing each byte
  const byteArray = hexString.match(/.{1,2}/g).map(byte =&gt; parseInt(byte, 16));
  // Create a new Uint8Array from the array of bytes
  return new Uint8Array(byteArray);
}

/**
 * @dev This function takes a hexadecimal string and returns its reverse
 * 
 */
function reverseHexString(hexString) {
  // Convert the hexadecimal string into an array of 2-character substrings, representing each byte
  // Reverse the order of the substrings and join them into a single string
  return hexString.match(/.{1,2}/g).reverse().join(&quot;&quot;);
}

/**
 * @dev This function takes an object representing block data, calculates 
 * its hash value, and logs it to the console
 * 
 */
async function calculateBlockHash(blockData) {
  // Destructure the block data object to extract its properties
  const { version, previousblockhash, merkleroot, time, bits, nonce } = blockData;
  // Convert each property to its hexadecimal representation in little-endian byte order
  const versionHex = toLittleEndianHex(version);
  console.log(&quot;versionHex:&quot;, versionHex);
  const previousblockhashHex = reverseHexString(previousblockhash);
  console.log(&quot;previousblockhashHex:&quot;, previousblockhashHex);
  const merklerootHex = reverseHexString(merkleroot);
  console.log(&quot;versionHex:&quot;, merklerootHex);
  const timeHex = toLittleEndianHex(time);
  console.log(&quot;timeHex:&quot;, timeHex);
  const bitsHex = toLittleEndianHex(bits);
  console.log(&quot;bitsHex:&quot;, bitsHex);
  const nonceHex = toLittleEndianHex(nonce);
  console.log(&quot;nonceHex:&quot;, nonceHex);
  // Concatenate the hexadecimal representations into a single string representing the block header
  const headerHex = `${versionHex}${previousblockhashHex}${merklerootHex}${timeHex}${bitsHex}${nonceHex}`;
  console.log(&quot;headerHex:&quot;, headerHex);
  // Convert the header hexadecimal string into a Uint8Array
  const headerBin = hexStringToUint8Array(headerHex);
  // Use the Web Crypto API to calculate the double-SHA256 hash of the block header
  const hashDigest1 = await crypto.subtle.digest(&quot;SHA-256&quot;, headerBin);
  const hashDigest2 = await crypto.subtle.digest(&quot;SHA-256&quot;, hashDigest1)
  // Convert the hash digest into a Uint8Array
  const hashBin = new Uint8Array(hashDigest2);
  // Convert the hash Uint8Array into a hexadecimal string with reversed byte order
  const hashHex = Array.from(hashBin, b =&gt; b.toString(16).padStart(2, &quot;0&quot;)).join(&quot;&quot;);
  const hashResult = hashHex.match(/.{1,2}/g).reverse().join(&quot;&quot;);
  // Log the hash result value to the console
  console.log(&quot;hashResult:&quot;, hashResult);
}

/**
 * @dev Data from Block #722,460
 * Verify at https://bitcoinexplorer.org/block-height/722460#JSON
 * 
 */
const blockData = {
  version: 536879108,
  previousblockhash: &quot;00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046&quot;,
  merkleroot: &quot;e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3&quot;,
  time: 1644403033,
  bits: 0x170a8bb4,
  nonce: 1693537958
};

// Call the calculateBlockHash function with the blockData object as its argument
await calculateBlockHash(blockData);
</code></pre>
<p><strong>Console Output:</strong></p>
<pre><code>versionHex: 04200020
previousblockhashHex: 461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000
versionHex: b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1
timeHex: 59990362
bitsHex: b48b0a17
nonceHex: a656f164
headerHex: 04200020461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e159990362b48b0a17a656f164
hashResult: 00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1
</code></pre>

CC BY-SA 4.0
2023-02-22T05:38:13.327Z
2023-02-22T15:31:31.990Z
2023-02-22T15:31:31.990Z
suchislife
<p>We need to concatenate these fields in correct format and order:</p>
<p><code>&quot;versionHex&quot;: &quot;20002004&quot;</code>, <code>&quot;previousblockhash&quot;: &quot;00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046&quot;</code>, <code>&quot;merkleroot&quot;: &quot;e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3&quot;</code>, <code>&quot;time&quot;: 1644403033</code>, <code>&quot;bits&quot;: &quot;170a8bb4&quot;</code>, <code>&quot;nonce&quot;: 1693537958</code>.</p>
<p>Problem here is that not all fields are given with the same encoding and are not in correct byte order.
The decimal numbers have to be converted to little endian hex, and other hex values need to have the bytes reversed.</p>
<p>Here's a little shell script that does the job:</p>
<pre><code>#!/bin/bash

# Input fields
versionHex=20002004
previousblockhash=00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046
merkleroot=e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3
time=1644403033
bits=170a8bb4
nonce=1693537958

# Function to convert a decimal number to hex big endian.
NumToHex () { echo &quot;obase=16;$1&quot; | bc | tr -d '\n'; }

# Function to change endianness, e.g. 01a2 becomes a201.
# Input must have even number of characters.
ReverseBytesHex () { echo -n &quot;$1&quot; | tac -rs '..'; }

# Function to calculate block hash from serialized block header.
BlockHash () { echo $1 -n | xxd -r -p | sha256sum | xxd -r -p | sha256sum | cut -b 1-64 | tr -d '\n' | tac -rs '..'; }

# Generate serialized block from input fields.
serializedBlock=$(ReverseBytesHex $versionHex)
serializedBlock=$serializedBlock$(ReverseBytesHex $previousblockhash)
serializedBlock=$serializedBlock$(ReverseBytesHex $merkleroot)
timeHex=$(NumToHex $time)
serializedBlock=$serializedBlock$(ReverseBytesHex $timeHex)
serializedBlock=$serializedBlock$(ReverseBytesHex $bits)
nonceHex=$(NumToHex $nonce)
serializedBlock=$serializedBlock$(ReverseBytesHex $nonceHex)

# Generate and print block's hash.
echo $(BlockHash $serializedBlock)
</code></pre>
<p>Output:</p>
<pre><code>00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1
</code></pre>
<p>For reference, please see the raw block header format below.</p>
<h2>Block Header Format</h2>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Field</th>
<th>Length</th>
<th>Format</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>version</td>
<td>4 bytes</td>
<td>unsigned integer<sup>(LE)</sup></td>
<td>The block format version.</td>
</tr>
<tr>
<td>previous block hash</td>
<td>32 bytes</td>
<td>block hash<sup>(LE)</sup></td>
<td>The hash of the block immediately preceding this block in the blockchain.</td>
</tr>
<tr>
<td>merkle root</td>
<td>32 bytes</td>
<td>merkle root<sup>(LE)</sup></td>
<td>The merkle tree root of the transactions in the block.</td>
</tr>
<tr>
<td>timestamp</td>
<td>4 bytes</td>
<td>unix timestamp<sup>(LE)</sup></td>
<td>The epoch timestamp of the block in seconds.</td>
</tr>
<tr>
<td>target</td>
<td>4 bytes</td>
<td>compressed target<sup>(LE)</sup></td>
<td>The target that the block hash must be below to be valid.  This value is determined by the timestamps of previously mined blocks.</td>
</tr>
<tr>
<td>nonce</td>
<td>4 bytes</td>
<td>bytes<sup>(LE)</sup></td>
<td>A random value that is repeatedly changed during block mining in order to achieve the block hash requirements.</td>
</tr>
</tbody>
</table>
</div>
CC BY-SA 4.0
2023-11-20T17:55:44.433Z
2023-11-20T17:55:44.433Z
bca-0353f40e
<p>I want to calculate/verify the hash for block #722,460.</p>
<p>Here is the JSON:</p>
<pre><code>  &quot;hash&quot;: &quot;00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1&quot;,
  &quot;confirmations&quot;: 9,
  &quot;height&quot;: 722460,
  &quot;version&quot;: 536879108,
  &quot;versionHex&quot;: &quot;20002004&quot;,
  &quot;merkleroot&quot;: &quot;e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3&quot;,
  &quot;time&quot;: 1644403033,
  &quot;mediantime&quot;: 1644398983,
  &quot;nonce&quot;: 1693537958,
  &quot;bits&quot;: &quot;170a8bb4&quot;,
  &quot;difficulty&quot;: 26690525287405.5,
  &quot;chainwork&quot;: &quot;000000000000000000000000000000000000000028664e27abbdcf002a673476&quot;,
  &quot;nTx&quot;: 3371,
  &quot;previousblockhash&quot;: &quot;00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046&quot;,
  &quot;nextblockhash&quot;: &quot;0000000000000000000a6bf48e80b7b873998e9b8e7c4ced6ec85d559bce72df&quot;,
  &quot;strippedsize&quot;: 821777,
  &quot;size&quot;: 1534464,
  &quot;weight&quot;: 3999795,
</code></pre>
<p>To calculate hash I need theese values (already have converted them):</p>
<pre><code>version: '04200020'
previousblockhash: '461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000'
merkleroot: 'b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1'
time: '3330404416'
bits: 'b48b0a17'
nonce: 'a656f164' 
</code></pre>
<p>Now I am trying to calculate the hash:</p>
<pre><code>import hashlib
from binascii import unhexlify, hexlify
header_hex = (&quot;04200020&quot; +
 &quot;461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000&quot; +
 &quot;b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1&quot; +
 &quot;3330404416&quot; +
 &quot;b48b0a17&quot; +
 &quot;a656f164&quot;)
header_bin = unhexlify(header_hex)
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
hexlify(hash[::-1]).decode(&quot;utf-8&quot;)
</code></pre>
<p>And all I get is:
<strong>b3e5b3485f11963cff6659fb48cfecc6e1cda271324d8b25b1af023205bb891b</strong></p>
<p>Instaed of:
<strong>00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1</strong></p>
<p>What am I doing wrong?</p>
<p>Thank you in advance</p>

CC BY-SA 4.0
2022-02-09T14:38:43.153Z
Your timestamp doesn't look like a 4-byte hexadecimal number.
Vojtěch Strnad
CC BY-SA 4.0
2022-02-10T11:26:15.160Z
It is hex 4- byte. I have tried different ways. Also have tried 87890362. But thank you for your comment!
Yuri
CC BY-SA 4.0
2022-02-10T11:41:20.523Z
Can't understand a thing! Just got block header in hex:
04200020461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e159990362b48b0a17a656f164
The timestamp in hex is 59990362, as you can find from the string. But if we convert it in decimal it takes 1503200098 and not 1644403033 if we get in the header in JSON.
Yuri
CC BY-SA 4.0
2022-02-10T15:42:32.440Z
59990362 is little-endian, you first have to convert it to big-endian (62039959) before you convert it to decimal.
Vojtěch Strnad
<hash><block-header><block-validity>
Calculate/Verify the Hash of a Bitcoin Block Header
Yuri
# Calculate/Verify the Hash of a Bitcoin Block Header

(Asked by: username_0 on 2022-02-09)

I want to calculate/verify the hash for block #722,460.

Here is the JSON:

```
  "hash": "00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1",
  "confirmations": 9,
  "height": 722460,
  "version": 536879108,
  "versionHex": "20002004",
  "merkleroot": "e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3",
  "time": 1644403033,
  "mediantime": 1644398983,
  "nonce": 1693537958,
  "bits": "170a8bb4",
  "difficulty": 26690525287405.5,
  "chainwork": "000000000000000000000000000000000000000028664e27abbdcf002a673476",
  "nTx": 3371,
  "previousblockhash": "00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046",
  "nextblockhash": "0000000000000000000a6bf48e80b7b873998e9b8e7c4ced6ec85d559bce72df",
  "strippedsize": 821777,
  "size": 1534464,
  "weight": 3999795,

```

To calculate hash I need theese values (already have converted them):

```
version: '04200020'
previousblockhash: '461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000'
merkleroot: 'b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1'
time: '3330404416'
bits: 'b48b0a17'
nonce: 'a656f164' 

```

Now I am trying to calculate the hash:

```
import hashlib
from binascii import unhexlify, hexlify
header_hex = ("04200020" +
 "461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000" +
 "b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1" +
 "3330404416" +
 "b48b0a17" +
 "a656f164")
header_bin = unhexlify(header_hex)
hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
hexlify(hash[::-1]).decode("utf-8")

```

And all I get is:
**b3e5b3485f11963cff6659fb48cfecc6e1cda271324d8b25b1af023205bb891b**

Instaed of:
**00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1**

What am I doing wrong?

Thank you in advance

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

> **Comments:**
> **username_1**: Your timestamp doesn't look like a 4-byte hexadecimal number.  
> **username_0**: It is hex 4- byte. I have tried different ways. Also have tried 87890362. But thank you for your comment!  
> **username_0**: Can't understand a thing! Just got block header in hex: 04200020461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e159990362b48b0a17a656f164 The timestamp in hex is 59990362, as you can find from the string. But if we convert it in decimal it takes 1503200098 and not 1644403033 if we get in the header in JSON.  
> **username_1**: 59990362 is little-endian, you first have to convert it to big-endian (62039959) before you convert it to decimal.  

---

## 2 answers

### Answer by username_2 on 2023-02-22. Score: 1

After a lot of research, I've come up with the following Javascript function that calculates the valid hash of any block provided you input the 6 required parameters that are officially used to generate the hash of a Bitcoin block header. I think it may help people understand how a block header is **correctly hashed**.

Parameters of this example are set with Bitcoin [Block #722,460](https://bitcoinexplorer.org/block-height/722460#JSON).

```javascript
/**
 * @dev This function takes the version, timestamp, bits or nonce and returns
 * its representation as a hexadecimal string in little-endian byte order
 * 
 */
function toLittleEndianHex(value) {
  // Create a new 4-byte little-endian DataView with an ArrayBuffer
  const littleEndian = new DataView(new ArrayBuffer(4));
  // Set the value of the DataView to the version, timestamp, bits or nonce in little-endian byte order
  littleEndian.setUint32(0, value, true);
  // Convert the DataView into a Uint8Array
  const byteArray = new Uint8Array(littleEndian.buffer);
  // Convert each byte into a 2-character hexadecimal string, and push them into an array
  const hexArray = [];
  for (let i = byteArray.length - 1; i >= 0; i--) {
    hexArray.push(byteArray[i].toString(16).padStart(2, "0"));
  }
  // Reverse the order of the hexadecimal strings in the array and join them into a single string
  return hexArray.reverse().join("");
}

/**
 * @dev This function takes a hexadecimal string and returns its 
 * representation as a Uint8Array
 * 
 */
function hexStringToUint8Array(hexString) {
  // Convert the hexadecimal string into an array of 2-character substrings, representing each byte
  const byteArray = hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
  // Create a new Uint8Array from the array of bytes
  return new Uint8Array(byteArray);
}

/**
 * @dev This function takes a hexadecimal string and returns its reverse
 * 
 */
function reverseHexString(hexString) {
  // Convert the hexadecimal string into an array of 2-character substrings, representing each byte
  // Reverse the order of the substrings and join them into a single string
  return hexString.match(/.{1,2}/g).reverse().join("");
}

/**
 * @dev This function takes an object representing block data, calculates 
 * its hash value, and logs it to the console
 * 
 */
async function calculateBlockHash(blockData) {
  // Destructure the block data object to extract its properties
  const { version, previousblockhash, merkleroot, time, bits, nonce } = blockData;
  // Convert each property to its hexadecimal representation in little-endian byte order
  const versionHex = toLittleEndianHex(version);
  console.log("versionHex:", versionHex);
  const previousblockhashHex = reverseHexString(previousblockhash);
  console.log("previousblockhashHex:", previousblockhashHex);
  const merklerootHex = reverseHexString(merkleroot);
  console.log("versionHex:", merklerootHex);
  const timeHex = toLittleEndianHex(time);
  console.log("timeHex:", timeHex);
  const bitsHex = toLittleEndianHex(bits);
  console.log("bitsHex:", bitsHex);
  const nonceHex = toLittleEndianHex(nonce);
  console.log("nonceHex:", nonceHex);
  // Concatenate the hexadecimal representations into a single string representing the block header
  const headerHex = `${versionHex}${previousblockhashHex}${merklerootHex}${timeHex}${bitsHex}${nonceHex}`;
  console.log("headerHex:", headerHex);
  // Convert the header hexadecimal string into a Uint8Array
  const headerBin = hexStringToUint8Array(headerHex);
  // Use the Web Crypto API to calculate the double-SHA256 hash of the block header
  const hashDigest1 = await crypto.subtle.digest("SHA-256", headerBin);
  const hashDigest2 = await crypto.subtle.digest("SHA-256", hashDigest1)
  // Convert the hash digest into a Uint8Array
  const hashBin = new Uint8Array(hashDigest2);
  // Convert the hash Uint8Array into a hexadecimal string with reversed byte order
  const hashHex = Array.from(hashBin, b => b.toString(16).padStart(2, "0")).join("");
  const hashResult = hashHex.match(/.{1,2}/g).reverse().join("");
  // Log the hash result value to the console
  console.log("hashResult:", hashResult);
}

/**
 * @dev Data from Block #722,460
 * Verify at https://bitcoinexplorer.org/block-height/722460#JSON
 * 
 */
const blockData = {
  version: 536879108,
  previousblockhash: "00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046",
  merkleroot: "e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3",
  time: 1644403033,
  bits: 0x170a8bb4,
  nonce: 1693537958
};

// Call the calculateBlockHash function with the blockData object as its argument
await calculateBlockHash(blockData);

```

**Console Output:**

```
versionHex: 04200020
previousblockhashHex: 461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000
versionHex: b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e1
timeHex: 59990362
bitsHex: b48b0a17
nonceHex: a656f164
headerHex: 04200020461059508fa9181f4ae83e7d56310d9622c10c114ad509000000000000000000b354496cf9b0fad2f64d69563ea3e01fb0560f54e759e88e12d898603e57e0e159990362b48b0a17a656f164
hashResult: 00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1

```


---

### Answer by username_3 on 2023-11-20. Score: 0

We need to concatenate these fields in correct format and order:

`"versionHex": "20002004"`, `"previousblockhash": "00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046"`, `"merkleroot": "e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3"`, `"time": 1644403033`, `"bits": "170a8bb4"`, `"nonce": 1693537958`.

Problem here is that not all fields are given with the same encoding and are not in correct byte order.
The decimal numbers have to be converted to little endian hex, and other hex values need to have the bytes reversed.

Here's a little shell script that does the job:

```
#!/bin/bash

# Input fields
versionHex=20002004
previousblockhash=00000000000000000009d54a110cc122960d31567d3ee84a1f18a98f50591046
merkleroot=e1e0573e6098d8128ee859e7540f56b01fe0a33e56694df6d2fab0f96c4954b3
time=1644403033
bits=170a8bb4
nonce=1693537958

# Function to convert a decimal number to hex big endian.
NumToHex () { echo "obase=16;$1" | bc | tr -d '\n'; }

# Function to change endianness, e.g. 01a2 becomes a201.
# Input must have even number of characters.
ReverseBytesHex () { echo -n "$1" | tac -rs '..'; }

# Function to calculate block hash from serialized block header.
BlockHash () { echo $1 -n | xxd -r -p | sha256sum | xxd -r -p | sha256sum | cut -b 1-64 | tr -d '\n' | tac -rs '..'; }

# Generate serialized block from input fields.
serializedBlock=$(ReverseBytesHex $versionHex)
serializedBlock=$serializedBlock$(ReverseBytesHex $previousblockhash)
serializedBlock=$serializedBlock$(ReverseBytesHex $merkleroot)
timeHex=$(NumToHex $time)
serializedBlock=$serializedBlock$(ReverseBytesHex $timeHex)
serializedBlock=$serializedBlock$(ReverseBytesHex $bits)
nonceHex=$(NumToHex $nonce)
serializedBlock=$serializedBlock$(ReverseBytesHex $nonceHex)

# Generate and print block's hash.
echo $(BlockHash $serializedBlock)

```

Output:

```
00000000000000000002b73f69e81b8b5e98dff0f2b7632fcb83c050c3b099a1

```

For reference, please see the raw block header format below.

## Block Header Format

| Field | Length | Format | Description |
| --- | --- | --- | --- |
| version | 4 bytes | unsigned integer(LE) | The block format version. |
| previous block hash | 32 bytes | block hash(LE) | The hash of the block immediately preceding this block in the blockchain. |
| merkle root | 32 bytes | merkle root(LE) | The merkle tree root of the transactions in the block. |
| timestamp | 4 bytes | unix timestamp(LE) | The epoch timestamp of the block in seconds. |
| target | 4 bytes | compressed target(LE) | The target that the block hash must be below to be valid. This value is determined by the timestamps of previously mined blocks. |
| nonce | 4 bytes | bytes(LE) | A random value that is repeatedly changed during block mining in order to achieve the block hash requirements. |


---

