Passed
Pull Request — master (#2)
by Jaime Pérez
02:11
created

AbstractSigner::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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