NSEC3PARAM   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.16%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 126
ccs 47
cts 51
cp 0.9216
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getHashAlgorithm() 0 4 1
A setHashAlgorithm() 0 7 2
A getFlags() 0 4 1
A setFlags() 0 7 2
A getIterations() 0 4 1
A setIterations() 0 7 2
A getSalt() 0 4 1
A setSalt() 0 7 2
A toText() 0 4 1
A toWire() 0 4 1
A fromText() 0 8 1
A fromWire() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
use Badcow\DNS\Parser\Tokens;
17
use Badcow\DNS\Validator;
18
19
/**
20
 * {@link https://tools.ietf.org/html/rfc5155#section-4}.
21
 */
22
class NSEC3PARAM implements RdataInterface
23
{
24 1
    use RdataTrait;
25
26
    const TYPE = 'NSEC3PARAM';
27
    const TYPE_CODE = 51;
28
29
    /**
30
     * @var int
31
     */
32
    private $hashAlgorithm;
33
34
    /**
35
     * @var int
36
     */
37
    private $flags = 0;
38
39
    /**
40
     * @var int
41
     */
42
    private $iterations;
43
44
    /**
45
     * @var string Binary encoded string
46
     */
47
    private $salt;
48
49 1
    public function getHashAlgorithm(): int
50
    {
51 1
        return $this->hashAlgorithm;
52
    }
53
54
    /**
55
     * @throws \InvalidArgumentException
56
     */
57 6
    public function setHashAlgorithm(int $hashAlgorithm): void
58
    {
59 6
        if (!Validator::isUnsignedInteger($hashAlgorithm, 8)) {
60
            throw new \InvalidArgumentException('Hash algorithm must be 8-bit integer.');
61
        }
62 6
        $this->hashAlgorithm = $hashAlgorithm;
63 6
    }
64
65 1
    public function getFlags(): int
66
    {
67 1
        return $this->flags;
68
    }
69
70
    /**
71
     * @throws \InvalidArgumentException
72
     */
73 6
    public function setFlags(int $flags): void
74
    {
75 6
        if (!Validator::isUnsignedInteger($flags, 8)) {
76
            throw new \InvalidArgumentException('Flags must be an 8-bit unsigned integer.');
77
        }
78 6
        $this->flags = $flags;
79 6
    }
80
81 1
    public function getIterations(): int
82
    {
83 1
        return $this->iterations;
84
    }
85
86 6
    public function setIterations(int $iterations): void
87
    {
88 6
        if (!Validator::isUnsignedInteger($iterations, 16)) {
89
            throw new \InvalidArgumentException('Hash algorithm must be 16-bit integer.');
90
        }
91 6
        $this->iterations = $iterations;
92 6
    }
93
94
    /**
95
     * @return string Base16 string
96
     */
97 1
    public function getSalt(): string
98
    {
99 1
        return bin2hex($this->salt);
100
    }
101
102
    /**
103
     * @param string $salt Hexadecimal string
104
     */
105 6
    public function setSalt(string $salt): void
106
    {
107 6
        if (false === $bin = @hex2bin($salt)) {
108
            throw new \InvalidArgumentException('Salt must be a hexadecimal string.');
109
        }
110 6
        $this->salt = $bin;
111 6
    }
112
113 2
    public function toText(): string
114
    {
115 2
        return sprintf('%d %d %d %s', $this->hashAlgorithm, $this->flags, $this->iterations, bin2hex($this->salt));
116
    }
117
118 1
    public function toWire(): string
119
    {
120 1
        return pack('CCnC', $this->hashAlgorithm, $this->flags, $this->iterations, strlen($this->salt)).$this->salt;
121
    }
122
123 1
    public function fromText(string $text): void
124
    {
125 1
        $rdata = explode(Tokens::SPACE, $text);
126 1
        $this->setHashAlgorithm((int) array_shift($rdata));
127 1
        $this->setFlags((int) array_shift($rdata));
128 1
        $this->setIterations((int) array_shift($rdata));
129 1
        $this->setSalt((string) array_shift($rdata));
130 1
    }
131
132 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
133
    {
134 1
        if (false === $integers = unpack('C<algorithm>/C<flags>/n<iterations>/C<saltLen>', $rdata, $offset)) {
135 1
            throw new DecodeException(static::TYPE, $rdata);
136 1
        }
137 1
        $saltLen = (int) $integers['<saltLen>'];
138 1
        $offset += 5;
139 1
        $this->setHashAlgorithm($integers['<algorithm>']);
140
        $this->setFlags($integers['<flags>']);
141 1
        $this->setIterations($integers['<iterations>']);
142 1
143 1
        $saltBin = substr($rdata, $offset, $saltLen);
144 1
        $this->setSalt(bin2hex($saltBin));
145
        $offset += $saltLen;
146
    }
147
}
148