How to create a md5 hash of a string in C?

前端 未结 6 831
独厮守ぢ
独厮守ぢ 2020-12-07 19:06

I\'ve found some md5 code that consists of the following prototypes...

I\'ve been trying to find out where I have to put the string I want to hash, what functions I

6条回答
  •  半阙折子戏
    2020-12-07 19:38

    I don't know this particular library, but I've used very similar calls. So this is my best guess:

    unsigned char digest[16];
    const char* string = "Hello World";
    struct MD5Context context;
    MD5Init(&context);
    MD5Update(&context, string, strlen(string));
    MD5Final(digest, &context);
    

    This will give you back an integer representation of the hash. You can then turn this into a hex representation if you want to pass it around as a string.

    char md5string[33];
    for(int i = 0; i < 16; ++i)
        sprintf(&md5string[i*2], "%02x", (unsigned int)digest[i]);
    

提交回复
热议问题