| @@ 18-69 (lines=52) @@ | ||
| 15 | * @author Vuong Minh <[email protected]> |
|
| 16 | * @since 1.0 |
|
| 17 | */ |
|
| 18 | class HmacDataSignature extends DataSignature |
|
| 19 | { |
|
| 20 | ||
| 21 | /** |
|
| 22 | * Tên loại mã hóa. Ví dụ: md5, sha1, sha256... |
|
| 23 | * |
|
| 24 | * @var string |
|
| 25 | */ |
|
| 26 | public $hmacAlgo; |
|
| 27 | ||
| 28 | /** |
|
| 29 | * Khóa mã hóa. Độ phức tạp càng cao thì dữ liệu càng được an toàn. |
|
| 30 | * |
|
| 31 | * @var string |
|
| 32 | */ |
|
| 33 | public $key; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @inheritdoc |
|
| 37 | * @throws InvalidConfigException |
|
| 38 | */ |
|
| 39 | public function init() |
|
| 40 | { |
|
| 41 | if ($this->key === null) { |
|
| 42 | throw new InvalidConfigException('Property `key` must be set for generate signature!'); |
|
| 43 | } elseif ($this->hmacAlgo === null) { |
|
| 44 | throw new InvalidConfigException('Property `hmacAlgo` must be set for generate signature!'); |
|
| 45 | } |
|
| 46 | ||
| 47 | parent::init(); |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * @inheritdoc |
|
| 52 | */ |
|
| 53 | public function generate(): string |
|
| 54 | { |
|
| 55 | return hash_hmac($this->hmacAlgo, $this->getData(), $this->key); |
|
| 56 | } |
|
| 57 | ||
| 58 | /** |
|
| 59 | * @inheritdoc |
|
| 60 | * @throws InvalidConfigException |
|
| 61 | */ |
|
| 62 | public function validate(string $expect): bool |
|
| 63 | { |
|
| 64 | $actual = $this->generate(); |
|
| 65 | ||
| 66 | return strcasecmp($expect, $actual) === 0; |
|
| 67 | } |
|
| 68 | ||
| 69 | } |
|
| 70 | ||
| @@ 22-61 (lines=40) @@ | ||
| 19 | * @since 1.0 |
|
| 20 | * @deprecated since 1.0.2 we use `yiiviet\payment\HashDataSignature` instead. |
|
| 21 | */ |
|
| 22 | class DataSignature extends BaseDataSignature |
|
| 23 | { |
|
| 24 | ||
| 25 | /** |
|
| 26 | * @var string |
|
| 27 | */ |
|
| 28 | public $hashSecret; |
|
| 29 | ||
| 30 | /** |
|
| 31 | * @var string |
|
| 32 | */ |
|
| 33 | public $hashAlgo; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @inheritdoc |
|
| 37 | * @throws InvalidConfigException |
|
| 38 | */ |
|
| 39 | public function generate(): string |
|
| 40 | { |
|
| 41 | if ($this->hashSecret === null) { |
|
| 42 | throw new InvalidConfigException('Property `hashSecret` must be set!'); |
|
| 43 | } elseif ($this->hashAlgo === null) { |
|
| 44 | throw new InvalidConfigException('Property `hashAlgo` must be set!'); |
|
| 45 | } |
|
| 46 | ||
| 47 | return hash($this->hashAlgo, $this->hashSecret . $this->getData()); |
|
| 48 | } |
|
| 49 | ||
| 50 | /** |
|
| 51 | * @throws InvalidConfigException |
|
| 52 | * @inheritdoc |
|
| 53 | */ |
|
| 54 | public function validate(string $expect): bool |
|
| 55 | { |
|
| 56 | $actual = $this->generate(); |
|
| 57 | ||
| 58 | return strcasecmp($expect, $actual) === 0; |
|
| 59 | } |
|
| 60 | ||
| 61 | } |
|
| 62 | ||