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?

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.