版权声明:本文为博主原创文章,未经博主允许不得转载。
参考: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
- #ifndef _ALGO_HMAC_H_
- #define _ALGO_HMAC_H_
- int HmacEncode(const char * algo,
- const char * key, unsigned int key_length,
- const char * input, unsigned int input_length,
- unsigned char * &output, unsigned int &output_length);
- #endif
algo_hmac.cpp
- #include "algo_hmac.h"
- #include <openssl/hmac.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- int HmacEncode(const char * algo,
- const char * key, unsigned int key_length,
- const char * input, unsigned int input_length,
- unsigned char * &output, unsigned int &output_length) {
- const EVP_MD * engine = NULL;
- if(strcasecmp("sha512", algo) == 0) {
- engine = EVP_sha512();
- }
- else if(strcasecmp("sha256", algo) == 0) {
- engine = EVP_sha256();
- }
- else if(strcasecmp("sha1", algo) == 0) {
- engine = EVP_sha1();
- }
- else if(strcasecmp("md5", algo) == 0) {
- engine = EVP_md5();
- }
- else if(strcasecmp("sha224", algo) == 0) {
- engine = EVP_sha224();
- }
- else if(strcasecmp("sha384", algo) == 0) {
- engine = EVP_sha384();
- }
- else if(strcasecmp("sha", algo) == 0) {
- engine = EVP_sha();
- }
- else if(strcasecmp("md2", algo) == 0) {
- engine = EVP_md2();
- }
- else {
- cout << "Algorithm " << algo << " is not supported by this program!" << endl;
- return -1;
- }
- output = (unsigned char*)malloc(EVP_MAX_MD_SIZE);
- HMAC_CTX ctx;
- HMAC_CTX_init(&ctx);
- HMAC_Init_ex(&ctx, key, strlen(key), engine, NULL);
- HMAC_Update(&ctx, (unsigned char*)input, strlen(input)); // input is OK; &input is WRONG !!!
- HMAC_Final(&ctx, output, &output_length);
- HMAC_CTX_cleanup(&ctx);
- return 0;
- }
- #include "algo_hmac.h"
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <algorithm>
- #include <string>
- using namespace std;
- int main(int argc, char * argv[])
- {
- if(argc < 2) {
- cout << "Please specify a hash algorithm!" << endl;
- return -1;
- }
- char key[] = "012345678";
- string data = "hello world";
- unsigned char * mac = NULL;
- unsigned int mac_length = 0;
- int ret = HmacEncode(argv[1], key, strlen(key), data.c_str(), data.length(), mac, mac_length);
- if(0 == ret) {
- cout << "Algorithm HMAC encode succeeded!" << endl;
- }
- else {
- cout << "Algorithm HMAC encode failed!" << endl;
- return -1;
- }
- cout << "mac length: " << mac_length << endl;
- cout << "mac:";
- for(int i = 0; i < mac_length; i++) {
- printf("%-03x", (unsigned int)mac[i]);
- }
- cout << endl;
- if(mac) {
- free(mac);
- cout << "mac is freed!" << endl;
- }
- return 0;
- }
- LNK_OPT = -g -L/usr/lib64/ -lssl
- all:
- g++ -g -c algo_hmac.cpp
- g++ -g main.cpp -o test algo_hmac.o $(LNK_OPT)
- clean:
- rm -f *.o
- rm -f test
运行结果
- [root@ampcommons02 hmac]# ./test sha1
- Algorithm HMAC encode succeeded!
- mac length: 20
- mac:e1 9e 22 1 22 b3 7b 70 8b fb 95 ac a2 57 79 5 ac ab f0 c0
- mac is freed!
- [root@ampcommons02 hmac]# ./test sha256
- Algorithm HMAC encode succeeded!
- mac length: 32
- 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
- mac is freed!
- [root@ampcommons02 hmac]# ./test sha512
- Algorithm HMAC encode succeeded!
- mac length: 64
- 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
- mac is freed!
- [root@ampcommons02 hmac]# ./test md5
- Algorithm HMAC encode succeeded!
- mac length: 16
- mac:49 d5 2c b 92 54 b8 65 2b ea af fc 8d 7e 4c 21
- mac is freed!