forked from LiXizhi/TMInterface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.cpp
More file actions
170 lines (141 loc) · 4.51 KB
/
Copy pathauth.cpp
File metadata and controls
170 lines (141 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file auth.cpp
* @brief 验证方法集合
* @author baron baron@taomee.com
* @version 1.0
* @date 2010-10-19
*/
#include "auth.h"
#include <stdio.h>
#include <string.h>
#include <openssl/md5.h>
#include <openssl/des.h>
void md5_16to32(const unsigned char *p_md5_buffer_16,
unsigned char *p_md5_buffer_32,
int is_upper)
{
const char *lower_set = "0123456789abcdef";
const char *upper_set = "0123456789ABCDEF";
if (is_upper)
{
for (int i = 0; i < 16; ++ i)
{
p_md5_buffer_32[2 * i]
= upper_set[static_cast<int>(p_md5_buffer_16[i] >> 4)];
p_md5_buffer_32[2 * i + 1]
= upper_set[static_cast<int>(p_md5_buffer_16[i] & 0x0F)];
}
}
else
{
for (int i = 0; i < 16; ++ i)
{
p_md5_buffer_32[2 * i]
= lower_set[static_cast<int>(p_md5_buffer_16[i] >> 4)];
p_md5_buffer_32[2 * i + 1]
= lower_set[static_cast<int>(p_md5_buffer_16[i] & 0x0F)];
}
}
return;
}
int gen_chnlhash32(int chnl_id,
const char *p_chnl_key,
const char *p_data,
int data_length,
chnlhash32_t *p_chnlhash32)
{
if (!p_chnl_key || !p_data || !p_chnlhash32 || data_length < 0)
{
return -1;
}
char hash_buffer[MAX_HASH_BUFFER_LENGTH];
unsigned char md5_buffer_16[16];
unsigned char md5_buffer_32[32];
int length = sprintf(hash_buffer,
"channelId=%d&securityCode=%s&data=",
chnl_id,
p_chnl_key);
if (length <= 0)
{
return -1;
}
// 用户hash的缓冲区不大于MAX_HASH_BUFFER_LENGTH
int length_empty = static_cast<int>(sizeof(hash_buffer)) - length;
int length_copy = data_length <= length_empty ? data_length : length_empty;
memcpy(hash_buffer + length, p_data, length_copy);
length += length_copy;
MD5(reinterpret_cast<unsigned char *>(hash_buffer), length, md5_buffer_16);
md5_16to32(md5_buffer_16, md5_buffer_32);
char x[33] = {'\0'};
memcpy(x, md5_buffer_32, 32);
p_chnlhash32->chnl_id = chnl_id;
memcpy(p_chnlhash32->vfy_code, md5_buffer_32, 32);
return 0;
}
int verify_chnlhash32(const chnlhash32_t *p_chnlhash32,
const char *p_key,
const char *p_data,
int data_length)
{
if (!p_chnlhash32 || !p_key || !p_data || data_length < 0)
{
return -1;
}
char hash_buffer[MAX_HASH_BUFFER_LENGTH];
unsigned char md5_buffer_16[16];
unsigned char md5_buffer_32[32];
int length = sprintf(hash_buffer,
"channelId=%d&securityCode=%s&data=",
p_chnlhash32->chnl_id,
p_key);
if (length <= 0)
{
return -1;
}
int length_empty = static_cast<int>(sizeof(hash_buffer)) - length;
int length_copy = data_length <= length_empty ? data_length : length_empty;
// 用户hash的缓冲区不大于MAX_HASH_BUFFER_LENGTH
memcpy(hash_buffer + length, p_data, length_copy);
length += length_copy;
MD5(reinterpret_cast<unsigned char *>(hash_buffer), length, md5_buffer_16);
md5_16to32(md5_buffer_16, md5_buffer_32);
if (memcmp(p_chnlhash32->vfy_code, md5_buffer_32, 32))
{
return -1;
}
return 0;
}
int encrypt_des(void *key, void *plaintext, void *ciphertext, int n_8bytes)
{
if (!key || !plaintext || !ciphertext || n_8bytes <= 0)
{
return -1;
}
DES_key_schedule schedule;
DES_set_key(reinterpret_cast<unsigned char (*)[8]>(key), &schedule);
for (int i = 0; i < n_8bytes; ++ i)
{
DES_ecb_encrypt(reinterpret_cast<unsigned char (*)[8]>(plaintext) + i,
reinterpret_cast<unsigned char (*)[8]>(ciphertext) + i,
&schedule,
1);
}
return 0;
}
int decrypt_des(void *key, void *ciphertext, void *plaintext, int n_8bytes)
{
if (!key || !plaintext || !ciphertext || n_8bytes <= 0)
{
return -1;
}
DES_key_schedule schedule;
DES_set_key(reinterpret_cast<unsigned char (*)[8]>(key), &schedule);
for (int i = 0; i < n_8bytes; ++ i)
{
DES_ecb_encrypt(reinterpret_cast<unsigned char (*)[8]>(ciphertext) + i,
reinterpret_cast<unsigned char (*)[8]>(plaintext) + i,
&schedule,
0);
}
return 0;
}