用OpenSSL 做HMAC(C++)

用OpenSSL 做HMAC(C++)

  6071人阅读  评论(5)  收藏  举报

参考:http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/


名词解释:

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


(下面的algo_hmac.h, algo_hmac.cpp 可以直接拿来放到自己的工程中)

本文工程在这里下载

algo_hmac.h

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef _ALGO_HMAC_H_  
  2. #define _ALGO_HMAC_H_  
  3.   
  4. int HmacEncode(const char * algo,  
  5.         const char * key, unsigned int key_length,  
  6.         const char * input, unsigned int input_length,  
  7.         unsigned char * &output, unsigned int &output_length);  
  8.   
  9. #endif  

algo_hmac.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "algo_hmac.h"  
  2. #include <openssl/hmac.h>  
  3. #include <string.h>  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. int HmacEncode(const char * algo,  
  8.                 const char * key, unsigned int key_length,  
  9.                 const char * input, unsigned int input_length,  
  10.                 unsigned char * &output, unsigned int &output_length) {  
  11.         const EVP_MD * engine = NULL;  
  12.         if(strcasecmp("sha512", algo) == 0) {  
  13.                 engine = EVP_sha512();  
  14.         }  
  15.         else if(strcasecmp("sha256", algo) == 0) {  
  16.                 engine = EVP_sha256();  
  17.         }  
  18.         else if(strcasecmp("sha1", algo) == 0) {  
  19.                 engine = EVP_sha1();  
  20.         }  
  21.         else if(strcasecmp("md5", algo) == 0) {  
  22.                 engine = EVP_md5();  
  23.         }  
  24.         else if(strcasecmp("sha224", algo) == 0) {  
  25.                 engine = EVP_sha224();  
  26.         }  
  27.         else if(strcasecmp("sha384", algo) == 0) {  
  28.                 engine = EVP_sha384();  
  29.         }  
  30.         else if(strcasecmp("sha", algo) == 0) {  
  31.                 engine = EVP_sha();  
  32.         }  
  33.         else if(strcasecmp("md2", algo) == 0) {  
  34.                 engine = EVP_md2();  
  35.         }  
  36.         else {  
  37.                 cout << "Algorithm " << algo << " is not supported by this program!" << endl;  
  38.                 return -1;  
  39.         }  
  40.   
  41.         output = (unsigned char*)malloc(EVP_MAX_MD_SIZE);  
  42.   
  43.         HMAC_CTX ctx;  
  44.         HMAC_CTX_init(&ctx);  
  45.         HMAC_Init_ex(&ctx, key, strlen(key), engine, NULL);  
  46.         HMAC_Update(&ctx, (unsigned char*)input, strlen(input));        // input is OK; &input is WRONG !!!  
  47.   
  48.         HMAC_Final(&ctx, output, &output_length);  
  49.         HMAC_CTX_cleanup(&ctx);  
  50.   
  51.         return 0;  
  52. }  
main.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "algo_hmac.h"  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <iostream>  
  5. #include <algorithm>  
  6. #include <string>  
  7. using namespace std;  
  8.   
  9. int main(int argc, char * argv[])  
  10. {  
  11.         if(argc < 2) {  
  12.                 cout << "Please specify a hash algorithm!" << endl;  
  13.                 return -1;  
  14.         }  
  15.   
  16.         char key[] = "012345678";  
  17.         string data = "hello world";  
  18.   
  19.         unsigned char * mac = NULL;  
  20.         unsigned int mac_length = 0;  
  21.   
  22.         int ret = HmacEncode(argv[1], key, strlen(key), data.c_str(), data.length(), mac, mac_length);  
  23.   
  24.         if(0 == ret) {  
  25.                 cout << "Algorithm HMAC encode succeeded!" << endl;  
  26.         }  
  27.         else {  
  28.                 cout << "Algorithm HMAC encode failed!" << endl;  
  29.                 return -1;  
  30.         }  
  31.   
  32.         cout << "mac length: " << mac_length << endl;  
  33.         cout << "mac:";  
  34.         for(int i = 0; i < mac_length; i++) {  
  35.                 printf("%-03x", (unsigned int)mac[i]);  
  36.         }  
  37.         cout << endl;  
  38.   
  39.         if(mac) {  
  40.                 free(mac);  
  41.                 cout << "mac is freed!" << endl;  
  42.         }  
  43.   
  44.         return 0;  
  45. }  
Makefile

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. LNK_OPT = -g -L/usr/lib64/ -lssl  
  2.   
  3. all:  
  4.     g++ -g -c algo_hmac.cpp  
  5.     g++ -g main.cpp -o test algo_hmac.o $(LNK_OPT)  
  6.   
  7. clean:  
  8.     rm -f *.o  
  9.     rm -f test  

运行结果

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. [root@ampcommons02 hmac]# ./test sha1  
  2. Algorithm HMAC encode succeeded!  
  3. mac length: 20  
  4. mac:e1 9e 22 1  22 b3 7b 70 8b fb 95 ac a2 57 79 5  ac ab f0 c0  
  5. mac is freed!  
  6.   
  7. [root@ampcommons02 hmac]# ./test sha256  
  8. Algorithm HMAC encode succeeded!  
  9. mac length: 32  
  10. mac:8b 4c 3e 7  ad 88 7a 8a 81 d0 80 ce d7 a3 bb 27 db a6 53 5f 28 fd 5e f1 45 2a 40 a6 e9 66 80 42  
  11. mac is freed!  
  12.   
  13. [root@ampcommons02 hmac]# ./test sha512  
  14. Algorithm HMAC encode succeeded!  
  15. mac length: 64  
  16. mac:40 81 e3 29 1e ec 15 4  91 10 e3 b8 d3 af 74 78 20 d1 c5 b5 39 f3 7b d7 72 49 a6 3c aa a6 e5 82 83 1  ae 2b 34 46 b1 ee 1c 45 39 d4 18 a6 4b 44 12 22 9f 9d 7  8e dc c7 8  8b 3b 41 b9 3c e6 e3  
  17. mac is freed!  
  18.   
  19. [root@ampcommons02 hmac]# ./test md5  
  20. Algorithm HMAC encode succeeded!  
  21. mac length: 16  
  22. mac:49 d5 2c b  92 54 b8 65 2b ea af fc 8d 7e 4c 21  
  23. mac is freed!  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值