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

HMACOutputLengthTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A validHMACOutputLength() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Assert;
6
7
use SimpleSAML\Assert\AssertionFailedException;
8
use SimpleSAML\XML\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
     * @var string
24
     */
25
    private static string $HMACOutputLength_regex = '/^([1-9]\d*)$/D';
26
27
28
    /**
29
     * @param string $value
30
     * @param string $message
31
     */
32
    protected static function validHMACOutputLength(string $value, string $message = ''): void
33
    {
34
        try {
35
            parent::regex(
36
                $value,
37
                self::$HMACOutputLength_regex,
38
                $message ?: '%s is not a valid ds:HMACOutputLengthType',
39
            );
40
        } catch (AssertionFailedException $e) {
41
            throw new SchemaViolationException($e->getMessage());
42
        }
43
44
        try {
45
            parent::true(
46
                intval($value) % 8 === 0,
47
                '%s is not devisible by 8 and therefore not a valid ds:HMACOutputLengthType',
48
            );
49
        } catch (AssertionFailedException $e) {
50
            throw new ProtocolViolationException($e->getMessage());
51
        }
52
    }
53
}
54