|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link https://github.com/phpviet/omnipay-onepay |
| 4 | + * @copyright (c) PHP Viet |
| 5 | + * @license [MIT](http://www.opensource.org/licenses/MIT) |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Omnipay\OnePay\Support; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Vuong Minh <vuongxuongminh@gmail.com> |
| 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 $hashKey; |
| 22 | + |
| 23 | + /** |
| 24 | + * Khởi tạo đối tượng DataSignature. |
| 25 | + * |
| 26 | + * @param string $hashKey |
| 27 | + */ |
| 28 | + public function __construct(string $hashKey) |
| 29 | + { |
| 30 | + $this->hashKey = pack('H*', $hashKey); |
| 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 strtoupper(hash_hmac('sha256', $data, $this->hashKey)); |
| 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 | +} |
0 commit comments