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 = malloc(strlen(user)+strlen(api_key)+strlen(nonce)+1);
	strcpy(data,user);
	strcat(data,api_key);
	strcat(data,nonce);
	printf("%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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。