1 | <?php |
||
31 | class Encryptor |
||
32 | { |
||
33 | /** |
||
34 | * App id. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $appId; |
||
39 | |||
40 | /** |
||
41 | * App token. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $token; |
||
46 | |||
47 | /** |
||
48 | * AES key. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $AESKey; |
||
53 | |||
54 | /** |
||
55 | * Block size. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $blockSize; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param string $appId |
||
65 | * @param string $token |
||
66 | * @param string $AESKey |
||
67 | * |
||
68 | * @throws RuntimeException |
||
69 | */ |
||
70 | public function __construct($appId, $token, $AESKey) |
||
81 | |||
82 | /** |
||
83 | * Encrypt the message and return XML. |
||
84 | * |
||
85 | * @param string $xml |
||
86 | * @param string $nonce |
||
87 | * @param int $timestamp |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function encryptMsg($xml, $nonce = null, $timestamp = null) |
||
111 | |||
112 | /** |
||
113 | * Decrypt message. |
||
114 | * |
||
115 | * @param string $msgSignature |
||
116 | * @param string $nonce |
||
117 | * @param string $timestamp |
||
118 | * @param string $postXML |
||
119 | * |
||
120 | * @return array |
||
121 | * |
||
122 | * @throws EncryptionException |
||
123 | */ |
||
124 | public function decryptMsg($msgSignature, $nonce, $timestamp, $postXML) |
||
142 | |||
143 | /** |
||
144 | * Get SHA1. |
||
145 | * |
||
146 | * @return string |
||
147 | * |
||
148 | * @throws EncryptionException |
||
149 | */ |
||
150 | public function getSHA1() |
||
161 | |||
162 | /** |
||
163 | * Encode string. |
||
164 | * |
||
165 | * @param string $text |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function encode($text) |
||
185 | |||
186 | /** |
||
187 | * Decode string. |
||
188 | * |
||
189 | * @param string $decrypted |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function decode($decrypted) |
||
203 | |||
204 | /** |
||
205 | * Return AESKey. |
||
206 | * |
||
207 | * @return string |
||
208 | * |
||
209 | * @throws InvalidConfigException |
||
210 | */ |
||
211 | protected function getAESKey() |
||
219 | |||
220 | /** |
||
221 | * Encrypt string. |
||
222 | * |
||
223 | * @param string $text |
||
224 | * @param string $appId |
||
225 | * |
||
226 | * @return string |
||
227 | * |
||
228 | * @throws EncryptionException |
||
229 | */ |
||
230 | private function encrypt($text, $appId) |
||
246 | |||
247 | /** |
||
248 | * Decrypt message. |
||
249 | * |
||
250 | * @param string $encrypted |
||
251 | * @param string $appId |
||
252 | * |
||
253 | * @return string |
||
254 | * |
||
255 | * @throws EncryptionException |
||
256 | */ |
||
257 | private function decrypt($encrypted, $appId) |
||
291 | |||
292 | /** |
||
293 | * Generate random string. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | private function getRandomStr() |
||
301 | } |
||
302 |