-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignUtils.php
More file actions
executable file
·66 lines (57 loc) · 1.75 KB
/
SignUtils.php
File metadata and controls
executable file
·66 lines (57 loc) · 1.75 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
<?php
/*******************************************************************************
* Copyright (c) 2022. Ankio. All Rights Reserved.
******************************************************************************/
/**
* Package: app\utils
* Class SignUtils
* Created By ankio.
* Date : 2023/4/28
* Time : 14:36
* Description :
*/
namespace library\login;
use cleanphp\App;
use cleanphp\file\Log;
class SignUtils
{
public static function checkSign(array $args, $key): bool
{
if (!isset($args['sign'])) return false;
$sign = trim($args['sign']);
unset($args['sign']);
return $sign === self::getSign($args, $key);
}
private static function getSign($args, $secretKey): string
{
foreach ($args as $key => &$val) {
if (empty($val)) unset($args[$key]);
$val = strval($val);
}
ksort($args);
$String = self::formatBizQueryParaMap($args);
$String = $String . "&key=" . $secretKey;
App::$debug && Log::record("SignUtils","$String",Log::TYPE_WARNING);
return strtoupper(hash('sha256', $String));
}
private static function formatBizQueryParaMap($paraMap): string
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
$buff .= $k . "=" . $v . "&";
}
$reqPar = "";
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
public static function sign(array $array, $key): array
{
App::$debug && Log::record("SignUtils",print_r($array,true));
App::$debug && Log::record("SignUtils","Key: $key");
$array['sign'] = self::getSign($array, $key);
return $array;
}
}