I'm trying to use OpenSSL to compute sha1 hash from a c program. I am compiling with clang on Mac OS X Yosemite with an Intel i7 (so 64 bit).
The relevant piece of code is roughly like so:
#include <openssl/evp.h>
...
unsigned char outHash[20];
hash("SHA1","abcd", 20, outHash);
The thing is, when using the "hash" function from openssl/evp.h
, compiling with clang yields the following error:
Undefined symbols for architecture x86_64:
"_hash", referenced from:
_getRandomSHA1 in main-68ccd6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So it looks like OpenSSL is not found by the linker (the "hash" function is unidentified). Any ideas on how to fix this?
EDIT:
It turns out that I was trying to use a function that does not exist ("hash") - sorry for misleading you about that.
However I'm still having quite the same problem: including openssl/evp.h
does not seem to work.
This is the hash function that I am using, it uses evp to perform sha1 encoding:
unsigned int hash(const char *mode, const char* dataToHash, size_t dataSize, unsigned char* outHashed) {
unsigned int md_len = -1;
const EVP_MD *md = EVP_get_digestbyname(mode);
if(NULL != md) {
EVP_MD_CTX mdctx;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, dataToHash, dataSize);
EVP_DigestFinal_ex(&mdctx, outHashed, &md_len);
EVP_MD_CTX_cleanup(&mdctx);
}
return md_len;
}
And then I call:
hash("SHA1","abcd", 20, outHash);
I am compiling same as before, this is my compile command (pretty simple):
clang main.c
And I'm getting the following error from the linker:
Undefined symbols for architecture x86_64:
"_EVP_DigestFinal_ex", referenced from:
_hash in main-935849.o
"_EVP_DigestInit_ex", referenced from:
_hash in main-935849.o
"_EVP_DigestUpdate", referenced from:
_hash in main-935849.o
"_EVP_MD_CTX_cleanup", referenced from:
_hash in main-935849.o
"_EVP_MD_CTX_init", referenced from:
_hash in main-935849.o
"_EVP_get_digestbyname", referenced from:
_hash in main-935849.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)