HMACOutputLengthTrait::validHMACOutputLength()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 19
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Assert;
6
7
use SimpleSAML\Assert\AssertionFailedException;
8
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
9
use SimpleSAML\XMLSecurity\Exception\ProtocolViolationException;
10
11
use function intval;
12
13
/**
14
 * @package simplesamlphp/xml-security
15
 */
16
trait HMACOutputLengthTrait
17
{
18
    /**
19
     * The HMAC algorithm (RFC2104 [HMAC]) takes the output (truncation) length in bits as a parameter;
20
     * this specification REQUIRES that the truncation length be a multiple of 8 (i.e. fall on a byte boundary)
21
     * because Base64 encoding operates on full bytes
22
     */
23
    private static string $HMACOutputLength_regex = '/^([1-9]\d*)$/D';
24
25
26
    /**
27
     * @param string $value
28
     * @param string $message
29
     */
30
    protected static function validHMACOutputLength(string $value, string $message = ''): void
31
    {
32
        try {
33
            parent::regex(
34
                $value,
35
                self::$HMACOutputLength_regex,
36
                $message ?: '%s is not a valid ds:HMACOutputLengthType',
37
            );
38
        } catch (AssertionFailedException $e) {
39
            throw new SchemaViolationException($e->getMessage());
40
        }
41
42
        try {
43
            parent::true(
44
                intval($value) % 8 === 0,
45
                '%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
46
            );
47
        } catch (AssertionFailedException $e) {
48
            throw new ProtocolViolationException($e->getMessage());
49
        }
50
    }
51
}
52