|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SlevomatEET\Cryptography; |
|
4
|
|
|
|
|
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
|
2 |
|
public function __construct(string $privateKeyFile, string $publicKeyFile, string $privateKeyPassword = '') |
|
18
|
|
|
{ |
|
19
|
2 |
|
$this->privateKeyFile = $privateKeyFile; |
|
20
|
2 |
|
$this->publicKeyFile = $publicKeyFile; |
|
21
|
2 |
|
$this->privateKeyPassword = $privateKeyPassword; |
|
22
|
2 |
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
public function getPkpCode(array $body): string |
|
25
|
|
|
{ |
|
26
|
|
|
$values = [ |
|
27
|
2 |
|
$body['dic_popl'], |
|
28
|
2 |
|
$body['id_provoz'], |
|
29
|
2 |
|
$body['id_pokl'], |
|
30
|
2 |
|
$body['porad_cis'], |
|
31
|
2 |
|
$body['dat_trzby'], |
|
32
|
2 |
|
$body['celk_trzba'], |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
2 |
|
$plaintext = implode('|', $values); |
|
36
|
|
|
|
|
37
|
2 |
|
$privateKey = file_get_contents($this->privateKeyFile); |
|
38
|
2 |
|
$privateKeyId = openssl_pkey_get_private($privateKey, $this->privateKeyPassword); |
|
39
|
2 |
|
if ($privateKeyId === false) { |
|
40
|
1 |
|
throw new PrivateKeyFileException($this->privateKeyFile); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$ok = openssl_sign($plaintext, $signature, $privateKeyId, OPENSSL_ALGO_SHA256); |
|
44
|
1 |
|
if (!$ok) { |
|
45
|
|
|
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 |
|
54
|
|
|
{ |
|
55
|
1 |
|
$bkp = strtoupper(sha1($pkpCode)); |
|
56
|
|
|
|
|
57
|
1 |
|
return implode('-', str_split($bkp, 8)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function addWSESignature(string $request): string |
|
61
|
|
|
{ |
|
62
|
|
|
$securityKey = new \RobRichards\XMLSecLibs\XMLSecurityKey(\RobRichards\XMLSecLibs\XMLSecurityKey::RSA_SHA256, ['type' => 'private']); |
|
63
|
|
|
$document = new \DOMDocument('1.0'); |
|
64
|
|
|
$document->loadXML($request); |
|
65
|
|
|
$wse = new \RobRichards\WsePhp\WSSESoap($document); |
|
66
|
|
|
$securityKey->loadKey($this->privateKeyFile, true); |
|
67
|
|
|
$wse->addTimestamp(); |
|
68
|
|
|
$wse->signSoapDoc($securityKey, ['algorithm' => \RobRichards\XMLSecLibs\XMLSecurityDSig::SHA256]); |
|
69
|
|
|
$binaryToken = $wse->addBinaryToken(file_get_contents($this->publicKeyFile)); |
|
70
|
|
|
$wse->attachTokentoSig($binaryToken); |
|
71
|
|
|
|
|
72
|
|
|
return $wse->saveXML(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|