Skip to content

Commit a486c3e

Browse files
author
yutingzeng
committed
修复乱码,修复时间疑惑
1 parent 8cc182d commit a486c3e

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/tls_sig_api_v2.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static std::string hmacsha256(uint32_t sdkappid,
4444
const std::string& identifier, uint64_t init_time, uint64_t expire,
4545
const std::string& key, const std::string& userbuf);
4646

47-
//去掉某些 base64 中生成的 \r\n space
47+
//去掉某些 base64 中生成的 \r\n space
4848
static std::string base64_strip(const void* data, size_t data_len)
4949
{
5050
const char* d = static_cast<const char*>(data);
@@ -171,7 +171,7 @@ static int json2sig(const rapidjson::Document &json, std::string &sig, std::stri
171171
}
172172
return 0;
173173
}
174-
TLS_API std::string getUserBuf(const std::string & account, uint32_t dwSdkappid,uint32_t dwAuthID,
174+
TLS_API std::string gen_userbuf(const std::string & account, uint32_t dwSdkappid,uint32_t dwAuthID,
175175
uint32_t dwExpTime,uint32_t dwPrivilegeMap,uint32_t dwAccountType)
176176
{
177177
int length = 1+2+account.length()+20;
@@ -200,13 +200,13 @@ TLS_API std::string getUserBuf(const std::string & account, uint32_t dwSdkappid
200200
userBuf[offset++] = ((dwAuthID & 0x00FF0000) >> 16);
201201
userBuf[offset++] = ((dwAuthID & 0x0000FF00) >> 8);
202202
userBuf[offset++] = (dwAuthID & 0x000000FF);
203-
204-
//dwExpTime
205-
//uint32_t dwExpTime = now + 300;
206-
userBuf[offset++] = ((dwExpTime & 0xFF000000) >> 24);
207-
userBuf[offset++] = ((dwExpTime & 0x00FF0000) >> 16);
208-
userBuf[offset++] = ((dwExpTime & 0x0000FF00) >> 8);
209-
userBuf[offset++] = (dwExpTime & 0x000000FF);
203+
204+
//uint32_t expire = now + dwExpTime;
205+
uint32_t expire = time(NULL) + dwExpTime;
206+
userBuf[offset++] = ((expire & 0xFF000000) >> 24);
207+
userBuf[offset++] = ((expire & 0x00FF0000) >> 16);
208+
userBuf[offset++] = ((expire & 0x0000FF00) >> 8);
209+
userBuf[offset++] = (expire & 0x000000FF);
210210

211211
//dwPrivilegeMap
212212
userBuf[offset++] = ((dwPrivilegeMap & 0xFF000000) >> 24);
@@ -222,7 +222,7 @@ TLS_API std::string getUserBuf(const std::string & account, uint32_t dwSdkappid
222222
return std::string(userBuf,length);
223223

224224
}
225-
// 生成签名
225+
// 生成签名
226226
TLS_API int gen_sig(uint32_t sdkappid, const std::string& identifier,
227227
const std::string& key, int expire,
228228
std::string& sig, std::string& errmsg)
@@ -240,17 +240,19 @@ TLS_API int gen_sig(uint32_t sdkappid, const std::string& identifier,
240240
return json2sig(sig_doc, sig, errmsg);
241241
}
242242

243-
// 生成带 userbuf 的签名
243+
// 生成带 userbuf 的签名
244244
TLS_API int gen_sig_with_userbuf(
245245
uint32_t sdkappid,
246246
const std::string& identifier,
247247
const std::string& key,
248+
int roomnum,
248249
int expire,
249-
const std::string& userbuf,
250+
int privilege,
250251
std::string& sig,
251252
std::string& errmsg)
252253
{
253254
uint64_t currTime = time(NULL);
255+
std::string userbuf = getUserBuf(identifier,sdkappid,roomnum,expire,privilege,0);
254256
std::string base64UserBuf;
255257
base64_encode(userbuf.data(), userbuf.length(), base64UserBuf);
256258
std::string base64RawSig = hmacsha256(
@@ -309,7 +311,7 @@ static std::string __hmacsha256(uint32_t sdkappid,
309311
}
310312

311313

312-
// 使用 hmac sha256 生成 sig
314+
// 使用 hmac sha256 生成 sig
313315
static std::string hmacsha256(uint32_t sdkappid,
314316
const std::string& identifier, uint64_t init_time,
315317
uint64_t expire, const std::string& key)
@@ -319,7 +321,7 @@ static std::string hmacsha256(uint32_t sdkappid,
319321
}
320322

321323

322-
// 使用 hmac sha256 生成带 userbuf 的 sig
324+
// 使用 hmac sha256 生成带 userbuf 的 sig
323325
static std::string hmacsha256(uint32_t sdkappid,
324326
const std::string& identifier, uint64_t init_time,
325327
uint64_t expire, const std::string& key,

src/tls_sig_api_v2.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ enum {
4242
* @param dwAccountType 用户类型,默认为0
4343
* @return byte[] userbuf
4444
*/
45-
TLS_API std::string getUserBuf(
45+
TLS_API std::string gen_userbuf(
4646
const std::string & account,
4747
uint32_t dwSdkappid,
4848
uint32_t dwAuthID,
@@ -75,8 +75,9 @@ TLS_API int gen_sig(
7575
* @param sdkappid 应用ID
7676
* @param identifier 用户账号,utf-8 编码
7777
* @param key 密钥
78+
* @param roomnum 房间号
7879
* @param expire 有效期,单位秒
79-
* @param userbuf 用户数据
80+
* @param privilege 用户权限,255表示所有权限
8081
* @param sig 返回的 sig
8182
* @param errmsg 错误信息
8283
*
@@ -86,8 +87,9 @@ TLS_API int gen_sig_with_userbuf(
8687
uint32_t sdkappid,
8788
const std::string& identifier,
8889
const std::string& key,
90+
int roomnum,
8991
int expire,
90-
const std::string& userbuf,
92+
int privilege,
9193
std::string& sig,
9294
std::string& errmsg);
9395

src/tlssigapi_v2_tool.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ static int gen_sig(const std::string& key, const std::string& sig_file,
6060
}
6161

6262
static int gen_sig_with_userbuf(const std::string& key, const std::string& sig_file,
63-
uint32_t sdkappid, const std::string& identifier, const std::string& userbuf)
63+
uint32_t sdkappid, const std::string& identifier)
6464
{
6565
std::string sig;
6666
std::string err_msg;
67-
int ret = gen_sig_with_userbuf(sdkappid, identifier, key,
68-
180*86400, userbuf, sig, err_msg);
67+
int ret = gen_sig_with_userbuf(sdkappid, identifier, key,
68+
1000,180*86400, 255, sig, err_msg);
6969
if (0 != ret) {
7070
std::cout << "error msg: " << err_msg << " return " << ret << std::endl;
7171
return -3;
@@ -108,7 +108,6 @@ int main(int argc, char * argv[])
108108
std::string sig_file;
109109
std::string sdkappid_str;
110110
std::string identifier;
111-
std::string userbuf;
112111

113112
int ret = 0;
114113
if (0 == strcmp(cmd, "gen") && 6 == argc) {
@@ -122,9 +121,8 @@ int main(int argc, char * argv[])
122121
std::string sig_file = argv[3];
123122
std::string sdkappid_str = argv[4];
124123
std::string identifier = argv[5];
125-
std::string userbuf = getUserBuf(identifier,strtol(sdkappid_str.c_str(), NULL, 10),10000,180*86400,255,0);
126124
ret = gen_sig_with_userbuf(key, sig_file,
127-
strtol(sdkappid_str.c_str(), NULL, 10), identifier, userbuf);
125+
strtol(sdkappid_str.c_str(), NULL, 10), identifier);
128126
} else {
129127
usage(argv[0]);
130128
return -1;

0 commit comments

Comments
 (0)