UTF-8 String and Hash calculation

I use ATL and Visual C++ to write my program. I have need to POST certain form data to a web site. Since this is a banking/financial web site, they require the hash-sum of the entire string I am posting. They have provide me a simple PHP that perform the login to their web site. When I execute the PHP code, it is able to connect and login to the web site. But when I try to do the same through the C++ application, I am unable to login to the same web site. The web site is complaining that certain form variable is not found in the POST data.

After a couple of experiments with the PHP, and another sample application in C#, I came to the conclusion that problem area is the hash-calculation. The hash-sum produced by the PHP is not the same as what is being produced by my C++ code. The relevant part of the PHP code is given below.

function shaHash512($string)
return hash('sha512', $string);

In the C++ program I use OpenSSL to calculate the sha512 hash. The C++ code is given below.

string CreatePostData(string m_RequestXML)
bool continue_flag = true;
string temp_string;
char separator = '&';
char separator2 = '.';

//Remove any new lines for testing
int pos;
pos = m_RequestXML.find('\n');
while (pos != m_RequestXML.npos)
m_RequestXML.erase(pos, 1);
pos = m_RequestXML.find('\n');

unsigned char digest[SHA512_DIGEST_LENGTH];

temp_string = m_RequestXML;

SHA512(reinterpret_cast<const unsigned char*>(temp_string.c_str()), temp_string.length(), (unsigned char*)&digest);

temp_string = ByteToString(digest, SHA512_DIGEST_LENGTH);

return (temp_string);

The function ByreToString is simply converting the byte data to Hex readable characters.

The OpenSSL's SHA512 function arguments are byte[] which is the string, the length of the string, and the last one is again a byte[] in which the hash will be returned.

When I use simple strings, like temp_string = "Hello World", then hash calculated by the C++ program and the PHP program are the same. But the temp_string contain complex XML string, the hash produced is considerably differ. The XML is UTF-8.

So, I assume that it has something to do with the UTF-8 string conversions. In the sample C# program it is using the Encoding built-in function to transform the C# string into a byte array required to be passed to the hash function (built-in .Net).

I do not know what should be done in C++ so that I get the same hash-sum produced by the PHP code. Any help will be appreciated.

I use Visual studio on 64-bit Windows-8. But the target "exe" is 32 bit.

Thanks in Advance.
Last edited on
Topic archived. No new replies allowed.