11<?php
22
3+ declare (strict_types=1 );
4+
35namespace SimpleSAML \XMLSecurity \Alg \Signature ;
46
7+ use SimpleSAML \Assert \Assert ;
58use SimpleSAML \XMLSecurity \Alg \SignatureAlgorithm ;
69use SimpleSAML \XMLSecurity \Backend \SignatureBackend ;
10+ use SimpleSAML \XMLSecurity \Exception \RuntimeException ;
711use SimpleSAML \XMLSecurity \Key \AbstractKey ;
812
913/**
1014 * An abstract class that implements a generic digital signature algorithm.
1115 *
12- * @package SimpleSAML\XMLSecurity\\Alg\Signature
16+ * @package simplesamlphp/xml-security
1317 */
1418abstract class AbstractSigner implements SignatureAlgorithm
1519{
1620 /** @var \SimpleSAML\XMLSecurity\Key\AbstractKey */
17- protected AbstractKey $ key ;
21+ private AbstractKey $ key ;
1822
1923 /** @var \SimpleSAML\XMLSecurity\Backend\SignatureBackend */
2024 protected SignatureBackend $ backend ;
@@ -25,22 +29,46 @@ abstract class AbstractSigner implements SignatureAlgorithm
2529 /** @var string */
2630 protected string $ digest ;
2731
32+ /** @var string */
33+ protected string $ algId ;
34+
2835
2936 /**
3037 * Build a signature algorithm.
3138 *
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+ *
3243 * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The signing key.
44+ * @param string $algId The identifier of this algorithm.
3345 * @param string $digest The identifier of the digest algorithm to use.
3446 */
35- public function __construct (AbstractKey $ key , string $ digest )
47+ public function __construct (AbstractKey $ key , string $ algId , string $ digest )
3648 {
49+ Assert::oneOf (
50+ $ algId ,
51+ static ::getSupportedAlgorithms (),
52+ 'Unsupported algorithm for ' . static ::class,
53+ RuntimeException::class
54+ );
3755 $ this ->key = $ key ;
56+ $ this ->algId = $ algId ;
3857 $ this ->digest = $ digest ;
3958 $ this ->backend = new $ this ->default_backend ();
4059 $ this ->backend ->setDigestAlg ($ digest );
4160 }
4261
4362
63+ /**
64+ * @return string
65+ */
66+ public function getAlgorithmId (): string
67+ {
68+ return $ this ->algId ;
69+ }
70+
71+
4472 /**
4573 * @return string
4674 */
@@ -50,6 +78,15 @@ public function getDigest(): string
5078 }
5179
5280
81+ /**
82+ * @return AbstractKey
83+ */
84+ public function getKey (): AbstractKey
85+ {
86+ return $ this ->key ;
87+ }
88+
89+
5390 /**
5491 * @param \SimpleSAML\XMLSecurity\Backend\SignatureBackend $backend
5592 */
@@ -67,7 +104,7 @@ public function setBackend(SignatureBackend $backend): void
67104 *
68105 * @return string The (binary) signature corresponding to the given plaintext.
69106 */
70- public function sign (string $ plaintext ): string
107+ final public function sign (string $ plaintext ): string
71108 {
72109 return $ this ->backend ->sign ($ this ->key , $ plaintext );
73110 }
@@ -81,7 +118,7 @@ public function sign(string $plaintext): string
81118 *
82119 * @return boolean True if the signature can be verified, false otherwise.
83120 */
84- public function verify (string $ plaintext , string $ signature ): bool
121+ final public function verify (string $ plaintext , string $ signature ): bool
85122 {
86123 return $ this ->backend ->verify ($ this ->key , $ plaintext , $ signature );
87124 }
0 commit comments