I am not able to compile the following code. I run it in a MACOSX with the following command in the terminal:
$gcc filename.c -lssl
I understand that MACOS already has an inbuilt openssl (which i can call as a command in the terminal). But I am not sure if i am linking it to the library in the correct fashion.
#include <stdio.h>
#include <openssl/evp.h>
main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;
OpenSSL_add_all_digests();
if(!argv[1]) {
printf("Usage: mdtest digestname\n");
exit(1);
}
md = EVP_get_digestbyname(argv[1]);
if(!md) {
printf("Unknown message digest %s\n", argv[1]);
exit(1);
}
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}
My error is:
Undefined symbols:
“_EVP_MD_CTX_cleanup”, referenced from: _main in ccfZG7WJ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I am surprised by the error since it shows “Undefined Symbols” instead of “Header file not found”. Can you please help me debug this issue.
Thanks!
OpenSSL has 2 libraries:
libsslandlibcrypto. Include libcrypto, too.(You actually don’t need
-lsslat all for this program, but it doesn’t hurt to include it)libssl is for making actual SSL or TLS connections, like when connecting to a web server with https. It also exports
SSL_load_error_strings, which is useful even when working only with the cryptography library.libcrypto is the cryptography library. It has encryption algorithms, key and certificate generation and verification functions, message authentication algorithms, and so on.