
1. 命令行格式验证sm3 hmacecho -n abc | openssl dgst -sm3 -hmac 01234567890123456789012345678901 说明 1. 01234567890123456789012345678901 为字符串key共32位 2. echo -n abc共abc3个字符长度不包含任何其他字符 执行如下 rootubuntu:/work2/learn/openssl/sm3# echo -n abc | openssl dgst -sm3 -hmac 01234567890123456789012345678901 (stdin) ce68f88b05a45a87303a2a3eca942e46d4dce6d06596fa52b3fc0ce43440d5dc2. 基于C语标题言使用openssl api接口实现 sm3 sm3_hmacrootubuntu:/work2/learn/openssl/sm3# ./sm3_hmac_demo hamc hmac_excepted_len 32: ce 68 f8 8b 05 a4 5a 87 30 3a 2a 3e ca 94 2e 46 d4 dc e6 d0 65 96 fa 52 b3 fc 0c e4 34 40 d5 dc 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00代码.// 编译// 编译gcc -g sm3_hmac_demo.c -o sm3_hmac_demo -L/usr/lib -lssl -lcrypto#includestdio.h#includestring.h#includeopenssl/evp.h#includeopenssl/x509v3.h#includeopenssl/hmac.hvoidsm3_hmac(constEVP_MD*evp_md,constvoid*key,intkey_len,constunsignedchar*d,size_tn,unsignedchar*md,unsignedint*md_len){HMAC_CTX*cNULL;if((cHMAC_CTX_new())NULL){printf(%s %d \n,__func__,__LINE__);gotoerr;}if(!HMAC_Init_ex(c,key,key_len,evp_md,NULL)){printf(%s %d \n,__func__,__LINE__);gotoerr;}if(!HMAC_Update(c,d,n)){printf(%s %d \n,__func__,__LINE__);gotoerr;}if(!HMAC_Final(c,md,md_len)){printf(%s %d \n,__func__,__LINE__);gotoerr;}HMAC_CTX_free(c);err:HMAC_CTX_free(c);}intmain(){unsignedcharkey[]01234567890123456789012345678901;unsignedchardata[]{a,b,c,0};unsignedcharhmac_value[64];unsignedinthmac_excepted_len;memset(hmac_value,0,sizeof(hmac_value));// sm3_hmac(EVP_sm3(),key,sizeof(key),data,sizeof(data),hmac_value,hmac_excepted_len);HMAC(EVP_sm3(),key,sizeof(key),data,sizeof(data)-1,hmac_value,hmac_excepted_len);printf(hamc hmac_excepted_len %d:\n,hmac_excepted_len);inti;for(i0;isizeof(hmac_value);i){printf(%02x ,hmac_value[i]);}printf(\n);return0;}