Completed
Push — master ( 4b202c...dc308f )
by Vuong
02:05
created

HashDataSignature::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-payment
4
 * @copyright Copyright (c) 2017 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
9
namespace yiiviet\payment;
10
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * Lớp HashDataSignature hổ trợ tạo chữ ký dữ liệu thông qua các kiểu mã hóa hash 1 chiều như md5, sha1, sha256...
15
 *
16
 * @author Vuong Minh <[email protected]>
17
 * @since 1.0.2
18
 */
19
class HashDataSignature extends DataSignature
20
{
21
22
    public $hashAlgo;
23
24
    /**
25
     * @inheritdoc
26
     * @throws InvalidConfigException
27
     */
28 3
    public function init()
29
    {
30 3
        if ($this->hashAlgo === null) {
31
            throw new InvalidConfigException('Property `hashAlgo` must be set!');
32
        }
33
34 3
        parent::init();
35 3
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40 3
    public function generate(): string
41
    {
42 3
        return hash($this->hashAlgo, $this->getData());
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function validate(string $expect): bool
49
    {
50
        return strcmp($expect, $this->generate()) === 0;
51
    }
52
53
}
54