Completed
Push — master ( 89329a...9c982a )
by Tony Karavasilev (Тони
19:19
created

HashAlgorithmFactory::__debugInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 63
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 65
ccs 63
cts 63
cp 1
rs 8.8072
cc 1
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Ripemd128 as Ripemd128;
22
use \CryptoManana\Hashing\Ripemd160 as Ripemd160;
23
use \CryptoManana\Hashing\Ripemd256 as Ripemd256;
24
use \CryptoManana\Hashing\Ripemd320 as Ripemd320;
25
use \CryptoManana\Hashing\Whirlpool as Whirlpool;
26
use \CryptoManana\Hashing\HmacMd5 as HmacMd5;
27
use \CryptoManana\Hashing\HmacSha1 as HmacSha1;
28
use \CryptoManana\Hashing\HmacShaThree224 as HmacShaThree224;
29
use \CryptoManana\Hashing\HmacShaThree256 as HmacShaThree256;
30
use \CryptoManana\Hashing\HmacShaThree384 as HmacShaThree384;
31
use \CryptoManana\Hashing\HmacShaThree512 as HmacShaThree512;
32
use \CryptoManana\Hashing\HmacShaTwo224 as HmacShaTwo224;
33
use \CryptoManana\Hashing\HmacShaTwo256 as HmacShaTwo256;
34
use \CryptoManana\Hashing\HmacShaTwo384 as HmacShaTwo384;
35
use \CryptoManana\Hashing\HmacShaTwo512 as HmacShaTwo512;
36
use \CryptoManana\Hashing\HmacRipemd128 as HmacRipemd128;
37
use \CryptoManana\Hashing\HmacRipemd160 as HmacRipemd160;
38
use \CryptoManana\Hashing\HmacRipemd256 as HmacRipemd256;
39
use \CryptoManana\Hashing\HmacRipemd320 as HmacRipemd320;
40
use \CryptoManana\Hashing\HmacWhirlpool as HmacWhirlpool;
41
use \CryptoManana\Hashing\HkdfMd5 as HkdfMd5;
42
use \CryptoManana\Hashing\HkdfSha1 as HkdfSha1;
43
use \CryptoManana\Hashing\HkdfShaThree224 as HkdfShaThree224;
44
use \CryptoManana\Hashing\HkdfShaThree256 as HkdfShaThree256;
45
use \CryptoManana\Hashing\HkdfShaThree384 as HkdfShaThree384;
46
use \CryptoManana\Hashing\HkdfShaThree512 as HkdfShaThree512;
47
use \CryptoManana\Hashing\HkdfShaTwo224 as HkdfShaTwo224;
48
use \CryptoManana\Hashing\HkdfShaTwo256 as HkdfShaTwo256;
49
use \CryptoManana\Hashing\HkdfShaTwo384 as HkdfShaTwo384;
50
use \CryptoManana\Hashing\HkdfShaTwo512 as HkdfShaTwo512;
51
use \CryptoManana\Hashing\HkdfRipemd128 as HkdfRipemd128;
52
use \CryptoManana\Hashing\HkdfRipemd160 as HkdfRipemd160;
53
use \CryptoManana\Hashing\HkdfRipemd256 as HkdfRipemd256;
54
use \CryptoManana\Hashing\HkdfRipemd320 as HkdfRipemd320;
55
use \CryptoManana\Hashing\HkdfWhirlpool as HkdfWhirlpool;
56
use \CryptoManana\Hashing\Pbkdf2Md5 as Pbkdf2Md5;
57
use \CryptoManana\Hashing\Pbkdf2Sha1 as Pbkdf2Sha1;
58
use \CryptoManana\Hashing\Pbkdf2ShaThree224 as Pbkdf2ShaThree224;
59
use \CryptoManana\Hashing\Pbkdf2ShaThree256 as Pbkdf2ShaThree256;
60
use \CryptoManana\Hashing\Pbkdf2ShaThree384 as Pbkdf2ShaThree384;
61
use \CryptoManana\Hashing\Pbkdf2ShaThree512 as Pbkdf2ShaThree512;
62
use \CryptoManana\Hashing\Pbkdf2ShaTwo224 as Pbkdf2ShaTwo224;
63
use \CryptoManana\Hashing\Pbkdf2ShaTwo256 as Pbkdf2ShaTwo256;
64
use \CryptoManana\Hashing\Pbkdf2ShaTwo384 as Pbkdf2ShaTwo384;
65
use \CryptoManana\Hashing\Pbkdf2ShaTwo512 as Pbkdf2ShaTwo512;
66
use \CryptoManana\Hashing\Pbkdf2Ripemd128 as Pbkdf2Ripemd128;
67
use \CryptoManana\Hashing\Pbkdf2Ripemd160 as Pbkdf2Ripemd160;
68
use \CryptoManana\Hashing\Pbkdf2Ripemd256 as Pbkdf2Ripemd256;
69
use \CryptoManana\Hashing\Pbkdf2Ripemd320 as Pbkdf2Ripemd320;
70
use \CryptoManana\Hashing\Pbkdf2Whirlpool as Pbkdf2Whirlpool;
71
use \CryptoManana\Hashing\Bcrypt as Bcrypt;
72
use \CryptoManana\Hashing\Argon2 as Argon2;
73
74
/**
75
 * Class HashAlgorithmFactory - Factory object for hash algorithm object instancing.
76
 *
77
 * @package CryptoManana\Factories
78
 */
79
class HashAlgorithmFactory extends FactoryPattern
80
{
81
    /**
82
     * The MD5 type.
83
     */
84
    const MD5 = Md5::class;
85
86
    /**
87
     * The HMAC-MD5 type.
88
     */
89
    const HMAC_MD5 = HmacMd5::class;
90
91
    /**
92
     * The HKDF-MD5 type.
93
     */
94
    const HKDF_MD5 = HkdfMd5::class;
95
96
    /**
97
     * The PBKDF2-MD5 type.
98
     */
99
    const PBKDF2_MD5 = Pbkdf2Md5::class;
100
101
    /**
102
     * The SHA-1 type.
103
     */
104
    const SHA1 = Sha1::class;
105
106
    /**
107
     * The HMAC-SHA-1 type.
108
     */
109
    const HMAC_SHA1 = HmacSha1::class;
110
111
    /**
112
     * The HKDF-SHA-1 type.
113
     */
114
    const HKDF_SHA1 = HkdfSha1::class;
115
116
    /**
117
     * The PBKDF2-SHA-1 type.
118
     */
119
    const PBKDF2_SHA1 = Pbkdf2Sha1::class;
120
121
    /**
122
     * The SHA-2-224 type.
123
     */
124
    const SHA2_224 = ShaTwo224::class;
125
126
    /**
127
     * The HMAC-SHA-2-224 type.
128
     */
129
    const HMAC_SHA2_224 = HmacShaTwo224::class;
130
131
    /**
132
     * The HKDF-SHA-2-224 type.
133
     */
134
    const HKDF_SHA2_224 = HkdfShaTwo224::class;
135
136
    /**
137
     * The PBKDF2-SHA-2-224 type.
138
     */
139
    const PBKDF2_SHA2_224 = Pbkdf2ShaTwo224::class;
140
141
    /**
142
     * The SHA-2-256 type.
143
     */
144
    const SHA2_256 = ShaTwo256::class;
145
146
    /**
147
     * The HMAC-SHA-2-256 type.
148
     */
149
    const HMAC_SHA2_256 = HmacShaTwo256::class;
150
151
    /**
152
     * The HKDF-SHA-2-256 type.
153
     */
154
    const HKDF_SHA2_256 = HkdfShaTwo256::class;
155
156
    /**
157
     * The PBKDF2-SHA-2-256 type.
158
     */
159
    const PBKDF2_SHA2_256 = Pbkdf2ShaTwo256::class;
160
161
    /**
162
     * The SHA-2-384 type.
163
     */
164
    const SHA2_384 = ShaTwo384::class;
165
166
    /**
167
     * The HMAC-SHA-2-384 type.
168
     */
169
    const HMAC_SHA2_384 = HmacShaTwo384::class;
170
171
    /**
172
     * The HKDF-SHA-2-384 type.
173
     */
174
    const HKDF_SHA2_384 = HkdfShaTwo384::class;
175
176
    /**
177
     * The PBKDF2-SHA-2-384 type.
178
     */
179
    const PBKDF2_SHA2_384 = Pbkdf2ShaTwo384::class;
180
181
    /**
182
     * The SHA-2-512 type.
183
     */
184
    const SHA2_512 = ShaTwo512::class;
185
186
    /**
187
     * The HMAC-SHA-2-512 type.
188
     */
189
    const HMAC_SHA2_512 = HmacShaTwo512::class;
190
191
    /**
192
     * The HKDF-SHA-2-512 type.
193
     */
194
    const HKDF_SHA2_512 = HkdfShaTwo512::class;
195
196
    /**
197
     * The PBKDF2-SHA-2-512 type.
198
     */
199
    const PBKDF2_SHA2_512 = Pbkdf2ShaTwo512::class;
200
201
    /**
202
     * The SHA-3-224 type.
203
     */
204
    const SHA3_224 = ShaThree224::class;
205
206
    /**
207
     * The HMAC-SHA-3-224 type.
208
     */
209
    const HMAC_SHA3_224 = HmacShaThree224::class;
210
211
    /**
212
     * The HKDF-SHA-3-224 type.
213
     */
214
    const HKDF_SHA3_224 = HkdfShaThree224::class;
215
216
    /**
217
     * The PBKDF2-SHA-3-224 type.
218
     */
219
    const PBKDF2_SHA3_224 = Pbkdf2ShaThree224::class;
220
221
    /**
222
     * The SHA-3-256 type.
223
     */
224
    const SHA3_256 = ShaThree256::class;
225
226
    /**
227
     * The HMAC-SHA-3-256 type.
228
     */
229
    const HMAC_SHA3_256 = HmacShaThree256::class;
230
231
    /**
232
     * The HKDF-SHA-3-256 type.
233
     */
234
    const HKDF_SHA3_256 = HkdfShaThree256::class;
235
236
    /**
237
     * The PBKDF2-SHA-3-256 type.
238
     */
239
    const PBKDF2_SHA3_256 = Pbkdf2ShaThree256::class;
240
241
    /**
242
     * The SHA-3-384 type.
243
     */
244
    const SHA3_384 = ShaThree384::class;
245
246
    /**
247
     * The HMAC-SHA-3-384 type.
248
     */
249
    const HMAC_SHA3_384 = HmacShaThree384::class;
250
251
    /**
252
     * The HKDF-SHA-3-384 type.
253
     */
254
    const HKDF_SHA3_384 = HkdfShaThree384::class;
255
256
    /**
257
     * The PBKDF2-SHA-3-384 type.
258
     */
259
    const PBKDF2_SHA3_384 = Pbkdf2ShaThree384::class;
260
261
    /**
262
     * The SHA-3-512 type.
263
     */
264
    const SHA3_512 = ShaThree512::class;
265
266
    /**
267
     * The HMAC-SHA-3-512 type.
268
     */
269
    const HMAC_SHA3_512 = HmacShaThree512::class;
270
271
    /**
272
     * The HKDF-SHA-3-512 type.
273
     */
274
    const HKDF_SHA3_512 = HkdfShaThree512::class;
275
276
    /**
277
     * The PBKDF2-SHA-3-512 type.
278
     */
279
    const PBKDF2_SHA3_512 = Pbkdf2ShaThree512::class;
280
281
    /**
282
     * The RIPEMD-128 type.
283
     */
284
    const RIPEMD_128 = Ripemd128::class;
285
286
    /**
287
     * The HMAC-RIPEMD-128 type.
288
     */
289
    const HMAC_RIPEMD_128 = HmacRipemd128::class;
290
291
    /**
292
     * The HKDF-RIPEMD-128 type.
293
     */
294
    const HKDF_RIPEMD_128 = HkdfRipemd128::class;
295
296
    /**
297
     * The PBKDF2-RIPEMD-128 type.
298
     */
299
    const PBKDF2_RIPEMD_128 = Pbkdf2Ripemd128::class;
300
301
    /**
302
     * The RIPEMD-160 type.
303
     */
304
    const RIPEMD_160 = Ripemd160::class;
305
306
    /**
307
     * The HMAC-RIPEMD-160 type.
308
     */
309
    const HMAC_RIPEMD_160 = HmacRipemd160::class;
310
311
    /**
312
     * The HKDF-RIPEMD-160 type.
313
     */
314
    const HKDF_RIPEMD_160 = HkdfRipemd160::class;
315
316
    /**
317
     * The PBKDF2-RIPEMD-160 type.
318
     */
319
    const PBKDF2_RIPEMD_160 = Pbkdf2Ripemd160::class;
320
321
    /**
322
     * The RIPEMD-256 type.
323
     */
324
    const RIPEMD_256 = Ripemd256::class;
325
326
    /**
327
     * The HMAC-RIPEMD-256 type.
328
     */
329
    const HMAC_RIPEMD_256 = HmacRipemd256::class;
330
331
    /**
332
     * The HKDF-RIPEMD-256 type.
333
     */
334
    const HKDF_RIPEMD_256 = HkdfRipemd256::class;
335
336
    /**
337
     * The PBKDF2-RIPEMD-256 type.
338
     */
339
    const PBKDF2_RIPEMD_256 = Pbkdf2Ripemd256::class;
340
341
    /**
342
     * The RIPEMD-320 type.
343
     */
344
    const RIPEMD_320 = Ripemd320::class;
345
346
    /**
347
     * The HMAC-RIPEMD-320 type.
348
     */
349
    const HMAC_RIPEMD_320 = HmacRipemd320::class;
350
351
    /**
352
     * The HKDF-RIPEMD-320 type.
353
     */
354
    const HKDF_RIPEMD_320 = HkdfRipemd320::class;
355
356
    /**
357
     * The PBKDF2-RIPEMD-320 type.
358
     */
359
    const PBKDF2_RIPEMD_320 = Pbkdf2Ripemd320::class;
360
361
    /**
362
     * The Whirlpool type.
363
     */
364
    const WHIRLPOOL = Whirlpool::class;
365
366
    /**
367
     * The HMAC-Whirlpool type.
368
     */
369
    const HMAC_WHIRLPOOL = HmacWhirlpool::class;
370
371
    /**
372
     * The HKDF-Whirlpool type.
373
     */
374
    const HKDF_WHIRLPOOL = HkdfWhirlpool::class;
375
376
    /**
377
     * The PBKDF2-Whirlpool type.
378
     */
379
    const PBKDF2_WHIRLPOOL = Pbkdf2Whirlpool::class;
380
381
    /**
382
     * The Bcrypt type.
383
     */
384
    const BCRYPT = Bcrypt::class;
385
386
    /**
387
     * The Argon2 type.
388
     */
389
    const ARGON2 = Argon2::class;
390
391
    /**
392
     * Create a hash algorithm object.
393
     *
394
     * @param string|null $type The algorithm class name as type for creation.
395
     *
396
     * @return HashAlgorithm|object|null A hash algorithm object or null.
397
     */
398 6
    public function create($type)
399
    {
400 6
        return self::createInstance($type);
401
    }
402
403
    /**
404
     * Create a hash algorithm object
405
     *
406
     * @param string|null $type The algorithm class name as type for creation.
407
     *
408
     * @return HashAlgorithm|object|null A hash algorithm object or null.
409
     */
410 8
    public static function createInstance($type)
411
    {
412
        /**
413
         * Check if class exists and has a correct base class
414
         *
415
         * @var HashAlgorithm|null $exception Object instance.
416
         */
417 8
        if (class_exists($type) && is_subclass_of($type, HashAlgorithm::class)) {
418 8
            $exception = new $type();
419
        } else {
420 4
            $exception = null; // Invalid type given
421
        }
422
423 8
        return $exception;
424
    }
425
426
    /**
427
     * Get debug information for the class instance.
428
     *
429
     * @return array Debug information.
430
     */
431 2
    public function __debugInfo()
432
    {
433
        return [
434 2
            self::class . '::MD5' => self::MD5,
435 1
            self::class . '::HMAC_MD5' => self::HMAC_MD5,
436 1
            self::class . '::HKDF_MD5' => self::HKDF_MD5,
437 1
            self::class . '::PBKDF2_MD5' => self::PBKDF2_MD5,
438 1
            self::class . '::SHA1' => self::SHA1,
439 1
            self::class . '::SHA1' => self::HMAC_SHA1,
440 1
            self::class . '::SHA1' => self::HKDF_SHA1,
441 1
            self::class . '::SHA1' => self::PBKDF2_SHA1,
442 1
            self::class . '::SHA2_224' => self::SHA2_224,
443 1
            self::class . '::HMAC_SHA2_224' => self::HMAC_SHA2_224,
444 1
            self::class . '::HKDF_SHA2_224' => self::HKDF_SHA2_224,
445 1
            self::class . '::PBKDF2_SHA2_224' => self::PBKDF2_SHA2_224,
446 1
            self::class . '::SHA2_256' => self::SHA2_256,
447 1
            self::class . '::HMAC_SHA2_256' => self::HMAC_SHA2_256,
448 1
            self::class . '::HKDF_SHA2_256' => self::HKDF_SHA2_256,
449 1
            self::class . '::PBKDF2_SHA2_256' => self::PBKDF2_SHA2_256,
450 1
            self::class . '::SHA2_384' => self::SHA2_384,
451 1
            self::class . '::HMAC_SHA2_384' => self::HMAC_SHA2_384,
452 1
            self::class . '::HKDF_SHA2_384' => self::HKDF_SHA2_384,
453 1
            self::class . '::PBKDF2_SHA2_384' => self::PBKDF2_SHA2_384,
454 1
            self::class . '::SHA2_512' => self::SHA2_512,
455 1
            self::class . '::HMAC_SHA2_512' => self::HMAC_SHA2_512,
456 1
            self::class . '::HKDF_SHA2_512' => self::HKDF_SHA2_512,
457 1
            self::class . '::PBKDF2_SHA2_512' => self::PBKDF2_SHA2_512,
458 1
            self::class . '::SHA3_224' => self::SHA3_224,
459 1
            self::class . '::HMAC_SHA3_224' => self::HMAC_SHA3_224,
460 1
            self::class . '::HKDF_SHA3_224' => self::HKDF_SHA3_224,
461 1
            self::class . '::PBKDF2_SHA3_224' => self::PBKDF2_SHA3_224,
462 1
            self::class . '::SHA3_256' => self::SHA3_256,
463 1
            self::class . '::HMAC_SHA3_256' => self::HMAC_SHA3_256,
464 1
            self::class . '::HKDF_SHA3_256' => self::HKDF_SHA3_256,
465 1
            self::class . '::PBKDF2_SHA3_256' => self::PBKDF2_SHA3_256,
466 1
            self::class . '::SHA3_384' => self::SHA3_384,
467 1
            self::class . '::HMAC_SHA3_384' => self::HMAC_SHA3_384,
468 1
            self::class . '::HKDF_SHA3_384' => self::HKDF_SHA3_384,
469 1
            self::class . '::PBKDF2_SHA3_384' => self::PBKDF2_SHA3_384,
470 1
            self::class . '::SHA3_512' => self::SHA3_512,
471 1
            self::class . '::HMAC_SHA3_512' => self::HMAC_SHA3_512,
472 1
            self::class . '::HKDF_SHA3_512' => self::HKDF_SHA3_512,
473 1
            self::class . '::PBKDF2_SHA3_512' => self::PBKDF2_SHA3_512,
474 1
            self::class . '::RIPEMD_128' => self::RIPEMD_128,
475 1
            self::class . '::HMAC_RIPEMD_128' => self::HMAC_RIPEMD_128,
476 1
            self::class . '::HKDF_RIPEMD_128' => self::HKDF_RIPEMD_128,
477 1
            self::class . '::PBKDF2_RIPEMD_128' => self::PBKDF2_RIPEMD_128,
478 1
            self::class . '::RIPEMD_160' => self::RIPEMD_160,
479 1
            self::class . '::HMAC_RIPEMD_160' => self::HMAC_RIPEMD_160,
480 1
            self::class . '::HKDF_RIPEMD_160' => self::HKDF_RIPEMD_160,
481 1
            self::class . '::PBKDF2_RIPEMD_160' => self::PBKDF2_RIPEMD_160,
482 1
            self::class . '::RIPEMD_256' => self::RIPEMD_256,
483 1
            self::class . '::HMAC_RIPEMD_256' => self::HMAC_RIPEMD_256,
484 1
            self::class . '::HKDF_RIPEMD_256' => self::HKDF_RIPEMD_256,
485 1
            self::class . '::PBKDF2_RIPEMD_256' => self::PBKDF2_RIPEMD_256,
486 1
            self::class . '::RIPEMD_320' => self::RIPEMD_320,
487 1
            self::class . '::HMAC_RIPEMD_320' => self::HMAC_RIPEMD_320,
488 1
            self::class . '::HKDF_RIPEMD_320' => self::HKDF_RIPEMD_320,
489 1
            self::class . '::PBKDF2_RIPEMD_320' => self::PBKDF2_RIPEMD_320,
490 1
            self::class . '::WHIRLPOOL' => self::WHIRLPOOL,
491 1
            self::class . '::HMAC_WHIRLPOOL' => self::HMAC_WHIRLPOOL,
492 1
            self::class . '::HKDF_WHIRLPOOL' => self::HKDF_WHIRLPOOL,
493 1
            self::class . '::PBKDF2_WHIRLPOOL' => self::PBKDF2_WHIRLPOOL,
494 1
            self::class . '::BCRYPT' => self::BCRYPT,
495 2
            self::class . '::ARGON2' => self::ARGON2,
496
        ];
497
    }
498
}
499