How to get block header hex?

1

I am trying to research hash functions and I want to be able to use bitcoin's mining outputs as hashes. I am curious about where the actual hex value of the data that generates that hash with a lot of zeroes is. So, this is what I want: the 80 byte input that gives a 32 bit output with a lot of zeros from sha256(sha256(x)).

Sunflower

Posted 2019-11-24T12:37:45.383

Reputation: 29

Answers

4

I am curious about where the actual hex value of the data that generates that hash with a lot of zeroes is.

It is a composite of values from the block - in brief, it is the concatenated binary (represented here in hex notation) little endian value of each of the fields:

version + previous_block_hash + merkle_root_hash + time + bits + nonce

of the block that is being mined.

These are contained in the "block header".

You have examples of Python code here: https://en.bitcoin.it/wiki/Block_hashing_algorithm , for example for block 125552:

>>> import hashlib
>>> header_hex = ("01000000" +
 "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
 "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
 "c7f5d74d" +
 "f2b9441a" +
 "42a14695")
>>> header_bin = header_hex.decode('hex')
>>> hash = hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
>>> hash.encode('hex_codec')
'1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000'
>>> hash[::-1].encode('hex_codec')
'00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d'

Note that the value is reversed at the last step in order to be represented in the big-endian format that we humans are used to.

So, this is what I want: the 80 byte input that gives a 32 bit output with a lot of zeros from sha256(sha256(x)).

You can check these 6 values for every block mined using any one of the many online block explorers available: for example here for the particular block of the previous example: https://blockexplorer.com/block/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d (you'll only have to convert the human time to unix time being careful with the time zone).

Here is a quick link to download blocks' headers in binary form (until May 2018) and start playing with real values. You can convert it to .csv if needed.

circulosmeos

Posted 2019-11-24T12:37:45.383

Reputation: 536

0

The first 80 bytes of each block are the block header.

You can get the block header for a given block hash by using the getblockheader RPC method.

You can get the block hash for a given block height by using the getblockhash RPC method.

Raghav Sood

Posted 2019-11-24T12:37:45.383

Reputation: 15 741