openssl linux下编译
git checkout OpenSSL_1_1_1两下tab健,确定最新版本为OpenSSL_1_1_1q
git clone https://github.com/openssl/openssl.git
git checkout OpenSSL_1_1_1q
输出到当前目录下的output目录
./config --prefix=`pwd`/output
make -j 6
make install
当前目录下的output目录就是最终生成的文件,复制output/include和output/lib/libssl.so,output/lib/libcrypto.so 到本地代码目录
openssl hmac加密,使用dlopen防止和系统的so干扰
#include <stdio.h>
#include <string.h>
#include <dlfcn.h>
#include <openssl/hmac.h>
#include <openssl/opensslv.h>
#include <openssl/crypto.h> // unsigned long SSLeay(void)
int main(int argc,void **argv) {
//key value
const char key[] = "key-abcd";
//data in hex, /or you can init with text
unsigned char data[] = "12345";
unsigned char result[20] = {0};
unsigned int resultlen = 20;
void *handleCrypto = dlopen("./libcrypto.so", RTLD_LAZY | RTLD_DEEPBIND);
if(handleCrypto == NULL)
{
printf("dlopen libcrypto.so error\n");
return -1;
}
typedef const char *(*FUN_OpenSSL_version)(int type);
FUN_OpenSSL_version pOpenSSL_version = (FUN_OpenSSL_version)dlsym(handleCrypto, "OpenSSL_version");
if(pOpenSSL_version == NULL)
{
printf("dlsym OpenSSL_version error\n");
return -1;
}
void *handleSsl = dlopen("./libssl.so", RTLD_LAZY | RTLD_DEEPBIND);
if(handleSsl == NULL)
{
printf("dlopen libssl.so error\n");
return -1;
}
typedef HMAC_CTX * (*FUN_HMAC_CTX_new)(void);
FUN_HMAC_CTX_new pHMAC_CTX_new = (FUN_HMAC_CTX_new)dlsym(handleSsl, "HMAC_CTX_new");
if(pHMAC_CTX_new == NULL)
{
printf("dlsym HMAC_CTX_new error\n");
return -1;
}
typedef EVP_MD *(*FUN_EVP_sha1)(void);
FUN_EVP_sha1 pEVP_sha1 = (FUN_EVP_sha1)dlsym(handleSsl, "EVP_sha1");
if(pEVP_sha1 == NULL)
{
printf("dlsym HMAC_Init_ex error\n");
return -1;
}
typedef int (*FUN_HMAC_Init_ex)(HMAC_CTX *ctx, const void *key, int len,
const EVP_MD *md, ENGINE *impl);
FUN_HMAC_Init_ex pHMAC_Init_ex = (FUN_HMAC_Init_ex)dlsym(handleSsl, "HMAC_Init_ex");
if(pHMAC_Init_ex == NULL)
{
printf("dlsym HMAC_Init_ex error\n");
return -1;
}
typedef int (*FUN_HMAC_Update)(HMAC_CTX *ctx, const unsigned char *data,
size_t len);
FUN_HMAC_Update pHMAC_Update = (FUN_HMAC_Update)dlsym(handleSsl, "HMAC_Update");
if(pHMAC_Update == NULL)
{
printf("dlsym HMAC_Update error\n");
return -1;
}
typedef int (*FUN_HMAC_Final)(HMAC_CTX *ctx, unsigned char *md,
unsigned int *len);
FUN_HMAC_Final pHMAC_Final = (FUN_HMAC_Final)dlsym(handleSsl, "HMAC_Final");
if(pHMAC_Final == NULL)
{
printf("dlsym HMAC_Final error\n");
return -1;
}
typedef void (*FUN_HMAC_CTX_free)(HMAC_CTX *ctx);
FUN_HMAC_CTX_free pHMAC_CTX_free = (FUN_HMAC_CTX_free)dlsym(handleSsl, "HMAC_CTX_free");
if(pHMAC_CTX_free == NULL)
{
printf("dlsym HMAC_CTX_free error\n");
return -1;
}
// 1.1.1m版本
printf("%s \n",(*pOpenSSL_version)(0));
HMAC_CTX *ctx = pHMAC_CTX_new();
pHMAC_Init_ex(ctx, key, strlen(key), pEVP_sha1(), NULL);
pHMAC_Update(ctx, data, strlen((char *)data));
pHMAC_Final(ctx, result, &resultlen);
pHMAC_CTX_free(ctx);
dlclose(handleSsl);
dlclose(handleCrypto);
for (int i = 0; i < resultlen; i++)
printf("%02x", result[i]);
printf("\n");
return 0;
}