|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
include './SecretContext.php'; |
|
4
|
|
|
include './MagicCrypt.php'; |
|
5
|
|
|
|
|
6
|
|
|
class SecurityUtil |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
private $BASE64_ARRAY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
|
10
|
|
|
private $SEPARATOR_CHAR_MAP; |
|
11
|
|
|
|
|
12
|
|
|
function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
if(!defined("PHONE_SEPARATOR_CHAR")) |
|
15
|
|
|
{ |
|
16
|
|
|
define('PHONE_SEPARATOR_CHAR','$'); |
|
17
|
|
|
} |
|
18
|
|
|
if(!defined("NICK_SEPARATOR_CHAR")) |
|
19
|
|
|
{ |
|
20
|
|
|
define('NICK_SEPARATOR_CHAR','~'); |
|
21
|
|
|
} |
|
22
|
|
|
if(!defined("NORMAL_SEPARATOR_CHAR")) |
|
23
|
|
|
{ |
|
24
|
|
|
define('NORMAL_SEPARATOR_CHAR',chr(1)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->SEPARATOR_CHAR_MAP['nick'] = NICK_SEPARATOR_CHAR; |
|
28
|
|
|
$this->SEPARATOR_CHAR_MAP['simple'] = NICK_SEPARATOR_CHAR; |
|
29
|
|
|
$this->SEPARATOR_CHAR_MAP['receiver_name'] = NICK_SEPARATOR_CHAR; |
|
30
|
|
|
$this->SEPARATOR_CHAR_MAP['search'] = NICK_SEPARATOR_CHAR; |
|
31
|
|
|
$this->SEPARATOR_CHAR_MAP['normal'] = NORMAL_SEPARATOR_CHAR; |
|
32
|
|
|
$this->SEPARATOR_CHAR_MAP['phone'] = PHONE_SEPARATOR_CHAR; |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/* |
|
37
|
|
|
* 判断是否是base64格式的数据 |
|
38
|
|
|
*/ |
|
39
|
|
|
function isBase64Str($str) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
$strLen = strlen($str); |
|
42
|
|
|
for($i = 0; $i < $strLen ; $i++) |
|
43
|
|
|
{ |
|
44
|
|
|
if(!$this->isBase64Char($str[$i])) |
|
45
|
|
|
{ |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/* |
|
53
|
|
|
* 判断是否是base64格式的字符 |
|
54
|
|
|
*/ |
|
55
|
|
|
function isBase64Char($char) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
return strpos($this->BASE64_ARRAY,$char) !== false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* 使用sep字符进行trim |
|
62
|
|
|
*/ |
|
63
|
|
|
function trimBySep($str,$sep) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$start = 0; |
|
66
|
|
|
$end = strlen($str); |
|
67
|
|
|
for($i = 0; $i < $end; $i++) |
|
68
|
|
|
{ |
|
69
|
|
|
if($str[$i] == $sep) |
|
70
|
|
|
{ |
|
71
|
|
|
$start = $i + 1; |
|
72
|
|
|
} |
|
73
|
|
|
else |
|
74
|
|
|
{ |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
for($i = $end -1 ; $i >= 0; $i--) |
|
79
|
|
|
{ |
|
80
|
|
|
if($str[$i] == $sep) |
|
81
|
|
|
{ |
|
82
|
|
|
$end = $i - 1; |
|
83
|
|
|
} |
|
84
|
|
|
else |
|
85
|
|
|
{ |
|
86
|
|
|
break; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return substr($str,$start,$end); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function checkEncryptData($dataArray) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
if(count($dataArray) == 2){ |
|
95
|
|
|
return $this->isBase64Str($dataArray[0]); |
|
96
|
|
|
}else{ |
|
97
|
|
|
return $this->isBase64Str($dataArray[0]) && $this->isBase64Str($dataArray[1]); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/* |
|
102
|
|
|
* 判断是否是加密数据 |
|
103
|
|
|
*/ |
|
104
|
|
|
function isEncryptDataArray($array,$type) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
foreach ($array as $value) { |
|
107
|
|
|
if(!$this->isEncryptData($value,$type)){ |
|
|
|
|
|
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* 判断是否是已加密的数据,数据必须是同一个类型 |
|
116
|
|
|
*/ |
|
117
|
|
|
function isPartEncryptData($array,$type) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$result = false; |
|
120
|
|
|
foreach ($array as $value) { |
|
121
|
|
|
if($this->isEncryptData($value,$type)){ |
|
122
|
|
|
$result = true; |
|
123
|
|
|
break; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/* |
|
130
|
|
|
* 判断是否是加密数据 |
|
131
|
|
|
*/ |
|
132
|
|
|
function isEncryptData($data,$type) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
if(!is_string($data) || strlen($data) < 4) |
|
135
|
|
|
{ |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
140
|
|
|
$strlen = strlen($data); |
|
141
|
|
|
if($data[0] != $separator || $data[$strlen -1] != $separator) |
|
142
|
|
|
{ |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
147
|
|
|
$arrayLength = count($dataArray); |
|
148
|
|
|
|
|
149
|
|
|
if($separator == PHONE_SEPARATOR_CHAR) |
|
150
|
|
|
{ |
|
151
|
|
|
if($arrayLength != 3) |
|
152
|
|
|
{ |
|
153
|
|
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
if($data[$strlen - 2] == $separator) |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->checkEncryptData($dataArray); |
|
158
|
|
|
} |
|
159
|
|
|
else |
|
160
|
|
|
{ |
|
161
|
|
|
$version = $dataArray[$arrayLength -1]; |
|
162
|
|
|
if(is_numeric($version)) |
|
163
|
|
|
{ |
|
164
|
|
|
$base64Val = $dataArray[$arrayLength -2]; |
|
165
|
|
|
return $this->isBase64Str($base64Val); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
}else{ |
|
169
|
|
|
if($data[strlen($data) - 2] == $separator && $arrayLength == 3) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->checkEncryptData($dataArray); |
|
172
|
|
|
} |
|
173
|
|
|
else if($arrayLength == 2) |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->checkEncryptData($dataArray); |
|
176
|
|
|
} |
|
177
|
|
|
else |
|
178
|
|
|
{ |
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
function search($data, $type,$secretContext) |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
$separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
187
|
|
|
if('phone' == $type) { |
|
188
|
|
|
if (strlen($data) != 4 ) { |
|
189
|
|
|
throw new Exception("phoneNumber error"); |
|
190
|
|
|
} |
|
191
|
|
|
return $separator.$this->hmacMD5EncryptToBase64($data, $secretContext->secret).$separator; |
|
192
|
|
|
} else { |
|
193
|
|
|
$compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
194
|
|
|
$slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
195
|
|
|
|
|
196
|
|
|
$slideList = $this->getSlideWindows($data, $slideSize); |
|
197
|
|
|
$builder = ''; |
|
198
|
|
|
foreach ($slideList as $slide) { |
|
199
|
|
|
$builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
200
|
|
|
} |
|
201
|
|
|
return $builder; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/* |
|
206
|
|
|
* 加密逻辑 |
|
207
|
|
|
*/ |
|
208
|
|
|
function encrypt($data,$type,$version,$secretContext) |
|
|
|
|
|
|
209
|
|
|
{ |
|
210
|
|
|
if(!is_string($data)) |
|
211
|
|
|
{ |
|
212
|
|
|
return false; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
216
|
|
|
$isIndexEncrypt = $this->isIndexEncrypt($type,$version,$secretContext); |
|
217
|
|
|
if($isIndexEncrypt || $type == "search"){ |
|
218
|
|
|
if('phone' == $type) { |
|
219
|
|
|
return $this->encryptPhoneIndex($data,$separator,$secretContext); |
|
220
|
|
|
} else { |
|
221
|
|
|
$compressLen = $this->getArrayValue($secretContext->appConfig,'encrypt_index_compress_len',3); |
|
222
|
|
|
$slideSize = $this->getArrayValue($secretContext->appConfig,'encrypt_slide_size',4); |
|
223
|
|
|
return $this->encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext); |
|
224
|
|
|
} |
|
225
|
|
|
}else{ |
|
226
|
|
|
if('phone' == $type) { |
|
227
|
|
|
return $this->encryptPhone($data,$separator,$secretContext); |
|
228
|
|
|
} else { |
|
229
|
|
|
return $this->encryptNormal($data,$separator,$secretContext); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/* |
|
236
|
|
|
* 加密逻辑,手机号码格式 |
|
237
|
|
|
*/ |
|
238
|
|
|
function encryptPhone($data,$separator,$secretContext) |
|
|
|
|
|
|
239
|
|
|
{ |
|
240
|
|
|
$len = strlen($data); |
|
241
|
|
|
if($len < 11) |
|
242
|
|
|
{ |
|
243
|
|
|
return $data; |
|
244
|
|
|
} |
|
245
|
|
|
$prefixNumber = substr($data,0,$len -8); |
|
246
|
|
|
$last8Number = substr($data,$len -8,$len); |
|
247
|
|
|
|
|
248
|
|
|
return $separator.$prefixNumber.$separator.Security::encrypt($last8Number,$secretContext->secret) |
|
249
|
|
|
.$separator.$secretContext->secretVersion.$separator ; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/* |
|
253
|
|
|
* 加密逻辑,非手机号码格式 |
|
254
|
|
|
*/ |
|
255
|
|
|
function encryptNormal($data,$separator,$secretContext) |
|
|
|
|
|
|
256
|
|
|
{ |
|
257
|
|
|
return $separator.Security::encrypt($data,$secretContext->secret) |
|
258
|
|
|
.$separator.$secretContext->secretVersion.$separator; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/* |
|
262
|
|
|
* 解密逻辑 |
|
263
|
|
|
*/ |
|
264
|
|
|
function decrypt($data,$type,$secretContext) |
|
|
|
|
|
|
265
|
|
|
{ |
|
266
|
|
|
if(!$this->isEncryptData($data,$type)) |
|
|
|
|
|
|
267
|
|
|
{ |
|
268
|
|
|
throw new Exception("数据[".$data."]不是类型为[".$type."]的加密数据"); |
|
269
|
|
|
} |
|
270
|
|
|
$dataLen = strlen($data); |
|
271
|
|
|
$separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
272
|
|
|
|
|
273
|
|
|
$secretData = null; |
|
274
|
|
|
if($data[$dataLen - 2] == $separator){ |
|
275
|
|
|
$secretData = $this->getIndexSecretData($data,$separator); |
|
276
|
|
|
}else{ |
|
277
|
|
|
$secretData = $this->getSecretData($data,$separator); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if($secretData == null){ |
|
281
|
|
|
return $data; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$result = Security::decrypt($secretData->originalBase64Value,$secretContext->secret); |
|
285
|
|
|
|
|
286
|
|
|
if($separator == PHONE_SEPARATOR_CHAR && !$secretData->search) |
|
287
|
|
|
{ |
|
288
|
|
|
return $secretData->originalValue.$result; |
|
289
|
|
|
} |
|
290
|
|
|
return $result; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/* |
|
294
|
|
|
* 判断是否是公钥数据 |
|
295
|
|
|
*/ |
|
296
|
|
|
function isPublicData($data,$type) |
|
|
|
|
|
|
297
|
|
|
{ |
|
298
|
|
|
$secretData = $this->getSecretDataByType($data,$type); |
|
299
|
|
|
if(empty($secretData)){ |
|
300
|
|
|
return false; |
|
301
|
|
|
} |
|
302
|
|
|
if(intval($secretData->secretVersion) < 0){ |
|
303
|
|
|
return true; |
|
304
|
|
|
} |
|
305
|
|
|
return false; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
function getSecretDataByType($data,$type) |
|
|
|
|
|
|
309
|
|
|
{ |
|
310
|
|
|
$separator = $this->SEPARATOR_CHAR_MAP[$type]; |
|
311
|
|
|
$dataLen = strlen($data); |
|
312
|
|
|
|
|
313
|
|
|
if($data[$dataLen - 2] == $separator){ |
|
314
|
|
|
return $secretData = $this->getIndexSecretData($data,$separator); |
|
|
|
|
|
|
315
|
|
|
}else{ |
|
316
|
|
|
return $secretData = $this->getSecretData($data,$separator); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/* |
|
321
|
|
|
* 分解密文 |
|
322
|
|
|
*/ |
|
323
|
|
|
function getSecretData($data,$separator) |
|
|
|
|
|
|
324
|
|
|
{ |
|
325
|
|
|
$secretData = new SecretData; |
|
326
|
|
|
$dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
327
|
|
|
$arrayLength = count($dataArray); |
|
328
|
|
|
|
|
329
|
|
|
if($separator == PHONE_SEPARATOR_CHAR) |
|
330
|
|
|
{ |
|
331
|
|
|
if($arrayLength != 3){ |
|
332
|
|
|
return null; |
|
333
|
|
|
}else{ |
|
334
|
|
|
$version = $dataArray[2]; |
|
335
|
|
|
if(is_numeric($version)) |
|
336
|
|
|
{ |
|
337
|
|
|
$secretData->originalValue = $dataArray[0]; |
|
338
|
|
|
$secretData->originalBase64Value = $dataArray[1]; |
|
339
|
|
|
$secretData->secretVersion = $version; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
else |
|
344
|
|
|
{ |
|
345
|
|
|
if($arrayLength != 2){ |
|
346
|
|
|
return null; |
|
347
|
|
|
}else{ |
|
348
|
|
|
$version = $dataArray[1]; |
|
349
|
|
|
if(is_numeric($version)) |
|
350
|
|
|
{ |
|
351
|
|
|
$secretData->originalBase64Value = $dataArray[0]; |
|
352
|
|
|
$secretData->secretVersion = $version; |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
return $secretData; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
function getIndexSecretData($data,$separator) { |
|
|
|
|
|
|
360
|
|
|
$secretData = new SecretData; |
|
361
|
|
|
$dataArray = explode($separator,$this->trimBySep($data,$separator)); |
|
362
|
|
|
$arrayLength = count($dataArray); |
|
363
|
|
|
|
|
364
|
|
|
if($separator == PHONE_SEPARATOR_CHAR) { |
|
365
|
|
|
if ($arrayLength != 3) { |
|
366
|
|
|
return null; |
|
367
|
|
|
}else{ |
|
368
|
|
|
$version = $dataArray[2]; |
|
369
|
|
|
if(is_numeric($version)) |
|
370
|
|
|
{ |
|
371
|
|
|
$secretData->originalValue = $dataArray[0]; |
|
372
|
|
|
$secretData->originalBase64Value = $dataArray[1]; |
|
373
|
|
|
$secretData->secretVersion = $version; |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
} else { |
|
378
|
|
|
if($arrayLength != 3){ |
|
379
|
|
|
return null; |
|
380
|
|
|
} else { |
|
381
|
|
|
$version = $dataArray[2]; |
|
382
|
|
|
if(is_numeric($version)) |
|
383
|
|
|
{ |
|
384
|
|
|
$secretData->originalBase64Value = $dataArray[0]; |
|
385
|
|
|
$secretData->originalValue = $dataArray[1]; |
|
386
|
|
|
$secretData->secretVersion = $version; |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
$secretData->search = true; |
|
392
|
|
|
return $secretData; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* 判断密文是否支持检索 |
|
397
|
|
|
* |
|
398
|
|
|
* @param key |
|
399
|
|
|
* @param version |
|
400
|
|
|
* @return |
|
401
|
|
|
*/ |
|
402
|
|
|
function isIndexEncrypt($key,$version,$secretContext) |
|
|
|
|
|
|
403
|
|
|
{ |
|
404
|
|
|
if ($version != null && $version < 0) { |
|
405
|
|
|
$key = "previous_".$key; |
|
406
|
|
|
} else { |
|
407
|
|
|
$key = "current_".$key; |
|
408
|
|
|
} |
|
409
|
|
|
|
|
410
|
|
|
return $secretContext->appConfig != null && |
|
411
|
|
|
array_key_exists($key,$secretContext->appConfig) && |
|
412
|
|
|
$secretContext->appConfig[$key] == "2"; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
function isLetterOrDigit($ch) |
|
|
|
|
|
|
416
|
|
|
{ |
|
417
|
|
|
$code = ord($ch); |
|
418
|
|
|
if (0 <= $code && $code <= 127) { |
|
419
|
|
|
return true; |
|
420
|
|
|
} |
|
421
|
|
|
return false; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
function utf8_strlen($string = null) { |
|
|
|
|
|
|
425
|
|
|
// 将字符串分解为单元 |
|
426
|
|
|
preg_match_all("/./us", $string, $match); |
|
427
|
|
|
// 返回单元个数 |
|
428
|
|
|
return count($match[0]); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
function utf8_substr($string,$start,$end) { |
|
|
|
|
|
|
432
|
|
|
// 将字符串分解为单元 |
|
433
|
|
|
preg_match_all("/./us", $string, $match); |
|
434
|
|
|
// 返回单元个数 |
|
435
|
|
|
$result = ""; |
|
436
|
|
|
for($i = $start; $i < $end; $i++){ |
|
437
|
|
|
$result .= $match[0][$i]; |
|
438
|
|
|
} |
|
439
|
|
|
return $result; |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
function utf8_str_at($string,$index) { |
|
|
|
|
|
|
443
|
|
|
// 将字符串分解为单元 |
|
444
|
|
|
preg_match_all("/./us", $string, $match); |
|
445
|
|
|
// 返回单元个数 |
|
446
|
|
|
return $match[0][$index]; |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
function compress($input,$toLength) { |
|
|
|
|
|
|
450
|
|
|
if($toLength < 0) { |
|
451
|
|
|
return null; |
|
452
|
|
|
} |
|
453
|
|
|
$output = array(); |
|
454
|
|
|
for($i = 0; $i < $toLength; $i++) { |
|
455
|
|
|
$output[$i] = chr(0); |
|
456
|
|
|
} |
|
457
|
|
|
$input = $this->getBytes($input); |
|
458
|
|
|
$inputLength = count($input); |
|
459
|
|
|
for ($i = 0; $i < $inputLength; $i++) { |
|
460
|
|
|
$index_output = $i % $toLength; |
|
461
|
|
|
$output[$index_output] = $output[$index_output] ^ $input[$i]; |
|
462
|
|
|
} |
|
463
|
|
|
return $output; |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* @see #hmacMD5Encrypt |
|
468
|
|
|
* |
|
469
|
|
|
* @param encryptText |
|
470
|
|
|
* 被签名的字符串 |
|
471
|
|
|
* @param encryptKey |
|
472
|
|
|
* 密钥 |
|
473
|
|
|
* @param compressLen压缩长度 |
|
474
|
|
|
* @return |
|
475
|
|
|
* @throws Exception |
|
476
|
|
|
*/ |
|
477
|
|
|
function hmacMD5EncryptToBase64($encryptText,$encryptKey,$compressLen = 0) { |
|
|
|
|
|
|
478
|
|
|
$encryptResult = Security::hmac_md5($encryptText,$encryptKey); |
|
479
|
|
|
if($compressLen != 0){ |
|
480
|
|
|
$encryptResult = $this->compress($encryptResult,$compressLen); |
|
481
|
|
|
} |
|
482
|
|
|
return base64_encode($this->toStr($encryptResult)); |
|
|
|
|
|
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* 生成滑动窗口 |
|
488
|
|
|
* |
|
489
|
|
|
* @param input |
|
490
|
|
|
* @param slideSize |
|
491
|
|
|
* @return |
|
492
|
|
|
*/ |
|
493
|
|
|
function getSlideWindows($input,$slideSize = 4) |
|
|
|
|
|
|
494
|
|
|
{ |
|
495
|
|
|
$endIndex = 0; |
|
496
|
|
|
$startIndex = 0; |
|
497
|
|
|
$currentWindowSize = 0; |
|
498
|
|
|
$currentWindow = null; |
|
499
|
|
|
$dataLength = $this->utf8_strlen($input); |
|
500
|
|
|
$windows = array(); |
|
501
|
|
|
while($endIndex < $dataLength || $currentWindowSize > $slideSize) |
|
502
|
|
|
{ |
|
503
|
|
|
$startsWithLetterOrDigit = false; |
|
504
|
|
|
if(!empty($currentWindow)){ |
|
505
|
|
|
$startsWithLetterOrDigit = $this->isLetterOrDigit($this->utf8_str_at($currentWindow,0)); |
|
506
|
|
|
} |
|
507
|
|
|
if($endIndex == $dataLength && $startsWithLetterOrDigit == false){ |
|
|
|
|
|
|
508
|
|
|
break; |
|
509
|
|
|
} |
|
510
|
|
|
if($currentWindowSize == $slideSize && |
|
511
|
|
|
$startsWithLetterOrDigit == false && |
|
|
|
|
|
|
512
|
|
|
$this->isLetterOrDigit($this->utf8_str_at($input,$endIndex))) { |
|
513
|
|
|
$endIndex ++; |
|
514
|
|
|
$currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
515
|
|
|
$currentWindowSize = 5; |
|
516
|
|
|
} else { |
|
517
|
|
|
if($endIndex != 0){ |
|
518
|
|
|
if($startsWithLetterOrDigit){ |
|
519
|
|
|
$currentWindowSize -= 1; |
|
520
|
|
|
}else{ |
|
521
|
|
|
$currentWindowSize -= 2; |
|
522
|
|
|
} |
|
523
|
|
|
$startIndex ++; |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
|
|
while ($currentWindowSize < $slideSize && $endIndex < $dataLength) { |
|
527
|
|
|
$currentChar = $this->utf8_str_at($input,$endIndex); |
|
528
|
|
|
if ($this->isLetterOrDigit($currentChar)) { |
|
529
|
|
|
$currentWindowSize += 1; |
|
530
|
|
|
} else { |
|
531
|
|
|
$currentWindowSize += 2; |
|
532
|
|
|
} |
|
533
|
|
|
$endIndex++; |
|
534
|
|
|
} |
|
535
|
|
|
$currentWindow = $this->utf8_substr($input,$startIndex,$endIndex); |
|
536
|
|
|
} |
|
537
|
|
|
array_push($windows,$currentWindow); |
|
538
|
|
|
} |
|
539
|
|
|
return $windows; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
function encryptPhoneIndex($data,$separator,$secretContext) { |
|
|
|
|
|
|
543
|
|
|
$dataLength = strlen($data); |
|
544
|
|
|
if($dataLength < 11) { |
|
545
|
|
|
return $data; |
|
546
|
|
|
} |
|
547
|
|
|
$last4Number = substr($data,$dataLength -4 ,$dataLength); |
|
548
|
|
|
return $separator.$this->hmacMD5EncryptToBase64($last4Number,$secretContext->secret).$separator |
|
549
|
|
|
.Security::encrypt($data,$secretContext->secret).$separator.$secretContext->secretVersion |
|
550
|
|
|
.$separator.$separator; |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
function encryptNormalIndex($data,$compressLen,$slideSize,$separator,$secretContext) { |
|
|
|
|
|
|
554
|
|
|
$slideList = $this->getSlideWindows($data, $slideSize); |
|
555
|
|
|
$builder = ""; |
|
556
|
|
|
foreach ($slideList as $slide) { |
|
557
|
|
|
$builder .= $this->hmacMD5EncryptToBase64($slide,$secretContext->secret,$compressLen); |
|
558
|
|
|
} |
|
559
|
|
|
return $separator.Security::encrypt($data,$secretContext->secret).$separator.$builder.$separator |
|
560
|
|
|
.$secretContext->secretVersion.$separator.$separator; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
function getArrayValue($array,$key,$default) { |
|
|
|
|
|
|
564
|
|
|
if(array_key_exists($key, $array)){ |
|
565
|
|
|
return $array[$key]; |
|
566
|
|
|
} |
|
567
|
|
|
return $default; |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
function getBytes($string) { |
|
|
|
|
|
|
571
|
|
|
$bytes = array(); |
|
572
|
|
|
for($i = 0; $i < strlen($string); $i++){ |
|
573
|
|
|
$bytes[] = ord($string[$i]); |
|
574
|
|
|
} |
|
575
|
|
|
return $bytes; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
function toStr($bytes) { |
|
|
|
|
|
|
579
|
|
|
if(!is_array($bytes)){ |
|
580
|
|
|
return $bytes; |
|
581
|
|
|
} |
|
582
|
|
|
$str = ''; |
|
583
|
|
|
foreach($bytes as $ch) { |
|
584
|
|
|
$str .= chr($ch); |
|
585
|
|
|
} |
|
586
|
|
|
return $str; |
|
587
|
|
|
} |
|
588
|
|
|
} |
|
589
|
|
|
?> |
|
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.