How do I hash header block with C++

0
75


So im making an attempt to hash a header in C++ and I’m having some bother.
I’ve tried completely different libraries (cryptopp, openssl) can not seem to hash the header accurately.

That is the block which I’m testing: https://www.blockchain.com/btc/block/669892

header.model = 1073676288; // 3ff0000
header.previous_block = "000000000000000000069237bbafea14ba06f7bdc5e192aa38875ca3fc0d0cd9";
header.tx_merkle_root = "b0a2b9b8a0063e5b6818a8725ac567977805c77b6bba11f03a0c33a0cdd866ad";
header.timestamp = 1612900436; // 6022e854
header.bits = "170d21b9";
header.nonce = 2792790281; // a6769909

I’ve a category/operate which can format them right into a hex string, which i get as: 0000ff3fd90c0dfca35c8738aa92e1c5bdf706ba14eaafbb379206000000000000000000ad66d8cda0330c3af011ba6b7bc705789767c55a72a818685b3e06a0b8b9a2b054e82260b9210d17099976a6

And the anticipated last hash of 000000000000000000064ec839564cc03166184f0a404d82cad9c655f714d886

I then use this hash operate, and much like get the ultimate computed hash SHA256(SHA256(blockheader))

static std::string SHA256(std::string information, int measurement)
{
  // convert hex string to char
  unsigned char tmp;
  Crypto::hexStringToCharBinary(&tmp, &information, measurement);

  unsigned char hash[SHA256_DIGEST_LENGTH];
  SHA256_CTX sha256;
  SHA256_Init(&sha256);
  SHA256_Update(&sha256, &tmp, measurement);
  SHA256_Final(hash, &sha256);
  std::stringstream ss;
  for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
  {
      ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
  }
  std::cout << "digest: " << ss.str() << std::endl;
  return ss.str();
}

I do know that I’m supposed to offer the sha256 the bits and never the string of hex, as a result of it really works on a bit foundation, however I dont know the way to do this. I’m not one of the best at byte manipulation in C++, or hashing,
does anybody know the way to do that good and straightforward, if that is attainable?

LEAVE A REPLY

Please enter your comment!
Please enter your name here