<p>The problem is that you are hashing the string of the hexadecimal representation of the bytes of the block header, not the bytes of the block header itself. You need to hash the bytes of the block header itself.</p>

<p>You need to set everything as a byte array, not a string. So you would have something like this for the entire block header all together:</p>

<pre><code>Byte[] block_header = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd,
                       0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76,
                       0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
                       0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab,
                       0x5f, 0x49, 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c};
</code></pre>

<p>It's possible convert the string hex into matrix byte in C++ with this code</p>

<pre><code>  vector&lt;unsigned char&gt; ToHexIntoVectorByte(string &amp;hexData)
{

  vector&lt;unsigned char&gt; *bytes = new vector&lt;unsigned char&gt;();
  for(unsigned i = 0; i &lt; hexData.length(); i += 2)
  {
      string byteSubString = hexData.substr(i, 2);
      unsigned char byte = (unsigned char)strtol(byteSubString.c_str(), NULL, 16);
      bytes-&gt;push_back(byte);

  }
  return *bytes;
}
</code></pre>

<p>for extract the matrix in the unsigned char using <code>vectorresult.data()</code> and if using the <a href="https://github.com/nayuki/Bitcoin-Cryptography-Library" rel="nofollow noreferrer">Bitcoin-Cryptography-Library</a> the final code is</p>

<pre><code>vector&lt;unsigned char&gt; vectorByte = ToHexIntoVectorByte(stringHex);

Sha256Hash shaHash = Sha256::getDoubleHash(vectorByte.data(), vectorByte.size());
</code></pre>

CC BY-SA 4.0
2019-03-25T08:38:42.987Z
Do you recommend some C ++ functions that convert the string into bytes?
vincenzopalazzo
CC BY-SA 4.0
2019-03-24T23:33:26.647Z
2019-12-12T15:35:01.170Z
2019-12-12T15:35:01.170Z
Ava Chow
<p>I have same problem whit create a double sha256 of the transactions and blocks with C++.</p>

<p>The hash block it is created with the concatenation of these data,</p>

<p><strong>version</strong> + <strong>previusBlock</strong> + <strong>merkleRoot</strong> + <strong>timeStamp</strong> + <strong>bit</strong> + <strong>nonce</strong></p>

<p><em>In the hash block is not necessary to include the all data raw transaction because is are include in the merkleRoot, right?</em></p>

<p>For create, hash transaction is create whit the concatenation of these data,
<strong>versionRawTransaction</strong> + <strong>output of the transaction input</strong> + <strong>scriptSing</strong> + <strong>sequences transaction input</strong> + <strong>cAmmount transaction output</strong> + <strong>publicKeyScript</strong> + <strong>lockTime raw transaction</strong></p>

<p>Or are include the data for saving the variable structure? in this method </p>

<p><strong>numberRawTransactions</strong> + <strong>versionRawTransaction</strong> + <strong>numbarTransactionInput</strong> + <strong>output of the transaction input</strong> + <strong>scriptLenght</strong> + <strong>scriptSing</strong> + <strong>sequences transaction input</strong> + <strong>numbarTransactionOutput</strong> + <strong>cAmmount transaction output</strong> + <strong>publicKeyScriptLenght</strong> + <strong>publicKeyScript</strong> + <strong>lockTime raw transaction</strong></p>

<p>Now I've tried to apply this theory in my C ++ program but can't get the desired hash. Can you help me?</p>

<p>This is the my test code</p>

<pre><code>    TEST(hash_test, first_test_double_sha_bit_genesi_block_bitcoin_crypolibrary)
{
    //Init logger
    FLAGS_minloglevel = 0;
    FLAGS_logtostderr = true;
    google::SetLogDestination(google::GLOG_WARNING,  "/home/vincenzo/Github/SpyCblock/test/log/first_test_sha_not_on_bitcoin_protocolo.log");

    string version = "01000000";
    string previusBlock = "0000000000000000000000000000000000000000000000000000000000000000";
    string merkleRoot = "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a";
    string timeStamp = "29ab5f49";
    string bit = "ffff001d";
    string nonce = "1dac2b7c";

    stringstream stream;

    stream  &lt;&lt; version &lt;&lt; previusBlock &lt;&lt; merkleRoot &lt;&lt; timeStamp &lt;&lt; bit &lt;&lt; nonce;


    Bytes byte = asciiBytes(stream.str().c_str());
    Sha256Hash shaHash = Sha256::getDoubleHash(byte.data(), byte.size());

    LOG(INFO) &lt;&lt; "The hash genesi block converting with double sha256: " &lt;&lt; shaHash.ToString();
    ASSERT_EQ(shaHash.ToString(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");


}
</code></pre>

<p>but I get this result <code>46f4d3f53251d05ab58a3f8925ce7023f2115dc38468ec78521c7d0e1dc7bd19</code></p>

<p>Since I know practically nothing in cryptography in practice, I relied on this library where I use this type of data here that makes the double sha256 <a href="https://github.com/nayuki/Bitcoin-Cryptography-Library/blob/master/cpp/Sha256.hpp" rel="nofollow noreferrer">Find here</a></p>

<p>Sorry for my terrible English but I'm learning</p>

<transactions><bitcoin-core><sha256><block-hash>
Create a Double sha256 block and trandaction
vincenzopalazzo
# Create a Double sha256 block and trandaction

(Asked by: username_0 on 2019-03-24)

I have same problem whit create a double sha256 of the transactions and blocks with C++.

The hash block it is created with the concatenation of these data,

**version** + **previusBlock** + **merkleRoot** + **timeStamp** + **bit** + **nonce**

*In the hash block is not necessary to include the all data raw transaction because is are include in the merkleRoot, right?*

For create, hash transaction is create whit the concatenation of these data,
**versionRawTransaction** + **output of the transaction input** + **scriptSing** + **sequences transaction input** + **cAmmount transaction output** + **publicKeyScript** + **lockTime raw transaction**

Or are include the data for saving the variable structure? in this method

**numberRawTransactions** + **versionRawTransaction** + **numbarTransactionInput** + **output of the transaction input** + **scriptLenght** + **scriptSing** + **sequences transaction input** + **numbarTransactionOutput** + **cAmmount transaction output** + **publicKeyScriptLenght** + **publicKeyScript** + **lockTime raw transaction**

Now I've tried to apply this theory in my C ++ program but can't get the desired hash. Can you help me?

This is the my test code

```
    TEST(hash_test, first_test_double_sha_bit_genesi_block_bitcoin_crypolibrary)
{
    //Init logger
    FLAGS_minloglevel = 0;
    FLAGS_logtostderr = true;
    google::SetLogDestination(google::GLOG_WARNING,  "/home/vincenzo/Github/SpyCblock/test/log/first_test_sha_not_on_bitcoin_protocolo.log");

    string version = "01000000";
    string previusBlock = "0000000000000000000000000000000000000000000000000000000000000000";
    string merkleRoot = "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a";
    string timeStamp = "29ab5f49";
    string bit = "ffff001d";
    string nonce = "1dac2b7c";

    stringstream stream;

    stream  << version << previusBlock << merkleRoot << timeStamp << bit << nonce;

    Bytes byte = asciiBytes(stream.str().c_str());
    Sha256Hash shaHash = Sha256::getDoubleHash(byte.data(), byte.size());

    LOG(INFO) << "The hash genesi block converting with double sha256: " << shaHash.ToString();
    ASSERT_EQ(shaHash.ToString(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");

}

```

but I get this result `46f4d3f53251d05ab58a3f8925ce7023f2115dc38468ec78521c7d0e1dc7bd19`

Since I know practically nothing in cryptography in practice, I relied on this library where I use this type of data here that makes the double sha256 [Find here](https://github.com/nayuki/Bitcoin-Cryptography-Library/blob/master/cpp/Sha256.hpp)

Sorry for my terrible English but I'm learning

**Tags:** transactions, bitcoin-core, sha256, block-hash


---

## 1 answers

### Answer by username_1 on 2019-03-24. Score: 1 | Accepted answer

The problem is that you are hashing the string of the hexadecimal representation of the bytes of the block header, not the bytes of the block header itself. You need to hash the bytes of the block header itself.

You need to set everything as a byte array, not a string. So you would have something like this for the entire block header all together:

```
Byte[] block_header = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd,
                       0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76,
                       0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
                       0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab,
                       0x5f, 0x49, 0xff, 0xff, 0x00, 0x1d, 0x1d, 0xac, 0x2b, 0x7c};

```

It's possible convert the string hex into matrix byte in C++ with this code

```
  vector<unsigned char> ToHexIntoVectorByte(string &hexData)
{

  vector<unsigned char> *bytes = new vector<unsigned char>();
  for(unsigned i = 0; i < hexData.length(); i += 2)
  {
      string byteSubString = hexData.substr(i, 2);
      unsigned char byte = (unsigned char)strtol(byteSubString.c_str(), NULL, 16);
      bytes->push_back(byte);

  }
  return *bytes;
}

```

for extract the matrix in the unsigned char using `vectorresult.data()` and if using the [Bitcoin-Cryptography-Library](https://github.com/nayuki/Bitcoin-Cryptography-Library) the final code is

```
vector<unsigned char> vectorByte = ToHexIntoVectorByte(stringHex);

Sha256Hash shaHash = Sha256::getDoubleHash(vectorByte.data(), vectorByte.size());

```

> **Comments:**
> **username_0**: Do you recommend some C ++ functions that convert the string into bytes?  

---

