1

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;
}
  • could you specify to which 9 steps you are referring to? From a book or a guide somewhere online? – 0xb10c Jul 4 '18 at 16:31
  • here is a good cross reference page with the 9 steps: 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 Jul 4 '18 at 21:17
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;
}
  • 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 Jul 5 '18 at 11:53
  • I found this resource online after doing some further research . fileformat.info/tool/… 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 Jul 5 '18 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.