I would like to hash a simple string using MD5 in Xcode c++. I searched a lot but I couldn't find a tutorial. I have to #import <CommonCrypto/CommonDigest.h>
. Is that all? How can I call MD5 after that?
I have found this code but it gives me an error. How will I get my hashed value is it updated in the string variable?
unsigned char digest[16];
const char* string = "Hello World";
struct MD5Context context; **(error: variable has incomplete type
MD5Init(&context);
MD5Update(&context, string, strlen(string));
MD5Final(digest, &context);
I'm just using a simple command line application no headers inside just the basic main.cpp. I really appreciate any help!!!!
You're using the wrong API. I'm not sure where you're getting those from (they look like OpenSSL calls), but it should look like this:
#include <stdio.h>
#include <string.h>
#include <CommonCrypto/CommonDigest.h>
int main()
{
unsigned char digest[CC_MD5_DIGEST_LENGTH];
const char string[] = "Hello World";
CC_MD5_CTX context;
CC_MD5_Init(&context);
CC_MD5_Update(&context, string, (CC_LONG)strlen(string));
CC_MD5_Final(digest, &context);
for (size_t i=0; i<CC_MD5_DIGEST_LENGTH; ++i)
printf("%.2x", digest[i]);
fputc('\n', stdout);
return 0;
}
Output
b10a8db164e0754105b7a99be72e3fe5
Validated here.