<p>The problem is that you are hashing the string of hex characters instead of the actual bytes that the hex string represents. You should be using an array instead with the bytes specified. The following should work:</p>

<pre><code>string sha256(char[] str, size_t len)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&amp;sha256);
  SHA256_Update(&amp;sha256, str, len);
  SHA256_Final(hash, &amp;sha256);
  stringstream ss;

  for(int i = 0; i &lt; SHA256_DIGEST_LENGTH; i++)
  {
    ss &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; (int)hash[i];
  }
return ss.str();
}

int main()
{
  char[] arr = {0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, 0x88, 0x7e, 0x5b, 0x23, 0x52};
  cout &lt;&lt; endl;
  cout &lt;&lt; sha256(arr, 32) &lt;&lt; endl;
  return 0;
}
</code></pre>

CC BY-SA 4.0
2018-07-05T11:53:14.090Z
I've tried the code above, and after correcting my own syntax errors and getting it to compile I still get a different hash digest than I was expecting. the message digest I have is:                                                                           cf2bafdb95b6354a89afdbc7b9c7d5bfb53d9ace57518ccc05c3bd493dbe4bb4
Ernest Kissiedu
CC BY-SA 4.0
2018-07-05T11:57:39.597Z
I found this resource online after doing some further research . http://www.fileformat.info/tool/hash.htm?hex=0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352        So it seems like if I do a Binary Hash I get the message digest I am looking for, so I think what I have to is find a way to do a binary conversion on the hex representation to get the message digest I am looking for. Thanks Again for your help.
Ernest Kissiedu
CC BY-SA 4.0
2018-07-04T23:05:54.550Z
2018-07-04T23:05:54.550Z
Ava Chow
<p>I need to write a script using C++ and the OPENSSL library that takes this: <code>0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352</code>
performs a SHA256 Hash using the sha256 function from the OpenSSL library and then returns <code>0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98</code> as the digest. </p>

<p>The issue I am having is that I get a different message digest after performing the sha256 hash function: <code>a9ce83de3a0ff3516b7c50cdd787e9f69f152f227d93c9512774231e7132e925</code>.</p>

<p>I've been stuck on this for about 2 weeks and from my research I have been told that my function is not doing the right conversion of my message before performing the Sha256 hash function. </p>

<h2>Code</h2>

<pre><code>string sha256(const string str)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&amp;sha256);
  SHA256_Update(&amp;sha256, str.c_str(), str.size());
  SHA256_Final(hash, &amp;sha256);
  stringstream ss;

  for(int i = 0; i &lt; SHA256_DIGEST_LENGTH; i++)
  {
    ss &lt;&lt; hex &lt;&lt; setw(2) &lt;&lt; setfill('0') &lt;&lt; (int)hash[i];
  }
return ss.str();
}

int main()
{

  cout &lt;&lt; endl;
  cout &lt;&lt; sha256("0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352") &lt;&lt; endl;
  return 0;
}
</code></pre>

CC BY-SA 4.0
2018-07-04T16:31:31.703Z
could you specify to which 9 steps you are referring to? From a book or a guide somewhere online?
0xb10c
CC BY-SA 4.0
2018-07-04T21:17:56.200Z
here is a good cross reference page with the 9 steps: http://gobittest.appspot.com/Address - when you deal with hashings in bitcoin, you must make sure that you hash the hex data, not the (ASCII) strings. I don't know ow to do this in C++, I had the problem in my shell codings with openssl...
pebwindkraft
<sha256><openssl>
Creating a Bitcoin address using the 9 steps. Stuck on stage 2
Ernest Kissiedu
# Creating a Bitcoin address using the 9 steps. Stuck on stage 2

(Asked by: username_0 on 2018-07-04)

I need to write a script using C++ and the OPENSSL library that takes this: `0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352`
performs a SHA256 Hash using the sha256 function from the OpenSSL library and then returns `0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98` as the digest.

The issue I am having is that I get a different message digest after performing the sha256 hash function: `a9ce83de3a0ff3516b7c50cdd787e9f69f152f227d93c9512774231e7132e925`.

I've been stuck on this for about 2 weeks and from my research I have been told that my function is not doing the right conversion of my message before performing the Sha256 hash function.

## Code

```
string sha256(const string str)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&sha256);
  SHA256_Update(&sha256, str.c_str(), str.size());
  SHA256_Final(hash, &sha256);
  stringstream ss;

  for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  {
    ss << hex << setw(2) << setfill('0') << (int)hash[i];
  }
return ss.str();
}

int main()
{

  cout << endl;
  cout << sha256("0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352") << endl;
  return 0;
}

```

**Tags:** sha256, openssl

> **Comments:**
> **username_1**: could you specify to which 9 steps you are referring to? From a book or a guide somewhere online?  
> **username_2**: here is a good cross reference page with the 9 steps: http://gobittest.appspot.com/Address - when you deal with hashings in bitcoin, you must make sure that you hash the hex data, not the (ASCII) strings. I don't know ow to do this in C++, I had the problem in my shell codings with openssl...  

---

## 1 answers

### Answer by username_3 on 2018-07-04. Score: 2

The problem is that you are hashing the string of hex characters instead of the actual bytes that the hex string represents. You should be using an array instead with the bytes specified. The following should work:

```
string sha256(char[] str, size_t len)
{
  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&sha256);
  SHA256_Update(&sha256, str, len);
  SHA256_Final(hash, &sha256);
  stringstream ss;

  for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  {
    ss << hex << setw(2) << setfill('0') << (int)hash[i];
  }
return ss.str();
}

int main()
{
  char[] arr = {0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, 0x88, 0x7e, 0x5b, 0x23, 0x52};
  cout << endl;
  cout << sha256(arr, 32) << endl;
  return 0;
}

```

> **Comments:**
> **username_0**: I've tried the code above, and after correcting my own syntax errors and getting it to compile I still get a different hash digest than I was expecting. the message digest I have is:                                                                           cf2bafdb95b6354a89afdbc7b9c7d5bfb53d9ace57518ccc05c3bd493dbe4bb4  
> **username_0**: I found this resource online after doing some further research . http://www.fileformat.info/tool/hash.htm?hex=0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352        So it seems like if I do a Binary Hash I get the message digest I am looking for, so I think what I have to is find a way to do a binary conversion on the hex representation to get the message digest I am looking for. Thanks Again for your help.  

---

