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
|
<?php
namespace App\Services;
// 第三方平台请求的签名校验
class AuthSign
{
/**
* 签名校验
* @param string $signType 签名类型,对接哪个第三方系统的标识
* @param string $sign 签名
* @param int $requestTime 请求时间戳
* @return array
*/
public static function checkSign(string $signType, string $sign, int $requestTime): array
{
// 签名类型:新增对外接口签名类型时,需要去配置新的签名类型和密钥
$signSecret = config('sign.sign_type_secret')[$signType] ?? '';
if ($signSecret == '') {
return ['result' => false, 'msg' => '该签名类型还没配置'];
}
if ($sign != md5("{$requestTime}-{$signType}-{$signSecret}")) {
return ['result' => false, 'msg' => '签名有误'];
}
$currentTime = time();
if ($currentTime < $requestTime || ($currentTime-$requestTime) > config('sign.sign_ttl')) {
return ['result' => false, 'msg' => '签名已过期'];
}
return ['result' => true, 'msg' => '签名校验成功'];
}
/**
* 生成签名
* @param string $signType 签名类型,对接哪个第三方系统的标识
* @return array
*/
public static function getSign(string $signType): array
{
$signSecret = config('sign.sign_type_secret')[$signType] ?? '';
if ($signSecret == '') {
return ['result' => false, 'msg' => '该签名类型还没配置'];
}
$requestTime = time();
return ['result' => true, 'time' => $requestTime, 'sign' => md5("{$requestTime}-{$signType}-{$signSecret}")];
}
}
|