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
|
|
|
use SimpleSAML\XML\XMLBase64ElementTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class representing a ds:X509Digest element. |
16
|
|
|
* |
17
|
|
|
* @package simplesaml/xml-security |
18
|
|
|
*/ |
19
|
|
|
final class X509Digest extends AbstractDsElement |
20
|
|
|
{ |
21
|
|
|
use XMLBase64ElementTrait; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The digest algorithm. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected string $algorithm; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize a X509Digest element. |
33
|
|
|
* |
34
|
|
|
* @param string $digest |
35
|
|
|
* @param string $algorithm |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $digest, string $algorithm) |
38
|
|
|
{ |
39
|
|
|
$this->setContent($digest); |
40
|
|
|
$this->setAlgorithm($algorithm); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Collect the value of the algorithm-property |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getAlgorithm(): string |
50
|
|
|
{ |
51
|
|
|
return $this->algorithm; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set the value of the algorithm-property |
57
|
|
|
* |
58
|
|
|
* @param string $algorithm |
59
|
|
|
*/ |
60
|
|
|
private function setAlgorithm(string $algorithm): void |
61
|
|
|
{ |
62
|
|
|
Assert::oneOf( |
63
|
|
|
$algorithm, |
64
|
|
|
[ |
65
|
|
|
Constants::DIGEST_SHA1, |
66
|
|
|
Constants::DIGEST_SHA224, |
67
|
|
|
Constants::DIGEST_SHA256, |
68
|
|
|
Constants::DIGEST_SHA384, |
69
|
|
|
Constants::DIGEST_SHA512, |
70
|
|
|
Constants::DIGEST_RIPEMD160, |
71
|
|
|
], |
72
|
|
|
'Invalid digest method', |
73
|
|
|
InvalidArgumentException::class |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->algorithm = $algorithm; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert XML into a X509Digest |
82
|
|
|
* |
83
|
|
|
* @param \DOMElement $xml The XML element we should load |
84
|
|
|
* @return self |
85
|
|
|
* |
86
|
|
|
* @throws \SimpleSAML\XML\Exception\InvalidDOMElementException |
87
|
|
|
* If the qualified name of the supplied element is wrong |
88
|
|
|
*/ |
89
|
|
|
public static function fromXML(DOMElement $xml): object |
90
|
|
|
{ |
91
|
|
|
Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class); |
92
|
|
|
Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class); |
93
|
|
|
|
94
|
|
|
$algorithm = self::getAttribute($xml, 'Algorithm'); |
95
|
|
|
|
96
|
|
|
return new self($xml->textContent, $algorithm); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Convert this X509Digest element to XML. |
102
|
|
|
* |
103
|
|
|
* @param \DOMElement|null $parent The element we should append this X509Digest element to. |
104
|
|
|
* @return \DOMElement |
105
|
|
|
*/ |
106
|
|
|
public function toXML(DOMElement $parent = null): DOMElement |
107
|
|
|
{ |
108
|
|
|
$e = $this->instantiateParentElement($parent); |
109
|
|
|
$e->textContent = $this->content; |
110
|
|
|
$e->setAttribute('Algorithm', $this->algorithm); |
111
|
|
|
|
112
|
|
|
return $e; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|