HMAC::__construct()   A
last analyzed

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 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Backend;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XMLSecurity\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
10
use SimpleSAML\XMLSecurity\Key\KeyInterface;
11
12
use function hash_equals;
13
use function hash_hmac;
14
15
/**
16
 * Backend for digital signatures based on hash-based message authentication codes.
17
 *
18
 * @package SimpleSAML\XMLSecurity\Backend
19
 */
20
final class HMAC implements SignatureBackend
21
{
22
    protected string $digest;
23
24
25
    /**
26
     * Build an HMAC backend.
27
     */
28
    public function __construct()
29
    {
30
        $this->digest = C::$DIGEST_ALGORITHMS[C::DIGEST_SHA256];
31
    }
32
33
34
    /**
35
     * Set the digest algorithm to be used by this backend.
36
     *
37
     * @param string $digest The identifier of the digest algorithm.
38
     *
39
     * @throws \SimpleSAML\XMLSecurity\Exception\InvalidArgumentException If the given digest is not valid.
40
     */
41
    public function setDigestAlg(string $digest): void
42
    {
43
        Assert::keyExists(
44
            C::$DIGEST_ALGORITHMS,
45
            $digest,
46
            'Unknown digest or non-cryptographic hash function.',
47
            InvalidArgumentException::class,
48
        );
49
50
        $this->digest = C::$DIGEST_ALGORITHMS[$digest];
51
    }
52
53
54
    /**
55
     * Sign a given plaintext with this cipher and a given key.
56
     *
57
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The key to use to sign.
58
     * @param string $plaintext The original text to sign.
59
     *
60
     * @return string The (binary) signature corresponding to the given plaintext.
61
     */
62
    public function sign(
63
        #[\SensitiveParameter]
64
        KeyInterface $key,
65
        string $plaintext,
66
    ): string {
67
        return hash_hmac($this->digest, $plaintext, $key->getMaterial(), true);
68
    }
69
70
71
    /**
72
     * Verify a signature with this cipher and a given key.
73
     *
74
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The key to use to verify the signature.
75
     * @param string $plaintext The original signed text.
76
     * @param string $signature The (binary) signature to verify.
77
     *
78
     * @return boolean True if the signature can be verified, false otherwise.
79
     */
80
    public function verify(
81
        #[\SensitiveParameter]
82
        KeyInterface $key,
83
        string $plaintext,
84
        string $signature,
85
    ): bool {
86
        return hash_equals(hash_hmac($this->digest, $plaintext, $key->getMaterial(), true), $signature);
87
    }
88
}
89