Passed
Push — master ( 638c2e...6ee01b )
by Tony Karavasilev (Тони
16:56
created

HashAlgorithmFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 100
c 1
b 0
f 0
dl 0
loc 321
ccs 54
cts 54
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __debugInfo() 0 49 1
A createInstance() 0 14 3
1
<?php
2
3
/**
4
 * Factory object for hash algorithm object instancing.
5
 */
6
7
namespace CryptoManana\Factories;
8
9
use \CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern;
10
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as HashAlgorithm;
11
use \CryptoManana\Hashing\Md5 as Md5;
12
use \CryptoManana\Hashing\Sha1 as Sha1;
13
use \CryptoManana\Hashing\ShaTwo224 as ShaTwo224;
14
use \CryptoManana\Hashing\ShaTwo256 as ShaTwo256;
15
use \CryptoManana\Hashing\ShaTwo384 as ShaTwo384;
16
use \CryptoManana\Hashing\ShaTwo512 as ShaTwo512;
17
use \CryptoManana\Hashing\ShaThree224 as ShaThree224;
18
use \CryptoManana\Hashing\ShaThree256 as ShaThree256;
19
use \CryptoManana\Hashing\ShaThree384 as ShaThree384;
20
use \CryptoManana\Hashing\ShaThree512 as ShaThree512;
21
use \CryptoManana\Hashing\Whirlpool as Whirlpool;
22
use \CryptoManana\Hashing\HmacMd5 as HmacMd5;
23
use \CryptoManana\Hashing\HmacSha1 as HmacSha1;
24
use \CryptoManana\Hashing\HmacShaThree224 as HmacShaThree224;
25
use \CryptoManana\Hashing\HmacShaThree256 as HmacShaThree256;
26
use \CryptoManana\Hashing\HmacShaThree384 as HmacShaThree384;
27
use \CryptoManana\Hashing\HmacShaThree512 as HmacShaThree512;
28
use \CryptoManana\Hashing\HmacShaTwo224 as HmacShaTwo224;
29
use \CryptoManana\Hashing\HmacShaTwo256 as HmacShaTwo256;
30
use \CryptoManana\Hashing\HmacShaTwo384 as HmacShaTwo384;
31
use \CryptoManana\Hashing\HmacShaTwo512 as HmacShaTwo512;
32
use \CryptoManana\Hashing\HmacWhirlpool as HmacWhirlpool;
33
use \CryptoManana\Hashing\HkdfMd5 as HkdfMd5;
34
use \CryptoManana\Hashing\HkdfSha1 as HkdfSha1;
35
use \CryptoManana\Hashing\HkdfShaThree224 as HkdfShaThree224;
36
use \CryptoManana\Hashing\HkdfShaThree256 as HkdfShaThree256;
37
use \CryptoManana\Hashing\HkdfShaThree384 as HkdfShaThree384;
38
use \CryptoManana\Hashing\HkdfShaThree512 as HkdfShaThree512;
39
use \CryptoManana\Hashing\HkdfShaTwo224 as HkdfShaTwo224;
40
use \CryptoManana\Hashing\HkdfShaTwo256 as HkdfShaTwo256;
41
use \CryptoManana\Hashing\HkdfShaTwo384 as HkdfShaTwo384;
42
use \CryptoManana\Hashing\HkdfShaTwo512 as HkdfShaTwo512;
43
use \CryptoManana\Hashing\HkdfWhirlpool as HkdfWhirlpool;
44
use \CryptoManana\Hashing\Pbkdf2Md5 as Pbkdf2Md5;
45
use \CryptoManana\Hashing\Pbkdf2Sha1 as Pbkdf2Sha1;
46
use \CryptoManana\Hashing\Pbkdf2ShaThree224 as Pbkdf2ShaThree224;
47
use \CryptoManana\Hashing\Pbkdf2ShaThree256 as Pbkdf2ShaThree256;
48
use \CryptoManana\Hashing\Pbkdf2ShaThree384 as Pbkdf2ShaThree384;
49
use \CryptoManana\Hashing\Pbkdf2ShaThree512 as Pbkdf2ShaThree512;
50
use \CryptoManana\Hashing\Pbkdf2ShaTwo224 as Pbkdf2ShaTwo224;
51
use \CryptoManana\Hashing\Pbkdf2ShaTwo256 as Pbkdf2ShaTwo256;
52
use \CryptoManana\Hashing\Pbkdf2ShaTwo384 as Pbkdf2ShaTwo384;
53
use \CryptoManana\Hashing\Pbkdf2ShaTwo512 as Pbkdf2ShaTwo512;
54
use \CryptoManana\Hashing\Pbkdf2Whirlpool as Pbkdf2Whirlpool;
55
use \CryptoManana\Hashing\Bcrypt as Bcrypt;
56
use \CryptoManana\Hashing\Argon2 as Argon2;
57
58
/**
59
 * Class HashAlgorithmFactory - Factory object for hash algorithm object instancing.
60
 *
61
 * @package CryptoManana\Factories
62
 */
63
class HashAlgorithmFactory extends FactoryPattern
64
{
65
    /**
66
     * The MD5 type.
67
     */
68
    const MD5 = Md5::class;
69
70
    /**
71
     * The HMAC-MD5 type.
72
     */
73
    const HMAC_MD5 = HmacMd5::class;
74
75
    /**
76
     * The HKDF-MD5 type.
77
     */
78
    const HKDF_MD5 = HkdfMd5::class;
79
80
    /**
81
     * The PBKDF2-MD5 type.
82
     */
83
    const PBKDF2_MD5 = Pbkdf2Md5::class;
84
85
    /**
86
     * The SHA-1 type.
87
     */
88
    const SHA1 = Sha1::class;
89
90
    /**
91
     * The HMAC-SHA-1 type.
92
     */
93
    const HMAC_SHA1 = HmacSha1::class;
94
95
    /**
96
     * The HKDF-SHA-1 type.
97
     */
98
    const HKDF_SHA1 = HkdfSha1::class;
99
100
    /**
101
     * The PBKDF2-SHA-1 type.
102
     */
103
    const PBKDF2_SHA1 = Pbkdf2Sha1::class;
104
105
    /**
106
     * The SHA-2-224 type.
107
     */
108
    const SHA2_224 = ShaTwo224::class;
109
110
    /**
111
     * The HMAC-SHA-2-224 type.
112
     */
113
    const HMAC_SHA2_224 = HmacShaTwo224::class;
114
115
    /**
116
     * The HKDF-SHA-2-224 type.
117
     */
118
    const HKDF_SHA2_224 = HkdfShaTwo224::class;
119
120
    /**
121
     * The PBKDF2-SHA-2-224 type.
122
     */
123
    const PBKDF2_SHA2_224 = Pbkdf2ShaTwo224::class;
124
125
    /**
126
     * The SHA-2-256 type.
127
     */
128
    const SHA2_256 = ShaTwo256::class;
129
130
    /**
131
     * The HMAC-SHA-2-256 type.
132
     */
133
    const HMAC_SHA2_256 = HmacShaTwo256::class;
134
135
    /**
136
     * The HKDF-SHA-2-256 type.
137
     */
138
    const HKDF_SHA2_256 = HkdfShaTwo256::class;
139
140
    /**
141
     * The PBKDF2-SHA-2-256 type.
142
     */
143
    const PBKDF2_SHA2_256 = Pbkdf2ShaTwo256::class;
144
145
    /**
146
     * The SHA-2-384 type.
147
     */
148
    const SHA2_384 = ShaTwo384::class;
149
150
    /**
151
     * The HMAC-SHA-2-384 type.
152
     */
153
    const HMAC_SHA2_384 = HmacShaTwo384::class;
154
155
    /**
156
     * The HKDF-SHA-2-384 type.
157
     */
158
    const HKDF_SHA2_384 = HkdfShaTwo384::class;
159
160
    /**
161
     * The PBKDF2-SHA-2-384 type.
162
     */
163
    const PBKDF2_SHA2_384 = Pbkdf2ShaTwo384::class;
164
165
    /**
166
     * The SHA-2-512 type.
167
     */
168
    const SHA2_512 = ShaTwo512::class;
169
170
    /**
171
     * The HMAC-SHA-2-512 type.
172
     */
173
    const HMAC_SHA2_512 = HmacShaTwo512::class;
174
175
    /**
176
     * The HKDF-SHA-2-512 type.
177
     */
178
    const HKDF_SHA2_512 = HkdfShaTwo512::class;
179
180
    /**
181
     * The PBKDF2-SHA-2-512 type.
182
     */
183
    const PBKDF2_SHA2_512 = Pbkdf2ShaTwo512::class;
184
185
    /**
186
     * The SHA-3-224 type.
187
     */
188
    const SHA3_224 = ShaThree224::class;
189
190
    /**
191
     * The HMAC-SHA-3-224 type.
192
     */
193
    const HMAC_SHA3_224 = HmacShaThree224::class;
194
195
    /**
196
     * The HKDF-SHA-3-224 type.
197
     */
198
    const HKDF_SHA3_224 = HkdfShaThree224::class;
199
200
    /**
201
     * The PBKDF2-SHA-3-224 type.
202
     */
203
    const PBKDF2_SHA3_224 = Pbkdf2ShaThree224::class;
204
205
    /**
206
     * The SHA-3-256 type.
207
     */
208
    const SHA3_256 = ShaThree256::class;
209
210
    /**
211
     * The HMAC-SHA-3-256 type.
212
     */
213
    const HMAC_SHA3_256 = HmacShaThree256::class;
214
215
    /**
216
     * The HKDF-SHA-3-256 type.
217
     */
218
    const HKDF_SHA3_256 = HkdfShaThree256::class;
219
220
    /**
221
     * The PBKDF2-SHA-3-256 type.
222
     */
223
    const PBKDF2_SHA3_256 = Pbkdf2ShaThree256::class;
224
225
    /**
226
     * The SHA-3-384 type.
227
     */
228
    const SHA3_384 = ShaThree384::class;
229
230
    /**
231
     * The HMAC-SHA-3-384 type.
232
     */
233
    const HMAC_SHA3_384 = HmacShaThree384::class;
234
235
    /**
236
     * The HKDF-SHA-3-384 type.
237
     */
238
    const HKDF_SHA3_384 = HkdfShaThree384::class;
239
240
    /**
241
     * The PBKDF2-SHA-3-384 type.
242
     */
243
    const PBKDF2_SHA3_384 = Pbkdf2ShaThree384::class;
244
245
    /**
246
     * The SHA-3-512 type.
247
     */
248
    const SHA3_512 = ShaThree512::class;
249
250
    /**
251
     * The HMAC-SHA-3-512 type.
252
     */
253
    const HMAC_SHA3_512 = HmacShaThree512::class;
254
255
    /**
256
     * The HKDF-SHA-3-512 type.
257
     */
258
    const HKDF_SHA3_512 = HkdfShaThree512::class;
259
260
    /**
261
     * The PBKDF2-SHA-3-512 type.
262
     */
263
    const PBKDF2_SHA3_512 = Pbkdf2ShaThree512::class;
264
265
    /**
266
     * The Whirlpool type.
267
     */
268
    const WHIRLPOOL = Whirlpool::class;
269
270
    /**
271
     * The HMAC-Whirlpool type.
272
     */
273
    const HMAC_WHIRLPOOL = HmacWhirlpool::class;
274
275
    /**
276
     * The HKDF-Whirlpool type.
277
     */
278
    const HKDF_WHIRLPOOL = HkdfWhirlpool::class;
279
280
    /**
281
     * The PBKDF2-Whirlpool type.
282
     */
283
    const PBKDF2_WHIRLPOOL = Pbkdf2Whirlpool::class;
284
285
    /**
286
     * The Bcrypt type.
287
     */
288
    const BCRYPT = Bcrypt::class;
289
290
    /**
291
     * The Argon2 type.
292
     */
293
    const ARGON2 = Argon2::class;
294
295
    /**
296
     * Create a hash algorithm object.
297
     *
298
     * @param string|null $type The algorithm class name as type for creation.
299
     *
300
     * @return HashAlgorithm|object|null A hash algorithm object or null.
301
     */
302 6
    public function create($type)
303
    {
304 6
        return self::createInstance($type);
305
    }
306
307
    /**
308
     * Create a hash algorithm object
309
     *
310
     * @param string|null $type The algorithm class name as type for creation.
311
     *
312
     * @return HashAlgorithm|object|null A hash algorithm object or null.
313
     */
314 8
    public static function createInstance($type)
315
    {
316
        /**
317
         * Check if class exists and has a correct base class
318
         *
319
         * @var HashAlgorithm|null $exception Object instance.
320
         */
321 8
        if (class_exists($type) && is_subclass_of($type, HashAlgorithm::class)) {
322 8
            $exception = new $type();
323
        } else {
324 4
            $exception = null; // Invalid type given
325
        }
326
327 8
        return $exception;
328
    }
329
330
    /**
331
     * Get debug information for the class instance.
332
     *
333
     * @return array Debug information.
334
     */
335 2
    public function __debugInfo()
336
    {
337
        return [
338 2
            self::class . '::MD5' => self::MD5,
339 1
            self::class . '::HMAC_MD5' => self::HMAC_MD5,
340 1
            self::class . '::HKDF_MD5' => self::HKDF_MD5,
341 1
            self::class . '::PBKDF2_MD5' => self::PBKDF2_MD5,
342 1
            self::class . '::SHA1' => self::SHA1,
343 1
            self::class . '::SHA1' => self::HMAC_SHA1,
344 1
            self::class . '::SHA1' => self::HKDF_SHA1,
345 1
            self::class . '::SHA1' => self::PBKDF2_SHA1,
346 1
            self::class . '::SHA2_224' => self::SHA2_224,
347 1
            self::class . '::HMAC_SHA2_224' => self::HMAC_SHA2_224,
348 1
            self::class . '::HKDF_SHA2_224' => self::HKDF_SHA2_224,
349 1
            self::class . '::PBKDF2_SHA2_224' => self::PBKDF2_SHA2_224,
350 1
            self::class . '::SHA2_256' => self::SHA2_256,
351 1
            self::class . '::HMAC_SHA2_256' => self::HMAC_SHA2_256,
352 1
            self::class . '::HKDF_SHA2_256' => self::HKDF_SHA2_256,
353 1
            self::class . '::PBKDF2_SHA2_256' => self::PBKDF2_SHA2_256,
354 1
            self::class . '::SHA2_384' => self::SHA2_384,
355 1
            self::class . '::HMAC_SHA2_384' => self::HMAC_SHA2_384,
356 1
            self::class . '::HKDF_SHA2_384' => self::HKDF_SHA2_384,
357 1
            self::class . '::PBKDF2_SHA2_384' => self::PBKDF2_SHA2_384,
358 1
            self::class . '::SHA2_512' => self::SHA2_512,
359 1
            self::class . '::HMAC_SHA2_512' => self::HMAC_SHA2_512,
360 1
            self::class . '::HKDF_SHA2_512' => self::HKDF_SHA2_512,
361 1
            self::class . '::PBKDF2_SHA2_512' => self::PBKDF2_SHA2_512,
362 1
            self::class . '::SHA3_224' => self::SHA3_224,
363 1
            self::class . '::HMAC_SHA3_224' => self::HMAC_SHA3_224,
364 1
            self::class . '::HKDF_SHA3_224' => self::HKDF_SHA3_224,
365 1
            self::class . '::PBKDF2_SHA3_224' => self::PBKDF2_SHA3_224,
366 1
            self::class . '::SHA3_256' => self::SHA3_256,
367 1
            self::class . '::HMAC_SHA3_256' => self::HMAC_SHA3_256,
368 1
            self::class . '::HKDF_SHA3_256' => self::HKDF_SHA3_256,
369 1
            self::class . '::PBKDF2_SHA3_256' => self::PBKDF2_SHA3_256,
370 1
            self::class . '::SHA3_384' => self::SHA3_384,
371 1
            self::class . '::HMAC_SHA3_384' => self::HMAC_SHA3_384,
372 1
            self::class . '::HKDF_SHA3_384' => self::HKDF_SHA3_384,
373 1
            self::class . '::PBKDF2_SHA3_384' => self::PBKDF2_SHA3_384,
374 1
            self::class . '::SHA3_512' => self::SHA3_512,
375 1
            self::class . '::HMAC_SHA3_512' => self::HMAC_SHA3_512,
376 1
            self::class . '::HKDF_SHA3_512' => self::HKDF_SHA3_512,
377 1
            self::class . '::PBKDF2_SHA3_512' => self::PBKDF2_SHA3_512,
378 1
            self::class . '::WHIRLPOOL' => self::WHIRLPOOL,
379 1
            self::class . '::HMAC_WHIRLPOOL' => self::HMAC_WHIRLPOOL,
380 1
            self::class . '::HKDF_WHIRLPOOL' => self::HKDF_WHIRLPOOL,
381 1
            self::class . '::PBKDF2_WHIRLPOOL' => self::PBKDF2_WHIRLPOOL,
382 1
            self::class . '::BCRYPT' => self::BCRYPT,
383 2
            self::class . '::ARGON2' => self::ARGON2,
384
        ];
385
    }
386
}
387