Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

AbstractSigner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Alg\Signature;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XMLSecurity\Backend\SignatureBackend;
9
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
10
use SimpleSAML\XMLSecurity\Key\KeyInterface;
11
12
/**
13
 * An abstract class that implements a generic digital signature algorithm.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
abstract class AbstractSigner implements SignatureAlgorithmInterface
18
{
19
    /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend */
20
    protected SignatureBackend $backend;
21
22
23
    /**
24
     * Build a signature algorithm.
25
     *
26
     * Extend this class to implement your own signers.
27
     *
28
     * WARNING: remember to adjust the type of the key to the one that works with your algorithm!
29
     *
30
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The signing key.
31
     * @param string $algId The identifier of this algorithm.
32
     * @param string $digest The identifier of the digest algorithm to use.
33
     */
34
    public function __construct(
35
        private KeyInterface $key,
36
        protected string $algId,
37
        protected string $digest,
38
    ) {
39
        Assert::oneOf(
40
            $algId,
41
            static::getSupportedAlgorithms(),
42
            sprintf('Unsupported algorithm for %s', static::class),
43
            UnsupportedAlgorithmException::class,
44
        );
45
46
        $this->backend = new (static::DEFAULT_BACKEND)();
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '(' on line 46 at column 29
Loading history...
47
        $this->backend->setDigestAlg($digest);
48
    }
49
50
51
    /**
52
     * @return string
53
     */
54
    public function getAlgorithmId(): string
55
    {
56
        return $this->algId;
57
    }
58
59
60
    /**
61
     * @return string
62
     */
63
    public function getDigest(): string
64
    {
65
        return $this->digest;
66
    }
67
68
69
    /**
70
     * @return \SimpleSAML\XMLSecurity\Key\KeyInterface
71
     */
72
    public function getKey(): KeyInterface
73
    {
74
        return $this->key;
75
    }
76
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function setBackend(?SignatureBackend $backend): void
82
    {
83
        if ($backend === null) {
84
            return;
85
        }
86
87
        $this->backend = $backend;
88
        $this->backend->setDigestAlg($this->digest);
89
    }
90
91
92
    /**
93
     * Sign a given plaintext with the current algorithm and key.
94
     *
95
     * @param string $plaintext The plaintext to sign.
96
     *
97
     * @return string The (binary) signature corresponding to the given plaintext.
98
     */
99
    final public function sign(string $plaintext): string
100
    {
101
        return $this->backend->sign($this->key, $plaintext);
102
    }
103
104
105
    /**
106
     * Verify a signature with the current algorithm and key.
107
     *
108
     * @param string $plaintext The original signed text.
109
     * @param string $signature The (binary) signature to verify.
110
     *
111
     * @return boolean True if the signature can be verified, false otherwise.
112
     */
113
    final public function verify(string $plaintext, string $signature): bool
114
    {
115
        return $this->backend->verify($this->key, $plaintext, $signature);
116
    }
117
}
118