Passed
Pull Request — master (#3)
by Tim
02:26
created

X509Digest::setAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
11
/**
12
 * Class representing a ds:X509Digest element.
13
 *
14
 * @package simplesaml/xml-security
15
 */
16
final class X509Digest extends AbstractDsElement
17
{
18
    /**
19
     * The digest.
20
     *
21
     * @var string
22
     */
23
    protected string $digest;
24
25
26
    /**
27
     * The digest algorithm.
28
     *
29
     * @var string
30
     */
31
    protected string $algorithm;
32
33
34
    /**
35
     * Initialize a X509Digest element.
36
     *
37
     * @param string $digest
38
     * @param string $algorithm
39
     */
40
    public function __construct(string $digest, string $algorithm)
41
    {
42
        $this->setDigest($digest);
43
        $this->setAlgorithm($algorithm);
44
    }
45
46
47
    /**
48
     * Collect the value of the digest-property
49
     *
50
     * @return string
51
     */
52
    public function getDigest(): string
53
    {
54
        return $this->digest;
55
    }
56
57
58
    /**
59
     * Set the value of the digest-property
60
     *
61
     * @param string $digest
62
     */
63
    private function setDigest(string $digest): void
64
    {
65
        $this->digest = $digest;
66
    }
67
68
69
    /**
70
     * Collect the value of the algorithm-property
71
     *
72
     * @return string
73
     */
74
    public function getAlgorithm(): string
75
    {
76
        return $this->algorithm;
77
    }
78
79
80
    /**
81
     * Set the value of the algorithm-property
82
     *
83
     * @param string $algorithm
84
     */
85
    private function setAlgorithm(string $algorithm): void
86
    {
87
        $this->algorithm = $algorithm;
88
    }
89
90
91
    /**
92
     * Convert XML into a X509Digest
93
     *
94
     * @param \DOMElement $xml The XML element we should load
95
     * @return self
96
     *
97
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
98
     *   If the qualified name of the supplied element is wrong
99
     */
100
    public static function fromXML(DOMElement $xml): object
101
    {
102
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
103
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
104
105
        $digest = $xml->textContent;
106
        $algorithm = self::getAttribute($xml, 'Algorithm');
107
108
        Assert::stringNotEmpty($xml->textContent, 'Missing value digest.');
109
        /**
110
         * Note: This test is not watertight but prevents a string containing illegal characters
111
         * from being passed and ensures the string roughly follows the correct format for a Base64 encoded string
112
         */
113
        Assert::string(
114
            filter_var(
115
                $digest,
116
                FILTER_VALIDATE_REGEXP,
117
                [
118
                    'options' => [
119
                        'regexp' => '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/'
120
                    ]
121
                ]
122
            ),
123
            'Digest is not a valid Base64 encoded string'
124
        );
125
        return new self($digest, $algorithm);
126
    }
127
128
129
    /**
130
     * Convert this X509Digest element to XML.
131
     *
132
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
133
     * @return \DOMElement
134
     */
135
    public function toXML(DOMElement $parent = null): DOMElement
136
    {
137
        $e = $this->instantiateParentElement($parent);
138
139
        $e->textContent = $this->digest;
140
        $e->setAttribute('Algorithm', $this->algorithm);
141
142
        return $e;
143
    }
144
}
145