1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyPay; |
4
|
|
|
|
5
|
|
|
use EasyPay\Log as Log; |
6
|
|
|
use EasyPay\Exception; |
7
|
|
|
use EasyPay\Key as Key; |
8
|
|
|
use EasyPay\OpenSSL as OpenSSL; |
9
|
|
|
|
10
|
|
|
class Sign |
11
|
|
|
{ |
12
|
|
|
public function __construct() {} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Verify signature of request |
16
|
|
|
* |
17
|
|
|
* @param string $request_str |
18
|
|
|
* @param string $sign |
19
|
|
|
* @param array $options |
20
|
|
|
*/ |
21
|
|
|
public function verify($request_str, $sign, $options) |
22
|
|
|
{ |
23
|
|
|
if (isset($options['UseSign']) && ($options['UseSign'] === true)) |
24
|
|
|
{ |
25
|
|
|
$this->check_verify_sign_result( |
26
|
|
|
$result = (new OpenSSL())->verify( |
27
|
|
|
str_replace($sign, '', $request_str), |
28
|
|
|
pack("H*", $sign), |
29
|
|
|
(new OpenSSL())->get_pub_key($this->get_pub_key($options)) |
30
|
|
|
) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* load file with easysoft public key |
37
|
|
|
* |
38
|
|
|
* @param array $options |
39
|
|
|
* @throws Exception\Runtime |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
protected function get_pub_key($options) |
43
|
|
|
{ |
44
|
|
|
if ( ! isset($options['EasySoftPKey'])) |
45
|
|
|
{ |
46
|
|
|
throw new Exception\Runtime('The parameter EasySoftPKey is not set!', -94); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return (new Key())->get($options['EasySoftPKey'], 'public'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* check result of openssl verify signature |
54
|
|
|
* |
55
|
|
|
* @param integer $result |
56
|
|
|
* @throws Exception\Sign |
57
|
|
|
*/ |
58
|
|
|
protected function check_verify_sign_result($result) |
59
|
|
|
{ |
60
|
|
|
if ($result == -1) |
61
|
|
|
{ |
62
|
|
|
throw new Exception\Sign('Error verify signature of request!', -96); |
63
|
|
|
} |
64
|
|
|
elseif ($result == 0) |
65
|
|
|
{ |
66
|
|
|
throw new Exception\Sign('Signature of request is incorrect!', -95); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate signature of response |
72
|
|
|
* |
73
|
|
|
* @param string $request_str |
74
|
|
|
* @param array $options |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function generate($request_str, $options) |
78
|
|
|
{ |
79
|
|
|
try |
80
|
|
|
{ |
81
|
|
|
$sign = ''; |
82
|
|
|
$this->check_generate_sign_result( |
83
|
|
|
$result = (new OpenSSL())->sign( |
84
|
|
|
$request_str, |
85
|
|
|
$sign, |
86
|
|
|
(new OpenSSL())->get_priv_key($this->get_priv_key($options)) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
return strtoupper(bin2hex($sign)); |
91
|
|
|
} |
92
|
|
|
catch (\Exception $e) |
93
|
|
|
{ |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* load file with provider private key |
100
|
|
|
* |
101
|
|
|
* @param array $options |
102
|
|
|
* @throws Exception\Runtime |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function get_priv_key($options) |
106
|
|
|
{ |
107
|
|
|
if ( ! isset($options['ProviderPKey'])) |
108
|
|
|
{ |
109
|
|
|
throw new \EasyPay\Exception\Runtime('The parameter ProviderPKey is not set!', -94); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return (new Key())->get($options['ProviderPKey'], 'private'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* check result of openssl sign |
117
|
|
|
* |
118
|
|
|
* @param bool $result |
119
|
|
|
* @throws Exception\Sign |
120
|
|
|
*/ |
121
|
|
|
protected function check_generate_sign_result($result) |
122
|
|
|
{ |
123
|
|
|
if ($result === FALSE) |
124
|
|
|
{ |
125
|
|
|
throw new \EasyPay\Exception\Sign('Can not generate signature!', -96); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|