Skip to content

Commit ceb9f03

Browse files
committed
modify sign verify
1 parent 0030896 commit ceb9f03

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

include/tigerapi/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
#include <cpprest/details/basic_types.h>
1212
#include "win32.h"
1313

14-
OPENAPI_EXPORT utility::string_t convert_str(std::string s);
14+
OPENAPI_EXPORT utility::string_t str8to16(std::string s);
15+
16+
OPENAPI_EXPORT std::string str16to8(utility::string_t s);
1517

1618
OPENAPI_EXPORT utility::string_t get_timestamp();
1719

src/common/sign_util.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ utility::string_t sha1_sign(const utility::string_t& context, const utility::str
6464
unsigned int encrypted_length;
6565
unsigned char hash[SHA_DIGEST_LENGTH] = { 0 };
6666

67-
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
68-
auto str_transfer_content = utility::conversions::utf16_to_utf8(context);
69-
#else
70-
auto str_transfer_content = context;
71-
#endif
67+
auto str_transfer_content = str16to8(context);
7268
int context_size = str_transfer_content.size();
7369
SHA1((const unsigned char*)str_transfer_content.c_str(), context_size, hash);
7470
RSA *rsa = create_rsa((utility::char_t *)key.c_str(), true);
@@ -83,21 +79,22 @@ utility::string_t sha1_sign(const utility::string_t& context, const utility::str
8379

8480
int sha1_verify(const utility::string_t& context, const utility::string_t& sign, const utility::string_t& key) {
8581

86-
utility::char_t sigbuf[8196 * 16] = {};
82+
auto context_s = str16to8(context);
83+
unsigned char sigbuf[8196 * 16] = {};
8784
unsigned int siglen = 0;
8885

89-
utility::string_t decoded = TIGER_API::base64_decode(sign);
86+
std::string decoded = str16to8(TIGER_API::base64_decode(sign));
9087
memcpy(sigbuf, decoded.data(), decoded.size());
9188
siglen = decoded.size();
9289

9390
unsigned char hash[SHA_DIGEST_LENGTH] = { 0 };
9491

95-
SHA1((const unsigned char*)context.c_str(), context.size(), hash);
92+
SHA1((const unsigned char*)context_s.c_str(), context_s.size(), hash);
9693

9794
RSA *rsa = create_rsa((utility::char_t *)key.c_str(), false);
9895

9996
int ret = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
100-
reinterpret_cast<unsigned char*>(sigbuf), siglen, rsa);
97+
sigbuf, siglen, rsa);
10198
return ret;
10299

103100
}

src/utils.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@ using namespace std;
1212
using namespace web;
1313
using namespace TIGER_API;
1414

15-
utility::string_t convert_str(std::string s) {
15+
utility::string_t str8to16(std::string s) {
1616
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
1717
return utility::conversions::utf8_to_utf16(s);
1818
#else
1919
return s;
2020
#endif
2121
}
2222

23+
std::string str16to8(utility::string_t s) {
24+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
25+
return utility::conversions::utf16_to_utf8(s);
26+
#else
27+
return s;
28+
#endif
29+
}
30+
2331
utility::string_t get_timestamp() {
2432
// time_t t = time(NULL);
2533
// utility::char_t tmp[32];
@@ -30,7 +38,7 @@ utility::string_t get_timestamp() {
3038
std::stringstream ss;
3139
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
3240
std::string result = ss.str();
33-
return convert_str(result);
41+
return str8to16(result);
3442
}
3543

3644
time_t date_string_to_timestamp(const utility::string_t &date_string) {
@@ -53,12 +61,11 @@ utility::string_t get_sign(utility::string_t &private_key, const utility::string
5361

5462
bool verify_sign(utility::string_t public_key, const utility::string_t &content,
5563
const utility::string_t &encoded_signature) {
56-
return true;
64+
//return true;
5765
utility::string_t filled_public_key = fill_public_key_marker(public_key);
5866
int ret = sha1_verify(content, encoded_signature, filled_public_key);
5967

6068
if (ret != 1) {
61-
//LOGGER(info) << U("Public Decrypt failed");
6269
return false;
6370
}
6471
return true;

0 commit comments

Comments
 (0)