Bitcoin Forum
May 05, 2024, 04:33:14 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to calculate the Hash value of a Bitcoin block?Use example of Genesis block!  (Read 1313 times)
fiero (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
December 06, 2016, 04:46:33 AM
 #1

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
There are several different types of Bitcoin clients. The most secure are full nodes like Bitcoin Core, but full nodes are more resource-heavy, and they must do a lengthy initial syncing process. As a result, lightweight clients with somewhat less security are commonly used.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
December 06, 2016, 05:24:53 AM
Last edit: December 06, 2016, 05:35:13 AM by Coding Enthusiast
Merited by ABCbits (4)
 #2

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

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
fiero (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
December 06, 2016, 07:24:03 PM
 #3

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);
Coding Enthusiast
Legendary
*
Offline Offline

Activity: 1039
Merit: 2783


Bitcoin and C♯ Enthusiast


View Profile WWW
December 07, 2016, 05:21:03 AM
Merited by ABCbits (2)
 #4

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

Projects List+Suggestion box
Donate: 1Q9s or bc1q
|
|
|
FinderOuter(0.19.1)Ann-git
Denovo(0.7.0)Ann-git
Bitcoin.Net(0.26.0)Ann-git
|
|
|
BitcoinTransactionTool(0.11.0)Ann-git
WatchOnlyBitcoinWallet(3.2.1)Ann-git
SharpPusher(0.12.0)Ann-git
fiero (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
December 07, 2016, 05:39:56 AM
 #5

Perfect, thanks Smiley
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!