1 | <?php declare(strict_types = 1); |
||
5 | class CryptographyService |
||
6 | { |
||
7 | |||
8 | /** @var string */ |
||
9 | private $privateKeyFile; |
||
10 | |||
11 | /** @var string */ |
||
12 | private $privateKeyPassword; |
||
13 | |||
14 | /** @var string */ |
||
15 | private $publicKeyFile; |
||
16 | |||
17 | 6 | public function __construct(string $privateKeyFile, string $publicKeyFile, string $privateKeyPassword = '') |
|
23 | |||
24 | 3 | public function getPkpCode(array $body): string |
|
25 | { |
||
26 | $values = [ |
||
27 | 3 | $body['dic_popl'], |
|
28 | 3 | $body['id_provoz'], |
|
29 | 3 | $body['id_pokl'], |
|
30 | 3 | $body['porad_cis'], |
|
31 | 3 | $body['dat_trzby'], |
|
32 | 3 | $body['celk_trzba'], |
|
33 | ]; |
||
34 | |||
35 | 3 | $plaintext = implode('|', $values); |
|
36 | |||
37 | 3 | $privateKey = file_get_contents($this->privateKeyFile); |
|
38 | 3 | $privateKeyId = openssl_pkey_get_private($privateKey, $this->privateKeyPassword); |
|
39 | 3 | if ($privateKeyId === false) { |
|
40 | 1 | throw new PrivateKeyFileException($this->privateKeyFile); |
|
41 | } |
||
42 | |||
43 | 2 | $ok = openssl_sign($plaintext, $signature, $privateKeyId, OPENSSL_ALGO_SHA256); |
|
44 | 2 | if (!$ok) { |
|
45 | 1 | throw new SigningFailedException($values); |
|
46 | } |
||
47 | |||
48 | 1 | openssl_free_key($privateKeyId); |
|
49 | |||
50 | 1 | return $signature; |
|
51 | } |
||
52 | |||
53 | 1 | public function getBkpCode(string $pkpCode): string |
|
59 | |||
60 | 3 | public function addWSESignature(string $request): string |
|
75 | |||
76 | } |
||
77 |