Passed
Push — master ( 99d0e8...9e7049 )
by Ondra
03:35
created

Signer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
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