首页 > 代码库 > ubuntu下安装 openssl&&编译运行测试代码

ubuntu下安装 openssl&&编译运行测试代码

检查是否已安装 openssl:

sudo apt-get install openssl

如果已安装执行以下操作:
sudo apt-get install libssl-dev
sudo apt-get install libssl0.9.8

Ubuntu 下安装 GTK+ 开发库
sudo apt-get install libgtk2.0-dev

 

代码:

#include <stdio.h>#include <string.h>#include <openssl/hmac.h> int main() {    // The secret key for hashing    char key[] = "f5f48fc13505425891fb429f99d66171";	char user[] = "Fazio";    char api_key[] = "4894a1a7b78e44b6a449c5299b1afc24";	char nonce[] = "000000";	char *data = http://www.mamicode.com/malloc(strlen(user)+strlen(api_key)+strlen(nonce)+1);"%s \n",data);	       unsigned char* result;    unsigned int len = 65;     result = (unsigned char*)malloc(sizeof(char) * len);     HMAC_CTX ctx;    HMAC_CTX_init(&ctx);     HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha256(), NULL);    HMAC_Update(&ctx, (unsigned char*)data, strlen(data));    HMAC_Final(&ctx, result, &len);    HMAC_CTX_cleanup(&ctx);     printf("HMAC digest: ");	int i;     for (i = 0; i != len; i++)        printf("%02x", (unsigned int)result[i]);     printf("\n");     free(result);     return 0;}

编译: gcc -o test  test.c -lssl  -lcrypto

运行结果:

Fazio4894a1a7b78e44b6a449c5299b1afc24000000
HMAC digest: 6764f0c105dcfa9fc143aea534232c297ab2ba4e362fd87af5eb0a26b270e0eb

ubuntu下安装 openssl&&编译运行测试代码