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:DigestValue element. |
13
|
|
|
* |
14
|
|
|
* @package simplesaml/xml-security |
15
|
|
|
*/ |
16
|
|
|
final class DigestValue extends AbstractDsElement |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The digest value. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected string $digest; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initialize a DigestValue element. |
28
|
|
|
* |
29
|
|
|
* @param string $digest |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $digest) |
32
|
|
|
{ |
33
|
|
|
$this->setDigest($digest); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Collect the value of the digest-property |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getDigest(): string |
43
|
|
|
{ |
44
|
|
|
return $this->digest; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the value of the digest-property |
50
|
|
|
* |
51
|
|
|
* @param string $digest |
52
|
|
|
*/ |
53
|
|
|
private function setDigest(string $digest): void |
54
|
|
|
{ |
55
|
|
|
Assert::stringPlausibleBase64($digest, 'ds:DigestValue is not a valid Base64 encoded string'); |
56
|
|
|
$this->digest = $digest; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert XML into a DigestValue |
62
|
|
|
* |
63
|
|
|
* @param \DOMElement $xml The XML element we should load |
64
|
|
|
* @return self |
65
|
|
|
* |
66
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
67
|
|
|
* If the qualified name of the supplied element is wrong |
68
|
|
|
*/ |
69
|
|
|
public static function fromXML(DOMElement $xml): object |
70
|
|
|
{ |
71
|
|
|
Assert::same($xml->localName, 'DigestValue', InvalidDOMElementException::class); |
72
|
|
|
Assert::same($xml->namespaceURI, DigestValue::NS, InvalidDOMElementException::class); |
73
|
|
|
|
74
|
|
|
return new self($xml->textContent); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Convert this DigestValue element to XML. |
80
|
|
|
* |
81
|
|
|
* @param \DOMElement|null $parent The element we should append this DigestValue element to. |
82
|
|
|
* @return \DOMElement |
83
|
|
|
*/ |
84
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
85
|
|
|
{ |
86
|
|
|
$e = $this->instantiateParentElement($parent); |
87
|
|
|
$e->textContent = $this->digest; |
88
|
|
|
|
89
|
|
|
return $e; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|