hmac-sha1加密算法C源码示例

HMAC: Hash-based Message Authentication Code,即基于Hash的消息鉴别码 
 
 

在各大开放平台大行其道的互联网开发潮流中,调用各平台的API接口过程中,无一例外都会用到计算签名值(sig值)。而在各种计算签名的方法中,经常被采用的就是HMAC-SHA1,现对HMAC-SHA1做一个简单的介绍:

HMAC,散列消息鉴别码,基于密钥的Hash算法认证协议。

实现原理为:

利用已经公开的Hash函数和私有的密钥,来生成固定长度的消息鉴别码;

SHA1、MD5等Hash算法是比较常用的不可逆Hash签名计算方法;

BASE64,将任意序列的8字节字符转换为人眼无法直接识别的符号编码的一种方法;

相关依赖库是openssl,安装方法如下:
apt-get install openssl   //Ubuntu 14.04
yum -y install openssl-devel    //centos 

下面提供两个源码示例:

例子一:

[cpp]  view plain  copy
  1. //gcc -g hmac_sha1_demo3.c -o hmac_sha1_demo3 -lcrypto -std=c99  
  2.   
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <openssl/hmac.h>  
  6.   
  7. int main()  
  8. {  
  9.     // The key to hash  
  10.     char key[] = "012345678";  
  11.   
  12.     // The data that we're going to hash using HMAC  
  13.     char data[] = "hello world";  
  14.   
  15.     unsigned char digest[EVP_MAX_MD_SIZE] = {'\0'};  
  16.     unsigned int digest_len = 0;  
  17.   
  18.     // Using sha1 hash engine here.  
  19.     // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc  
  20.     HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), digest, &digest_len);  
  21.     printf("%s, len %u\n", digest, digest_len);  
  22.   
  23.     // Be careful of the length of string with the choosen hash engine. SHA1 produces a 20-byte hash value which rendered as 40 characters.  
  24.     // Change the length accordingly with your choosen hash engine  
  25.     char mdString[41] = {'\0'};  
  26.     for(int i = 0; i < 20; i++)  
  27.          sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);  
  28.   
  29.     printf("HMAC digest: %s\n", mdString);  
  30.   
  31.     return 0;  
  32. }  

例子二:

[cpp]  view plain  copy
  1. //gcc -g hmac_sha1_demo2.c -o hmac_sha1_demo2 -lcrypto --std=c99  
  2.   
  3. #include <stdio.h>  
  4. #include <string.h>  
  5. #include <openssl/hmac.h>  
  6.   
  7. int main() {  
  8.     // The secret key for hashing  
  9.     const char key[] = "012345678";  
  10.   
  11.     // The data that we're going to hash  
  12.     char data[] = "hello world";  
  13.   
  14.     // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.  
  15.     // Change the length accordingly with your choosen hash engine.  
  16.     unsigned char* result;  
  17.     unsigned int len = 20;  
  18.   
  19.     result = (unsigned char*)malloc(sizeof(char) * len);  
  20.   
  21.     HMAC_CTX ctx;  
  22.     HMAC_CTX_init(&ctx);  
  23.   
  24.     // Using sha1 hash engine here.  
  25.     // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc  
  26.     HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);  
  27.     HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));  
  28.     HMAC_Final(&ctx, result, &len);  
  29.     HMAC_CTX_cleanup(&ctx);  
  30.   
  31.     printf("HMAC digest: ");  
  32.   
  33.     for (int i = 0; i != len; i++)  
  34.         printf("%02x", (unsigned int)result[i]);  
  35.   
  36.     printf("\n");  
  37.   
  38.     free(result);  
  39.   
  40.     return 0;  
  41. }  
运行截图:


参考文献:

[1].http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/  

猜你喜欢

转载自blog.csdn.net/weixin_37569048/article/details/80393129
今日推荐