1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Pixidos package. |
5
|
|
|
* |
6
|
|
|
* (c) Ondra Votava <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Pixidos\GPWebPay\Signer; |
16
|
|
|
|
17
|
|
|
use Pixidos\GPWebPay\Exceptions\SignerException; |
18
|
|
|
use Pixidos\GPWebPay\Signer\Key\PrivateKey; |
19
|
|
|
use Pixidos\GPWebPay\Signer\Key\PublicKey; |
20
|
|
|
use Stringable; |
21
|
|
|
|
22
|
|
|
class Signer implements SignerInterface |
23
|
|
|
{ |
24
|
18 |
|
public function __construct( |
25
|
|
|
private readonly PrivateKey $privateKey, |
26
|
|
|
private readonly PublicKey $publicKey, |
27
|
|
|
private readonly string|int $algorithm = OPENSSL_ALGO_SHA1 |
28
|
|
|
) { |
29
|
18 |
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
9 |
|
public function sign(array $params): string |
33
|
|
|
{ |
34
|
9 |
|
$digestText = implode('|', $params); |
35
|
|
|
|
36
|
9 |
|
openssl_sign($digestText, $digest, $this->privateKey->getKey(), $this->algorithm); |
37
|
|
|
// @codeCoverageIgnoreStart |
38
|
|
|
if (!is_string($digest)) { |
39
|
|
|
throw new SignerException('Unable to sign data'); |
40
|
|
|
} |
41
|
|
|
// @codeCoverageIgnoreEnd |
42
|
|
|
|
43
|
9 |
|
return base64_encode($digest); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
13 |
|
public function verify(array $params, string $digest): bool |
48
|
|
|
{ |
49
|
13 |
|
$data = implode('|', $params); |
50
|
13 |
|
$decode = (string)base64_decode($digest, true); |
51
|
13 |
|
$ok = openssl_verify($data, $decode, $this->publicKey->getKey(), $this->algorithm); |
52
|
|
|
|
53
|
13 |
|
return 1 === $ok; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|