Passed
Push — master ( 2e6afe...f2d03e )
by Tim
02:27
created

X509Digest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XMLSecurity\Constants;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
13
/**
14
 * Class representing a ds:X509Digest element.
15
 *
16
 * @package simplesaml/xml-security
17
 */
18
final class X509Digest extends AbstractDsElement
19
{
20
    /**
21
     * The digest.
22
     *
23
     * @var string
24
     */
25
    protected string $digest;
26
27
28
    /**
29
     * The digest algorithm.
30
     *
31
     * @var string
32
     */
33
    protected string $algorithm;
34
35
36
    /**
37
     * Initialize a X509Digest element.
38
     *
39
     * @param string $digest
40
     * @param string $algorithm
41
     */
42
    public function __construct(string $digest, string $algorithm)
43
    {
44
        $this->setDigest($digest);
45
        $this->setAlgorithm($algorithm);
46
    }
47
48
49
    /**
50
     * Collect the value of the digest-property
51
     *
52
     * @return string
53
     */
54
    public function getDigest(): string
55
    {
56
        return $this->digest;
57
    }
58
59
60
    /**
61
     * Set the value of the digest-property
62
     *
63
     * @param string $digest
64
     */
65
    private function setDigest(string $digest): void
66
    {
67
        Assert::notEmpty($digest, 'X509Digest cannot be empty');
68
        Assert::stringPlausibleBase64($digest, 'ds:X509Digest is not a valid Base64 encoded string');
69
70
        $this->digest = $digest;
71
    }
72
73
74
    /**
75
     * Collect the value of the algorithm-property
76
     *
77
     * @return string
78
     */
79
    public function getAlgorithm(): string
80
    {
81
        return $this->algorithm;
82
    }
83
84
85
    /**
86
     * Set the value of the algorithm-property
87
     *
88
     * @param string $algorithm
89
     */
90
    private function setAlgorithm(string $algorithm): void
91
    {
92
        Assert::oneOf(
93
            $algorithm,
94
            [
95
                Constants::DIGEST_SHA1,
96
                Constants::DIGEST_SHA224,
97
                Constants::DIGEST_SHA256,
98
                Constants::DIGEST_SHA384,
99
                Constants::DIGEST_SHA512,
100
                Constants::DIGEST_RIPEMD160,
101
            ],
102
            'Invalid digest method',
103
            InvalidArgumentException::class
104
        );
105
106
        $this->algorithm = $algorithm;
107
    }
108
109
110
    /**
111
     * Convert XML into a X509Digest
112
     *
113
     * @param \DOMElement $xml The XML element we should load
114
     * @return self
115
     *
116
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
117
     *   If the qualified name of the supplied element is wrong
118
     */
119
    public static function fromXML(DOMElement $xml): object
120
    {
121
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
122
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
123
124
        $algorithm = self::getAttribute($xml, 'Algorithm');
125
126
        $digest = $xml->textContent;
127
        Assert::stringPlausibleBase64($digest);
128
129
        return new self($digest, $algorithm);
130
    }
131
132
133
    /**
134
     * Convert this X509Digest element to XML.
135
     *
136
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
137
     * @return \DOMElement
138
     */
139
    public function toXML(DOMElement $parent = null): DOMElement
140
    {
141
        $e = $this->instantiateParentElement($parent);
142
143
        $e->textContent = $this->digest;
144
        $e->setAttribute('Algorithm', $this->algorithm);
145
146
        return $e;
147
    }
148
}
149