Passed
Pull Request — master (#60)
by Tim
02:17
created

HMACOutputLengthTrait::validHMACOutputLength()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 12
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Assert;
6
7
use InvalidArgumentException;
8
9
use function intval;
10
11
/**
12
 * @package simplesamlphp/xml-security
13
 */
14
trait HMACOutputLengthTrait
15
{
16
    /**
17
     * The HMAC algorithm (RFC2104 [HMAC]) takes the output (truncation) length in bits as a parameter;
18
     * this specification REQUIRES that the truncation length be a multiple of 8 (i.e. fall on a byte boundary)
19
     * because Base64 encoding operates on full bytes
20
     *
21
     * @var string
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
        parent::regex(
33
            $value,
34
            self::$HMACOutputLength_regex,
35
            $message ?: '%s is not a valid ds:HMACOutputLengthType',
36
            InvalidArgumentException::class,
37
        );
38
        parent::true(
39
            intval($value) % 8 === 0,
40
            $message ?: '%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
41
            InvalidArgumentException::class,
42
        );
43
    }
44
}
45