Passed
Push — master ( 87c83f...ac88b3 )
by Sam
03:00
created

Algorithms::getAlgorithmValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
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;
15
16
/**
17
 * Algorithms listed in {@link https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xml}.
18
 */
19
class Algorithms
20
{
21
    /**
22
     * Delete DS.
23
     *
24
     * {@link https://tools.ietf.org/html/rfc8078#section-4}
25
     */
26
    const DELETE = 0;
27
28
    /**
29
     * RSA/MD5.
30
     *
31
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
32
     */
33
    const RSAMD5 = 1;
34
35
    /**
36
     * Diffie-Hellman.
37
     *
38
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
39
     */
40
    const DH = 2;
41
42
    /**
43
     * DSA/SHA-1.
44
     *
45
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
46
     */
47
    const DSA = 3;
48
49
    /**
50
     * Elliptic Curve (Proposed).
51
     *
52
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
53
     */
54
    const ECC = 4;
55
56
    /**
57
     * RSA/SHA-1.
58
     *
59
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
60
     */
61
    const RSASHA1 = 5;
62
63
    /**
64
     * DSA-NSEC3-SHA1.
65
     *
66
     * {@link https://tools.ietf.org/html/RFC5155}
67
     */
68
    const DSA_NSEC3_SHA1 = 6;
69
70
    /**
71
     * RSASHA1-NSEC3-SHA1.
72
     *
73
     * {@link https://tools.ietf.org/html/RFC5155}
74
     */
75
    const RSASHA1_NSEC3_SHA1 = 7;
76
77
    /**
78
     * RSA/SHA-256.
79
     *
80
     * {@link https://tools.ietf.org/html/RFC5702}
81
     */
82
    const RSASHA256 = 8;
83
84
    /**
85
     * RSA/SHA-512.
86
     *
87
     * {@link https://tools.ietf.org/html/RFC5702}.
88
     */
89
    const RSASHA512 = 10;
90
91
    /**
92
     * GOST R 34.10-2001.
93
     *
94
     * {@link https://tools.ietf.org/html/RFC5933}.
95
     */
96
    const ECC_GOST = 12;
97
98
    /**
99
     * ECDSA Curve P-256 with SHA-256.
100
     *
101
     * {@link https://tools.ietf.org/html/RFC6605}.
102
     */
103
    const ECDSAP256SHA256 = 13;
104
105
    /**
106
     * ECDSA Curve P-384 with SHA-384.
107
     *
108
     * {@link https://tools.ietf.org/html/RFC6605}.
109
     */
110
    const ECDSAP384SHA384 = 14;
111
112
    /**
113
     * Ed25519.
114
     *
115
     * {@link https://tools.ietf.org/html/RFC8080}.
116
     */
117
    const ED25519 = 15;
118
119
    /**
120
     * Ed448.
121
     *
122
     * {@link https://tools.ietf.org/html/RFC8080}.
123
     */
124
    const ED448 = 16;
125
126
    /**
127
     * Indirect.
128
     *
129
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
130
     */
131
    const INDIRECT = 252;
132
133
    /**
134
     * Private.
135
     *
136
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
137
     */
138
    const PRIVATEDNS = 253;
139
140
    /**
141
     * Private.
142
     *
143
     * {@link https://tools.ietf.org/html/rfc4034#appendix-A.1}
144
     */
145
    const PRIVATEOID = 254;
146
147
    /**
148
     * @var array
149
     */
150
    const MNEMONICS = [
151
        self::DELETE => 'DELETE',
152
        self::RSAMD5 => 'RSAMD5',
153
        self::DH => 'DH',
154
        self::DSA => 'DSA',
155
        self::ECC => 'ECC',
156
        self::RSASHA1 => 'RSASHA1',
157
        self::DSA_NSEC3_SHA1 => 'DSA-NSEC3-SHA1',
158
        self::RSASHA1_NSEC3_SHA1 => 'RSASHA1-NSEC3-SHA1',
159
        self::RSASHA256 => 'RSASHA256',
160
        self::RSASHA512 => 'RSASHA512',
161
        self::ECC_GOST => 'ECC-GOST',
162
        self::ECDSAP256SHA256 => 'ECDSAP256SHA256',
163
        self::ECDSAP384SHA384 => 'ECDSAP384SHA384',
164
        self::ED25519 => 'ED25519',
165
        self::ED448 => 'ED448',
166
        self::INDIRECT => 'INDIRECT',
167
        self::PRIVATEDNS => 'PRIVATEDNS',
168
        self::PRIVATEOID => 'PRIVATEOID',
169
    ];
170
171
    /**
172
     * Get the associated mnemonic of an algorithm.
173
     *
174
     * @param int $algorithmId
175
     *
176
     * @return string
177
     *
178
     * @throws \InvalidArgumentException
179
     */
180 2
    public static function getMnemonic(int $algorithmId)
181
    {
182 2
        if (!array_key_exists($algorithmId, self::MNEMONICS)) {
183 1
            throw new \InvalidArgumentException(sprintf('"%d" is not a valid algorithm.', $algorithmId));
184
        }
185
186 1
        return self::MNEMONICS[$algorithmId];
187
    }
188
189
    /**
190
     * @param string $algorithmMnemonic
191
     *
192
     * @return int
193
     *
194
     * @throws \InvalidArgumentException
195
     */
196 2
    public static function getAlgorithmValue(string $algorithmMnemonic): int
197
    {
198 2
        if (false === $keyTypeValue = array_search($algorithmMnemonic, self::MNEMONICS, true)) {
199 1
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid algorithm mnemonic.', $algorithmMnemonic));
200
        }
201
202 1
        return (int) $keyTypeValue;
203
    }
204
}
205