I have this code that show me correctly, the md5 of a string.
I prefer to return a string to the function, but I have some problem converting the values of md5 into my string.
This is the code:
string calculatemd5(string msg)
{
string result;
const char* test = msg.c_str();
int i;
MD5_CTX md5;
MD5_Init (&md5);
MD5_Update (&md5, (const unsigned char *) test, msg.length());
unsigned char buffer_md5[16];
MD5_Final ( buffer_md5, &md5);
printf("Input: %s", test);
printf("\nMD5: ");
for (i=0;i<16;i++){
printf ("%02x", buffer_md5[i]);
result[i]=buffer_md5[i];
}
std::cout <<"\nResult:"<< result[i]<<endl;
return result;
}
For example result[i] is a strange ascii char like this: .
How can is possible solve this problem?
replace
with
notice that when you print out result, print
result, notresult[i]to get whole string.if you put the
buffer_md5[i]value directly in result then you may get problems since a string may not have an embedded 0 (if there is one).