Signature   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generate() 0 6 1
A validate() 0 6 1
1
<?php
2
/**
3
 * @link https://github.com/phpviet/omnipay-momo
4
 * @copyright (c) PHP Viet
5
 * @license [MIT](http://www.opensource.org/licenses/MIT)
6
 */
7
8
namespace Omnipay\MoMo\Support;
9
10
/**
11
 * @author Vuong Minh <[email protected]>
12
 * @since 1.0.0
13
 */
14
class Signature
15
{
16
    /**
17
     * Khóa bí mật dùng để tạo và kiểm tra chữ ký dữ liệu.
18
     *
19
     * @var string
20
     */
21
    protected $secretKey;
22
23
    /**
24
     * Khởi tạo đối tượng DataSignature.
25
     *
26
     * @param  string  $secretKey
27
     */
28
    public function __construct(string $secretKey)
29
    {
30
        $this->secretKey = $secretKey;
31
    }
32
33
    /**
34
     * Trả về chữ ký dữ liệu của dữ liệu truyền vào.
35
     *
36
     * @param  array  $data
37
     * @return string
38
     */
39
    public function generate(array $data): string
40
    {
41
        $data = urldecode(http_build_query($data));
42
43
        return hash_hmac('sha256', $data, $this->secretKey);
44
    }
45
46
    /**
47
     * Kiểm tra tính hợp lệ của chữ ký dữ liệu so với dữ liệu truyền vào.
48
     *
49
     * @param  array  $data
50
     * @param  string  $expect
51
     * @return bool
52
     */
53
    public function validate(array $data, string $expect): bool
54
    {
55
        $actual = $this->generate($data);
56
57
        return 0 === strcasecmp($expect, $actual);
58
    }
59
}
60