最近游戏后台接入腾讯敏感词接口: /v3/user/uic_filter   需要加密方式是hmac_sha1,项目中没有,于是从网上找了库,最后上线应用。说下具体流程和注意的坑:

关键的函数:HMAC_SHA1

注意的点:mars_https::BYTE digest[20];这里数组一定要是20

最终要经过另一个函数调用洗礼:byteToHexStr

以下是测试代码和库函数。

hmac_sha1 c++例子
 1 #include <iostream>
 2 #include <math.h>
 3 #include "HmacSha1.h"
 4 #include <string>
 5 using namespace std;
 6 
 7 string byteToHexStr(unsigned char byte_arr[], int arr_len)
 8 {
 9     string hexstr;
10     for (int i=0;i<arr_len;i++)
11     {
12         char hex1;
13         char hex2;
14         int value=byte_arr[i];
15         int v1=value/16;
16         int v2=value % 16;
17 
18         //将商转成字母
19         if (v1>=0&&v1<=9)
20             hex1=(char)(48+v1);
21         else
22             hex1=(char)(55+v1);
23 
24         //将余数转成字母
25         if (v2>=0&&v2<=9)
26             hex2=(char)(48+v2);
27         else
28             hex2=(char)(55+v2);
29 
30         //将字母连接成串
31         hexstr=hexstr+hex1+hex2;
32     }
33     return hexstr;
34 }
35 int main()
36 {
37     string strJoin = "POST&%2Fv3%2Fuser%2Fuic_filter&appid%3D1105752926%26openid%3D33FA239D835DADB54CC6E06D100AB989%26openkey%3DAFE95D97DF01E8DC8F7C75E1041AD1E2";
38     string strKey = "0aYIIoABlh3VCM4I&";
39     mars_https::BYTE digest[20];
40     mars_https::HMAC_SHA1((mars_https::BYTE*)strJoin.c_str(), strlen(strJoin.c_str()),(mars_https::BYTE*)strKey.c_str(),strlen(strKey.c_str()),digest);
41     int len = sizeof(digest);
42     string m_strSerialNumber=byteToHexStr(digest,len).c_str();
43     cout<<m_strSerialNumber<<endl;
44     return 0;
45 }
test.cpp

相关文章:

  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案