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

Signer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 10
c 4
b 0
f 2
dl 0
loc 32
ccs 11
cts 11
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sign() 0 12 2
A __construct() 0 5 1
A verify() 0 7 1
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