Signature   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 33
c 2
b 1
f 0
dl 0
loc 93
ccs 0
cts 48
cp 0
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sign() 0 15 3
B _buildParamStr() 0 28 7
A makeSignPlainText() 0 12 1
1
<?php
2
3
namespace Freyo\LaravelQueueCMQ\Queue\Driver;
4
5
use Exception;
6
7
/**
8
 * Sign
9
 * 签名类.
10
 */
11
class Signature
12
{
13
    /**
14
     * sign
15
     * 生成签名.
16
     *
17
     * @param string $srcStr    拼接签名源文字符串
18
     * @param string $secretKey secretKey
19
     * @param string $method    请求方法
20
     *
21
     * @throws Exception
22
     *
23
     * @return string
24
     */
25
    public static function sign($srcStr, $secretKey, $method = 'HmacSHA1')
26
    {
27
        switch ($method) {
28
            case 'HmacSHA1':
29
                $retStr = base64_encode(hash_hmac('sha1', $srcStr, $secretKey, true));
30
                break;
31
            case 'HmacSHA256':
32
                $retStr = base64_encode(hash_hmac('sha256', $srcStr, $secretKey, true));
33
                break;
34
            default:
35
                throw new Exception($method.' is not a supported encrypt method');
36
                break;
37
        }
38
39
        return $retStr;
40
    }
41
42
    /**
43
     * makeSignPlainText
44
     * 生成拼接签名源文字符串.
45
     *
46
     * @param array  $requestParams 请求参数
47
     * @param string $requestMethod 请求方法
48
     * @param string $requestHost   接口域名
49
     * @param string $requestPath   url路径
50
     *
51
     * @return string
52
     */
53
    public static function makeSignPlainText($requestParams,
54
        $requestMethod = 'POST', $requestHost = '',
55
        $requestPath = '/v2/index.php')
56
    {
57
        $url = $requestHost.$requestPath;
58
59
        // 取出所有的参数
60
        $paramStr = self::_buildParamStr($requestParams, $requestMethod);
61
62
        $plainText = $requestMethod.$url.$paramStr;
63
64
        return $plainText;
65
    }
66
67
    /**
68
     * _buildParamStr
69
     * 拼接参数.
70
     *
71
     * @param array  $requestParams 请求参数
72
     * @param string $requestMethod 请求方法
73
     *
74
     * @return string
75
     */
76
    protected static function _buildParamStr($requestParams, $requestMethod = 'POST')
77
    {
78
        $paramStr = '';
79
        ksort($requestParams);
80
        $i = 0;
81
        foreach ($requestParams as $key => $value) {
82
            if ($key == 'Signature') {
83
                continue;
84
            }
85
            // 排除上传文件的参数
86
            if ($requestMethod == 'POST' && substr($value, 0, 1) == '@') {
87
                continue;
88
            }
89
            // 把 参数中的 _ 替换成 .
90
            if (strpos($key, '_')) {
91
                $key = str_replace('_', '.', $key);
92
            }
93
94
            if ($i == 0) {
95
                $paramStr .= '?';
96
            } else {
97
                $paramStr .= '&';
98
            }
99
            $paramStr .= $key.'='.$value;
100
            $i++;
101
        }
102
103
        return $paramStr;
104
    }
105
}
106