SSHFP   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.56%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 111
ccs 40
cts 41
cp 0.9756
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlgorithm() 0 4 1
A setAlgorithm() 0 7 2
A getFingerprintType() 0 4 1
A setFingerprintType() 0 7 2
A getFingerprint() 0 4 1
A setFingerprint() 0 4 1
A toText() 0 4 1
A toWire() 0 4 1
A fromText() 0 11 2
A fromWire() 0 12 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\ParseException;
17
use Badcow\DNS\Parser\Tokens;
18
use Badcow\DNS\Validator;
19
20
/**
21
 * {@link https://tools.ietf.org/html/rfc4255}.
22
 */
23
class SSHFP implements RdataInterface
24
{
25 1
    use RdataTrait;
26
27
    const TYPE = 'SSHFP';
28
    const TYPE_CODE = 44;
29
    const ALGORITHM_RSA = 1;
30
    const ALGORITHM_DSA = 2;
31
    const FP_TYPE_SHA1 = 1;
32
33
    /**
34
     * 8-bit algorithm designate.
35
     *
36
     * @var int
37
     */
38
    private $algorithm;
39
40
    /**
41
     * 8-bit Fingerprint type.
42
     *
43
     * @var int
44
     */
45
    private $fingerprintType = self::FP_TYPE_SHA1;
46
47
    /**
48
     * Hexadecimal string.
49
     *
50
     * @var string
51
     */
52
    private $fingerprint;
53
54 2
    public function getAlgorithm(): int
55
    {
56 2
        return $this->algorithm;
57
    }
58
59
    /**
60
     * @throws \InvalidArgumentException
61
     */
62 10
    public function setAlgorithm(int $algorithm): void
63
    {
64 10
        if (!Validator::isUnsignedInteger($algorithm, 8)) {
65 2
            throw new \InvalidArgumentException('Algorithm must be an 8-bit integer between 0 and 255.');
66
        }
67 8
        $this->algorithm = $algorithm;
68 8
    }
69
70 2
    public function getFingerprintType(): int
71
    {
72 2
        return $this->fingerprintType;
73
    }
74
75
    /**
76
     * @throws \InvalidArgumentException
77
     */
78 8
    public function setFingerprintType(int $fingerprintType): void
79
    {
80 8
        if (!Validator::isUnsignedInteger($fingerprintType, 8)) {
81 2
            throw new \InvalidArgumentException('Fingerprint type must be an 8-bit integer between 0 and 255.');
82
        }
83 6
        $this->fingerprintType = $fingerprintType;
84 6
    }
85
86 2
    public function getFingerprint(): string
87
    {
88 2
        return $this->fingerprint;
89
    }
90
91 6
    public function setFingerprint(string $fingerprint): void
92
    {
93 6
        $this->fingerprint = $fingerprint;
94 6
    }
95
96 2
    public function toText(): string
97
    {
98 2
        return sprintf('%d %d %s', $this->algorithm, $this->fingerprintType, bin2hex($this->fingerprint));
99
    }
100
101 1
    public function toWire(): string
102
    {
103 1
        return pack('CC', $this->algorithm, $this->fingerprintType).$this->fingerprint;
104
    }
105
106
    /**
107
     * @throws ParseException
108
     */
109 2
    public function fromText(string $text): void
110
    {
111 2
        $rdata = explode(Tokens::SPACE, $text);
112
113 2
        $this->setAlgorithm((int) array_shift($rdata));
114 2
        $this->setFingerprintType((int) array_shift($rdata));
115 2
        if (false === $fingerprint = hex2bin((string) array_shift($rdata))) {
116
            throw new ParseException('Fingerprint could no be parsed. Invalid hexadecimal.');
117
        }
118 2
        $this->setFingerprint($fingerprint);
119 2
    }
120
121 1
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
122
    {
123 1
        if (false === $integers = unpack('C<algorithm>/C<fpType>', $rdata, $offset)) {
124 1
            throw new DecodeException(static::TYPE, $rdata);
125 1
        }
126 1
        $offset += 2;
127 1
        $this->setAlgorithm($integers['<algorithm>']);
128 1
        $this->setFingerprintType($integers['<fpType>']);
129 1
        $fpLen = ($rdLength ?? strlen($rdata)) - 2;
130 1
        $this->setFingerprint(substr($rdata, $offset, $fpLen));
131
        $offset += $fpLen;
132
    }
133
}
134