Author

Topic: How to calculate the Hash value of a Bitcoin block?Use example of Genesis block! (Read 1314 times)

newbie
Activity: 4
Merit: 0
legendary
Activity: 1040
Merit: 2785
Bitcoin and C♯ Enthusiast
Because the SHA256 class ComputeHash() function only takes a byte array. And the code you quoted is the way to convert hex to byte array. (every two characters represent one byte)
eg.
int  :  hex
1    :  01
2    :  02
10  :  0A
...

This page has a Python example if that is the language you are comfortable with: https://en.bitcoin.it/wiki/Block_hashing_algorithm
newbie
Activity: 4
Merit: 0
Thanks a billion. This is extremely helpful. Infact BlockHeader is concatenated by taking the Little-Endian order of each field individually. I was not taking that into consideration.
Eg: I was taking the Nonce value as Hex(2083236893 ) = 7C2BAC1D when it should have been "1DAC2B7C".

From the final result (r) = 0100000000000000000000000000000000000000000000000000000000000000000000003ba3edf d7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b 7c
you have mentioned to perform SHA256 twice. So I am not sure why we need  conversion to "bytes" . Can you please explain. Thanks in advance   

Please explain why the code as below  is needed

         
Code:
   int NumberChars = s.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
legendary
Activity: 1040
Merit: 2785
Bitcoin and C♯ Enthusiast
You take the header of the block, then perform two Sha256 on it= Sha256(Sha256(Block_header) and then report the result in Little-Endian order.

Details:
https://blockexplorer.com/api/rawblock/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

Block_header= Version(4)+hashPrevBlock(32)+hashMerkleRoot(32)+Time(4)+Bits(4)+Nonce(4)
Code:
0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c

In C#
Code:
string s = "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c";
           
            int NumberChars = s.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);

            System.Security.Cryptography.SHA256 sha = new System.Security.Cryptography.SHA256Managed();

            byte[] b2 = sha.ComputeHash(bytes);
            byte[] b3 = sha.ComputeHash(b2);

            if (BitConverter.IsLittleEndian)
            {
                b3 = b3.Reverse().ToArray();
            }

            StringBuilder result = new StringBuilder();
            for (int i = 0; i < b3.Length; i++)
            {
                result.Append(b3[i].ToString("x2"));
            }
            string r = result.ToString();

r=
Code:
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
newbie
Activity: 4
Merit: 0
Hello All,

From blockchain.info – The Genesis Block info is at blockchain.info/block/000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

How is the hash value of “000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f” derived?
We can use online SHA-256 online tool like www.xorbin.com/tools/sha256-hash-calculator .
I understand that the value is calculated as hash value of the hash value of block header. But I want to know the specifics. The exact input that went into calculation. I tried a few ways as per my understanding but was unable to do so ?

Please help.
Thanks in advance
Jump to: