I need to successfully generate a hash value based on what i feel so far has been described as a hexadecimal filtered sha256 implementation... or something like that. I've been here , here , here, here and here ... amongst many other places.
Example Case:
Using "abc" as the input string,
AnyHash.com with the 'Hex' checkbox ticked produces a hash - 15f27c6b8828e62cd3d09e8b4de5230844facfc88f1b9c34129cab7706f2f561
. And when 15f27c6b8828e62cd3d09e.......
is fed as the input it produces the hash - 42a5aa8cd08811f43428c42f76279a5a92e12b3e3f7080b87804a05135773487
.
Following the comment by dave_thompson_085 i've been trying to make it work. The sha256 implementation in use is the one provided by Zedwood and below is my unfruitful struggle to recreate the hash output as generated by AnyHash.com.
// ----------------------- --------------------------- //
// ----------------------- --------------------------- //
// ----------------------- --------------------------- //
// COMMENT BY DAVE_THOMPSON_085
// That code is okay except the silly std::string wrapper is useless for bitcoin. Do something like :
// unsigned char hdr[80], hash[32];
// SHA256 ctx = SHA256();
/* fill in hdr */
// ctx.init();
// ctx.update(hdr,80);
// ctx.final(hash);
// ctx.init();
// ctx.update(hash,32);
// ctx.final(hash);
/* (reverse and) use results in hash, possibly converted to hex or whatever you need */
// ----------------------- --------------------------- //
// ----------------------- --------------------------- //
// ----------------------- --------------------------- //
std::string sha256(std::string input)
{
string kk;
// header_data is "160" in size because midstate optimization is not implemented
unsigned char header_data[160], hash[32];
char hash2[32], hash3[32];
char buf[2*32+1];
buf[2*32] = 0;
// input = GetBinaryStringFromHexString(input); // convert input data to binary format
init();
update((unsigned char*)input.c_str(), input.length());
final(hash);
// Iterate and store values from hash[] to hash2[]
for (int i = 0; i < 32; i++)
hash2[i] = hash[i];
// kk = final2(hash); // MODIFIED FUNCTION THAT RETURNS A STRING
// //kk = char_ToString(hash2); // Converts char array to string variable
kk = GetBinaryStringFromHexString(char_ToString(hash2)); // convert data to binary format
input = kk; // Store data in kk to input
init();
update((unsigned char*)input.c_str(), input.length());
final((unsigned char *)hash3); // Modified as char* to unsigned char* conversion is not allowed
for (int i = 0; i < 32; i++)
sprintf(buf+i*2, "%02x", hash3[i]); // ORIGINAL : sprintf(buf+i*2, "%02x", digest[i])
return std::string(buf); // Return buf as string instead of char
}
int main(int argc, char const *argv[])
{
std::string data = "abc";
std::cout << "sha256(data) : " << sha256(data) << "\n";
std::cout << "sha256(sha256(data)) : " << sha256(sha256(data)) << "\n";
return 0;
}
// Input : abc
// Expecting first hash output from sha256 ( with hex conversion ) to be :
// 15f27c6b8828e62cd3d09e8b4de5230844facfc88f1b9c34129cab7706f2f561 - single sha
// And second output to be :
// 42a5aa8cd08811f43428c42f76279a5a92e12b3e3f7080b87804a05135773487 - double sha
// As generated byanyhash.com
It's always the obvious implementations that are sometimes the hardest to implement. - in my opinion.
The same hash generator with the 'Hex' ticked successfully generates the correct hash of block 100000 existing in the bitcoin blockchain. Block 100,000 hashed by AnyHash.com
Block header 0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710
I tried getting the code from bitcoin source code hahahahahahahahahahahaha
....... need i say more?
With so many headers included and dependencies beyond what is needed, just thinking about it was a nightmare. So please, if anyone knows how to actually make this code work... please assist a fellow programmer..
Lord knows all things bitcoin make most programmers look like a novice. A great technology nonetheless!
Below are the other functions used(in the event it has something wrong with it) Thanks in advance!
sing std::hex;
string GetBinaryStringFromHexString(string hexData);
// std::string char_ToString(char data[]);
std::string char_ToString(char data[])
{
std::string stringData = ""; //declare string variable
// int size = sizeof(data)/sizeof(*data);
int size = strlen(data); //get amount of characters in char array
for (int i = 0; i < size; i++) // for elements in char array
stringData = data; // store elements in string variable "stringData"
return stringData; // return stringData
}
string GetBinaryStringFromHexString(string hexData)
{
string binaryResult = "";
for (int i = 0; i < hexData.length(); ++i)
{
switch (hexData[i])
{
case '0':
binaryResult.append("0000");
break;
case '1':
binaryResult.append("0001");
break;
case '2':
binaryResult.append("0010");
break;
case '3':
binaryResult.append("0011");
break;
case '4':
binaryResult.append("0100");
break;
case '5':
binaryResult.append("0101");
break;
case '6':
binaryResult.append("0110");
break;
case '7':
binaryResult.append("0111");
break;
case '8':
binaryResult.append("1000");
break;
case '9':
binaryResult.append("1001");
break;
case 'a':
binaryResult.append("1010");
break;
case 'b':
binaryResult.append("1011");
break;
case 'c':
binaryResult.append("1100");
break;
case 'd':
binaryResult.append("1101");
break;
case 'e':
binaryResult.append("1110");
break;
case 'f':
binaryResult.append("1111");
break;
}
}
return binaryResult;
}