1

Tried to follow the example in SO example, but when compiling the following, got error:

$ gcc te.c
te.c: In function ‘main’:
te.c:10:17: error: storage size of ‘context’ isn’t known

Here is the code:

#include <string.h>
#include <stdio.h>
#include <openssl/md5.h>
//#include <md5.h>


int main(int argc, char *argv[]) {
    unsigned char digest[16];
    const char* string = "Hello World";
    struct MD5_CTX context;
    MD5Init(&context);
    MD5Update(&context, string, strlen(string));
    MD5Final(digest, &context);
    int i;
    for (i=0; i<16; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

By the way, my PC is running ubuntu 12.04 desktop. My gcc version is 4.7.3 and here is the version of libssl-dev

dpkg -l libssl-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
ii  libssl-dev     1.0.1-4ubuntu5 SSL development libraries, header files and

Any ideas?

UPDATE1

Thanks to Sourav Ghosh who pointed out that in the above, the struct in struct MD5_CTX context should be removed. Turned out the function name should be changed too, for example MD5Init to MD5_Init

This is working code:

#include <string.h>
#include <stdio.h>
#include <openssl/md5.h>
//#include <md5.h>


int main(int argc, char *argv[]) {
    unsigned char digest[16];
    const char* string = "Hello World";
    MD5_CTX context;
    MD5_Init(&context);
    MD5_Update(&context, string, strlen(string));
    MD5_Final(digest, &context);
    int i;
    for (i=0; i<16; i++) {
        printf("%02x", digest[i]);
    }
    printf("\n");
    return 0;
}

To compile it, one needs to use gcc te.c -lssl -lcrypto. Thanks to an SO answer for this too!

Community
  • 1
  • 1
packetie
  • 4,839
  • 8
  • 37
  • 72

1 Answers1

3

I think, (and as I can see) MD5_CTX is already a typedef to a struct. You don't need to write struct MD5_CTX context;.

Change it to MD5_CTX context; and it should work.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yes, you are right. I just realized that too. Will accept your answer in a few minutes since SO is preventing me from doing it now. By the way, the functions should be `MD5_Init`, `MD5_Update`, `MD5_Final`, but linker still can't find it when I ran `gcc te.c -lssl`. Any ideas? – packetie Jun 01 '15 at 13:47
  • @codingFun That brings down to the same point as my earlier comment, did you provide the path to the library? – Sourav Ghosh Jun 01 '15 at 13:53
  • yes, I think the default should work since if it was able to find the libssl.a (-lssl option in the command line). Otherwise, the linker will complain it can't find the library. – packetie Jun 01 '15 at 13:57
  • @codingFun Can you try providing the path to the library explicitly by using the `-L` option? I don't see any other reason as of now. – Sourav Ghosh Jun 01 '15 at 14:00
  • I see the problem: missed crypto . This works: `gcc te.c -lssl -lcrypto`. Thanks. – packetie Jun 01 '15 at 14:01
  • @codingFun I was not aware of the required libraries. So, cant guess. You're welcome, BTW. – Sourav Ghosh Jun 01 '15 at 14:04