<p>Your padding is correct, but you don't need to pad.</p>
<p>It appears that the SHA256 implementation you're using already performs the padding itself (demonstrated by the fact that it works correctly when presented with the unpadded block header as input). Thus, if you instead pad it manually, the effectively computed hash is the result of performing the padding operation twice.</p>
<p>Padding necessarily must not be a idempotent operation (= applying it a second time cannot be a no-op), or it would result in trivial collisions: just any string as well as its manually-padded version would give the same result.</p>

CC BY-SA 4.0
2021-08-17T13:25:18.980Z
2021-08-17T13:25:18.980Z
Pieter Wuille
<p>For any bitcoin block we combine various Header fields to create a string which is an input to a 2 pass SHA algorithm. The resultant hash must match with the Hash in the Block header for the Block to be valid.</p>
<p>To test this logic as well as SHA algorithm, I used the header of Block number 695877 (<a href="https://blockchain.info/rawblock/695877?format=json" rel="nofollow noreferrer">https://blockchain.info/rawblock/695877?format=json</a>) to create the Input string <strong>04008020546c359986812644420e453113e209afeaaeeb316f3a07000000000000000000b8fa13b3fca087c1456daac626ab9b8a47eae821a326f17e0ffffc15433df709b0f718610b1812175a5c9544</strong>.</p>
<p>Now when I apply the 2 pass SHA algorithm the resultant hash matches the hash in the Block (ignoring the Endian part for the time being).</p>
<p>Calculated Hash: <strong>629ef98d63e12f6b01476419a5a71efa8814dea40aec09000000000000000000</strong></p>
<p>Block hash: <strong>00000000000000000009ec0aa4de1488fa1ea7a5196447016b2fe1638df99e62</strong></p>
<p>But when I pad the header manually before passing it to 2 pass SHA algorithm as follows:
<strong>04008020546c359986812644420e453113e209afeaaeeb316f3a07000000000000000000b8fa13b3fca087c1456daac626ab9b8a47eae821a326f17e0ffffc15433df709b0f718610b1812175a5c9544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280</strong>
This is resulting in an incorrect hash.</p>
<p>I am failing to see what is wrong in the padding. Can someone please point it out?</p>

<sha256><block-header>
How to pad Bitcoin Header in the SHA Algorithm Passes?
J.Doe
# How to pad Bitcoin Header in the SHA Algorithm Passes?

(Asked by: username_0 on 2021-08-17)

For any bitcoin block we combine various Header fields to create a string which is an input to a 2 pass SHA algorithm. The resultant hash must match with the Hash in the Block header for the Block to be valid.

To test this logic as well as SHA algorithm, I used the header of Block number 695877 (<https://blockchain.info/rawblock/695877?format=json>) to create the Input string **04008020546c359986812644420e453113e209afeaaeeb316f3a07000000000000000000b8fa13b3fca087c1456daac626ab9b8a47eae821a326f17e0ffffc15433df709b0f718610b1812175a5c9544**.

Now when I apply the 2 pass SHA algorithm the resultant hash matches the hash in the Block (ignoring the Endian part for the time being).

Calculated Hash: **629ef98d63e12f6b01476419a5a71efa8814dea40aec09000000000000000000**

Block hash: **00000000000000000009ec0aa4de1488fa1ea7a5196447016b2fe1638df99e62**

But when I pad the header manually before passing it to 2 pass SHA algorithm as follows:
**04008020546c359986812644420e453113e209afeaaeeb316f3a07000000000000000000b8fa13b3fca087c1456daac626ab9b8a47eae821a326f17e0ffffc15433df709b0f718610b1812175a5c9544800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280**
This is resulting in an incorrect hash.

I am failing to see what is wrong in the padding. Can someone please point it out?

**Tags:** sha256, block-header


---

## 1 answers

### Answer by username_1 on 2021-08-17. Score: 2 | Accepted answer

Your padding is correct, but you don't need to pad.

It appears that the SHA256 implementation you're using already performs the padding itself (demonstrated by the fact that it works correctly when presented with the unpadded block header as input). Thus, if you instead pad it manually, the effectively computed hash is the result of performing the padding operation twice.

Padding necessarily must not be a idempotent operation (= applying it a second time cannot be a no-op), or it would result in trivial collisions: just any string as well as its manually-padded version would give the same result.


---

