AbstractRsaSigner::sign()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Cryptography\Algorithms\Rsa;
6
7
use MiladRahimi\Jwt\Cryptography\Keys\RsaPrivateKey;
8
use MiladRahimi\Jwt\Cryptography\Signer;
9
use MiladRahimi\Jwt\Exceptions\SigningException;
10
11
class AbstractRsaSigner implements Signer
12
{
13
    use Algorithm;
14
15
    protected RsaPrivateKey $privateKey;
16
17
    public function __construct(RsaPrivateKey $publicKey)
18
    {
19
        $this->privateKey = $publicKey;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function sign(string $message): string
26
    {
27
        $signature = '';
28
29
        if (openssl_sign($message, $signature, $this->privateKey->getResource(), $this->algorithm()) === true) {
30
            return $signature;
31
        }
32
33
        throw new SigningException(openssl_error_string() ?: "OpenSSL cannot sign the token.");
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function kid(): ?string
40
    {
41
        return $this->privateKey->getId();
42
    }
43
44
    public function getPrivateKey(): RsaPrivateKey
45
    {
46
        return $this->privateKey;
47
    }
48
}
49