<pre><code>#include &lt;openssl/sha.h&gt;

int main()
{
    const char * sin = "00000020" // version
        "164a1e4a7f34b96b0e201dcc6a623c63fe3874696e4875000000000000000000" // prev hash
        "49de8b4f4bfa9fc890d3d28a93156a111f891dc680090cd497b58a7d5c2b09cf" // merkle
        "2f62345a"      // timestamp
        "edb00018"      // bits
        "ffdfd257";
    string xxs = Helper::decode16(sin);

    unsigned char md[32];
    SHA256_CTX ctx;
    SHA256_Init(&amp;ctx);
    SHA256_Update(&amp;ctx, xxs.data(), xxs.length());
    SHA256_Final(md, &amp;ctx);

    SHA256_Init(&amp;ctx);
    SHA256_Update(&amp;ctx, md, 32);
    SHA256_Final(md, &amp;ctx);

    const char * tbl = "0123456789abcdef";
    char outs[64 + 1] = { 0 };
    for (int i = 0; i &lt; 32; ++i)
    {
        outs[2 * i + 0] = tbl[md[31-i] &gt;&gt; 4];
        outs[2 * i + 1] = tbl[md[31 - i] &amp; 0xf];
    }
    printf("%s\n", outs);

    return 0;
}
</code></pre>

<p>my code uses openssl. passed.</p>

CC BY-SA 3.0
2017-12-27T15:05:04.957Z
2017-12-27T15:05:04.957Z
swigger
<p>Seems to me that you are hashing the whole block instead of header only. Take my code (in C++). </p>

<pre><code>void test ( )
{
  const MyByteArray header ( QByteArray::fromHex (
     "00000020" // version
     "164a1e4a7f34b96b0e201dcc6a623c63fe3874696e4875000000000000000000" // prev hash
     "49de8b4f4bfa9fc890d3d28a93156a111f891dc680090cd497b58a7d5c2b09cf" // merkle
     "2f62345a"      // timestamp
     "edb00018"      // bits
     "ffdfd257" ) ); // nonce
  const MyKey32 key ( header.sha256d ( ) );
  qDebug ( ) &lt;&lt; key.toString ( );
}
</code></pre>

<p>and the output is:</p>

<p><strong>000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da</strong></p>

CC BY-SA 3.0
2017-12-16T21:23:42.893Z
What format does the input data need to be? I'm trying to manually plug the data into a sha256 calculator, and not getting the correct result. I know it is a double sha256 but I must be making a mistake somewhere
chytrik
CC BY-SA 3.0
2017-12-17T04:48:10.027Z
input is raw binary data 80 bytes length
amaclin
CC BY-SA 3.0
2017-12-17T07:58:43.700Z
I thank you amaclin, I did not know that you only need to hash the header. But can someone maybe write such a short program in Javascript or Java or Python? (I can't read C++, I don't know what a QByteArray ist, what MyKey32 ist, what the proramm is really doing.)
hardfork
CC BY-SA 3.0
2017-12-16T13:10:59.010Z
2017-12-16T13:10:59.010Z
amaclin
<p>Let's pick an example bitcoin block: #499583</p>

<p>This is the block: <a href="https://insight.bitpay.com/api/rawblock/000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da" rel="nofollow noreferrer">https://insight.bitpay.com/api/rawblock/000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da</a></p>

<p>It starts with <code>00000020164a1e4a7f34b96b0e201d....</code></p>

<p>I'm trying to hash it so that the hash is <code>000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da</code> again. This is my Javascript-Code:</p>

<pre><code>var Bitcoin = require('bitcoinjs-lib');
var request = require('request');
var crypto = require('crypto');

function getRawBlock(blockHash) {
  return new Promise((resolve, reject) =&gt; {
    request('https://insight.bitpay.com/api/rawblock/' + blockHash, // hitting an insight API to get the full block
      (error, response, body) =&gt; {
        try {
          var block = JSON.parse(body); // result is in JSON
          resolve(block.rawblock)
        } catch (error) {
          reject(error)
        }
      })
  })
}

getRawBlock('000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da')
  .then((rawBlock) =&gt; {
    var hash = crypto.createHash('sha256').update(rawBlock).digest('hex');
     console.log(hash);
  })
})
</code></pre>

<p>But the result is <code>484cd10be70dbc7615dd9a71b1f91375b100715d9b2a0ecc6a05b9d247a8cda9</code>.
What am I doing wrong? The result should be <code>000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da</code>. Do I hash something in the wrong format?</p>

<blockchain><block><hash><sha256><javascript>
How can I check the hash of a bitcoin block?
hardfork
# How can I check the hash of a bitcoin block?

(Asked by: username_0 on 2017-12-16)

Let's pick an example bitcoin block: #499583

This is the block: <https://insight.bitpay.com/api/rawblock/000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da>

It starts with `00000020164a1e4a7f34b96b0e201d....`

I'm trying to hash it so that the hash is `000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da` again. This is my Javascript-Code:

```
var Bitcoin = require('bitcoinjs-lib');
var request = require('request');
var crypto = require('crypto');

function getRawBlock(blockHash) {
  return new Promise((resolve, reject) => {
    request('https://insight.bitpay.com/api/rawblock/' + blockHash, // hitting an insight API to get the full block
      (error, response, body) => {
        try {
          var block = JSON.parse(body); // result is in JSON
          resolve(block.rawblock)
        } catch (error) {
          reject(error)
        }
      })
  })
}

getRawBlock('000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da')
  .then((rawBlock) => {
    var hash = crypto.createHash('sha256').update(rawBlock).digest('hex');
     console.log(hash);
  })
})

```

But the result is `484cd10be70dbc7615dd9a71b1f91375b100715d9b2a0ecc6a05b9d247a8cda9`.
What am I doing wrong? The result should be `000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da`. Do I hash something in the wrong format?

**Tags:** blockchain, block, hash, sha256, javascript


---

## 2 answers

### Answer by username_2 on 2017-12-16. Score: 4 | Accepted answer

Seems to me that you are hashing the whole block instead of header only. Take my code (in C++).

```
void test ( )
{
  const MyByteArray header ( QByteArray::fromHex (
     "00000020" // version
     "164a1e4a7f34b96b0e201dcc6a623c63fe3874696e4875000000000000000000" // prev hash
     "49de8b4f4bfa9fc890d3d28a93156a111f891dc680090cd497b58a7d5c2b09cf" // merkle
     "2f62345a"      // timestamp
     "edb00018"      // bits
     "ffdfd257" ) ); // nonce
  const MyKey32 key ( header.sha256d ( ) );
  qDebug ( ) << key.toString ( );
}

```

and the output is:

**000000000000000000677c4077da7c9f01dde5f332ba2fbff962ee699714d5da**

> **Comments:**
> **username_3**: What format does the input data need to be? I'm trying to manually plug the data into a sha256 calculator, and not getting the correct result. I know it is a double sha256 but I must be making a mistake somewhere  
> **username_2**: input is raw binary data 80 bytes length  
> **username_0**: I thank you username_2, I did not know that you only need to hash the header. But can someone maybe write such a short program in Javascript or Java or Python? (I can't read C++, I don't know what a QByteArray ist, what MyKey32 ist, what the proramm is really doing.)  

---

### Answer by username_1 on 2017-12-27. Score: 1

```
#include <openssl/sha.h>

int main()
{
    const char * sin = "00000020" // version
        "164a1e4a7f34b96b0e201dcc6a623c63fe3874696e4875000000000000000000" // prev hash
        "49de8b4f4bfa9fc890d3d28a93156a111f891dc680090cd497b58a7d5c2b09cf" // merkle
        "2f62345a"      // timestamp
        "edb00018"      // bits
        "ffdfd257";
    string xxs = Helper::decode16(sin);

    unsigned char md[32];
    SHA256_CTX ctx;
    SHA256_Init(&ctx);
    SHA256_Update(&ctx, xxs.data(), xxs.length());
    SHA256_Final(md, &ctx);

    SHA256_Init(&ctx);
    SHA256_Update(&ctx, md, 32);
    SHA256_Final(md, &ctx);

    const char * tbl = "0123456789abcdef";
    char outs[64 + 1] = { 0 };
    for (int i = 0; i < 32; ++i)
    {
        outs[2 * i + 0] = tbl[md[31-i] >> 4];
        outs[2 * i + 1] = tbl[md[31 - i] & 0xf];
    }
    printf("%s\n", outs);

    return 0;
}

```

my code uses openssl. passed.


---

