I'm trying to use the hash algorithms provided by the openssl library. I have openssl and libssl-dev installed. The version is 1.1.0f. I try to run the example code of the openssl.org site:
#include <stdio.h>
#include <openssl/evp.h>
int 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;
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);
}
mdctx = EVP_MD_CTX_new();
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_free(mdctx);
printf("Digest is: ");
for (i = 0; i < md_len; i++)
printf("%02x", md_value[i]);
printf("\n");
exit(0);
}
I try to compile this with:
gcc digest_example.c -lcrypto -lssl
And the compiler gives the error:
digest_example.c:(.text+0xbc): undefined reference to `EVP_MD_CTX_new'
digest_example.c:(.text+0x138): undefined reference to `EVP_MD_CTX_free'
collect2: error: ld returned 1 exit status
And honestly, I'm clueless. I installed and reinstalled OpenSSL 2 times from the website by compiling it. Additionally, all the other commands make no problem. Just these two. Do I have to use other libraries when linking?
Thanks for your help.