0

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!!!!

Tom
  • 13
  • 3
  • `#include` would probably make you C++ compiler happier, as would using the correct function names and types. The CC stuff all starts with CC_. – WhozCraig Jan 12 '17 at 01:25
  • Ah thank you yes that one was in obj C my bad. – Tom Jan 12 '17 at 01:33
  • Thank you I have found this manual and updated as you suggested. But I still get the error for the variable. Do I have to include additional libraries? https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/CC_MD5.3cc.html#//apple_ref/doc/man/3cc/CC_MD5 – Tom Jan 12 '17 at 01:41
  • Apple dropped supported OpenSSL awhile back. it's still there, but they want you using CC instead (can't really blame them). The CC libs have been around for awhile now, and their use is similar to the code in ObjC. If you still have an error, update your question with an **addendum** that includes the new code, and more importantly, the exact error message. You code should ideally look like one of the answers below. – WhozCraig Jan 12 '17 at 01:43
  • Thank you so much you are great! It works!! :) – Tom Jan 12 '17 at 01:48

2 Answers2

2

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.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
1

There is a one-shot version:

#include <CommonCrypto/CommonDigest.h>

unsigned char digest[16];
const char* string = "Hello World";

CC_MD5(string, (CC_LONG)strlen(string), digest);

You will need to include the Security.framework (or at lease the applicable library file).

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thank you for answering! I also tried your solution but it gives an error message: "Implicit conversion loses integer precision unsigned long, to unsigned int. And it points at strlen(string) in the last line. If I add (CC_LONG)strlen(string) it fixes the error. But if I try to print the digest it gives me some crazy things like: \261 \215\261d\...... I have included these: #include #include #include #include . – Tom Jan 12 '17 at 02:33
  • Right, some casting was needed, fixed. MD5 returns data and most data values are not ASCII, if you need ASCII output either encode as hexadecimal or Base64. – zaph Jan 12 '17 at 02:56
  • Yes I would need Hex. How can tell directly to the MD5 fn to output hex for me as you suggest? Normally I would just try to convert the output but I don't know from what I'm converting from. Thank you for your help! – Tom Jan 12 '17 at 03:11
  • If I print it out like WhozCraig does it works just fine! I just can't figure out how I could store it in a variable in hex, nothing seems to work:( – Tom Jan 12 '17 at 12:48
  • How do you plan to use it? I generally need it in data form to pass to another method. – zaph Jan 12 '17 at 14:26
  • I have to break a toy project for a lecture and I need to compare the hex output from md5 to another hex that I store in some variable. I have found your answer on a similar topic but that is in swift. Swift 2.0 solution would be perfect for me in a c++ equivalent version. I tried to use sprintf(var, "%02x", digest[i]); but it only gives me back every second character but it's close. http://stackoverflow.com/questions/32163848/how-to-convert-string-to-md5-hash-using-ios-swift# – Tom Jan 12 '17 at 15:34