1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PimpayBundle\Handler; |
4
|
|
|
|
5
|
|
|
use PimpayBundle\Exception\HashNotSupportedException; |
6
|
|
|
use PimpayBundle\Exception\OpenSslNotInstalledException; |
7
|
|
|
use PimpayBundle\Service\SoapClientService; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class OpenSslСryptoHandler |
11
|
|
|
* @package PimpayBundle\Handler |
12
|
|
|
*/ |
13
|
|
|
class OpenSslCryptoHandler implements CryptoHandlerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
const SUPPORT_HASH = [ |
19
|
|
|
'SHA', |
20
|
|
|
'SHA1', |
21
|
|
|
'SHA224', |
22
|
|
|
'SHA256', |
23
|
|
|
'SHA384', |
24
|
|
|
'SHA512', |
25
|
|
|
'DSA', |
26
|
|
|
'DSA-SHA', |
27
|
|
|
'ecdsa-with-SHA1', |
28
|
|
|
'MD4', |
29
|
|
|
'MD5', |
30
|
|
|
'RIPEMD160', |
31
|
|
|
'whirlpool', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $pathKey; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $hash; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $passphrase; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* OpenSslCryptoHandler constructor. |
51
|
|
|
* @param array $params |
52
|
|
|
* @throws HashNotSupportedException |
53
|
|
|
* @throws OpenSslNotInstalledException |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $params) |
56
|
|
|
{ |
57
|
|
|
if (!extension_loaded('openssl')) { |
58
|
|
|
throw new OpenSslNotInstalledException('OpenSSL extension is not installed.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!in_array($params['hash'], self::SUPPORT_HASH, true)) { |
62
|
|
|
throw new HashNotSupportedException("Unsupported OpenSSL type hash {$params['hash']}."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->pathKey = $params['path_key']; |
66
|
|
|
$this->passphrase = $params['passphrase']; |
67
|
|
|
$this->hash = $params['hash']; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $data |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function sign(string $data): string |
75
|
|
|
{ |
76
|
|
|
$signature = ''; |
77
|
|
|
$privateKey = openssl_pkey_get_private('file://'.$this->pathKey, $this->passphrase); |
78
|
|
|
openssl_sign($data, $signature, $privateKey, $this->hash); |
79
|
|
|
|
80
|
|
|
return base64_encode($signature); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param SoapClientService $soapClient |
85
|
|
|
* @param string $request |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function injectSignature(SoapClientService $soapClient, string $request): string |
89
|
|
|
{ |
90
|
|
|
$signature = $this->sign($request); |
91
|
|
|
stream_context_set_option($soapClient->getStreamContext(), 'http', 'header', 'X-Request-Signature: '.$signature); |
92
|
|
|
|
93
|
|
|
return $request; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|