Completed
Push — master ( 687aa4...46f89c )
by Tony Karavasilev (Тони
19:17
created

HashAlgorithmFactory::__debugInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
     * Get the array of containing all supported unkeyed hash algorithms by the factory.
393
     *
394
     * @return array An array of available unkeyed hash algorithms.
395
     */
396 2
    protected static function getUnkeyedHashAlgorithms()
397
    {
398
        return [
399 2
            self::class . '::MD5' => self::MD5,
400 1
            self::class . '::SHA1' => self::SHA1,
401 1
            self::class . '::SHA2_224' => self::SHA2_224,
402 1
            self::class . '::SHA2_256' => self::SHA2_256,
403 1
            self::class . '::SHA2_384' => self::SHA2_384,
404 1
            self::class . '::SHA2_512' => self::SHA2_512,
405 1
            self::class . '::SHA3_224' => self::SHA3_224,
406 1
            self::class . '::SHA3_256' => self::SHA3_256,
407 1
            self::class . '::SHA3_384' => self::SHA3_384,
408 1
            self::class . '::SHA3_512' => self::SHA3_512,
409 1
            self::class . '::RIPEMD_128' => self::RIPEMD_128,
410 1
            self::class . '::RIPEMD_160' => self::RIPEMD_160,
411 1
            self::class . '::RIPEMD_256' => self::RIPEMD_256,
412 1
            self::class . '::RIPEMD_320' => self::RIPEMD_320,
413 2
            self::class . '::WHIRLPOOL' => self::WHIRLPOOL,
414
        ];
415
    }
416
417
    /**
418
     * Get the array of containing all supported keyed hash algorithms by the factory.
419
     *
420
     * @return array An array of available keyed hash algorithms.
421
     */
422 2
    protected static function getKeyedHashAlgorithms()
423
    {
424
        return [
425 2
            self::class . '::HMAC_MD5' => self::HMAC_MD5,
426 1
            self::class . '::HMAC_SHA1' => self::HMAC_SHA1,
427 1
            self::class . '::HMAC_SHA2_224' => self::HMAC_SHA2_224,
428 1
            self::class . '::HMAC_SHA2_256' => self::HMAC_SHA2_256,
429 1
            self::class . '::HMAC_SHA2_384' => self::HMAC_SHA2_384,
430 1
            self::class . '::HMAC_SHA2_512' => self::HMAC_SHA2_512,
431 1
            self::class . '::HMAC_SHA3_224' => self::HMAC_SHA3_224,
432 1
            self::class . '::HMAC_SHA3_256' => self::HMAC_SHA3_256,
433 1
            self::class . '::HMAC_SHA3_384' => self::HMAC_SHA3_384,
434 1
            self::class . '::HMAC_SHA3_512' => self::HMAC_SHA3_512,
435 1
            self::class . '::HMAC_RIPEMD_128' => self::HMAC_RIPEMD_128,
436 1
            self::class . '::HMAC_RIPEMD_160' => self::HMAC_RIPEMD_160,
437 1
            self::class . '::HMAC_RIPEMD_256' => self::HMAC_RIPEMD_256,
438 1
            self::class . '::HMAC_RIPEMD_320' => self::HMAC_RIPEMD_320,
439 2
            self::class . '::HMAC_WHIRLPOOL' => self::HMAC_WHIRLPOOL,
440
        ];
441
    }
442
443
    /**
444
     * Get the array of containing all supported key derivation algorithms by the factory.
445
     *
446
     * @return array An array of available key derivation algorithms.
447
     */
448 2
    protected static function getKeyDerivationAlgorithms()
449
    {
450
        return [
451 2
            self::class . '::HKDF_MD5' => self::HKDF_MD5,
452 1
            self::class . '::HKDF_SHA1' => self::HKDF_SHA1,
453 1
            self::class . '::HKDF_SHA2_224' => self::HKDF_SHA2_224,
454 1
            self::class . '::HKDF_SHA2_256' => self::HKDF_SHA2_256,
455 1
            self::class . '::HKDF_SHA2_384' => self::HKDF_SHA2_384,
456 1
            self::class . '::HKDF_SHA2_512' => self::HKDF_SHA2_512,
457 1
            self::class . '::HKDF_SHA3_224' => self::HKDF_SHA3_224,
458 1
            self::class . '::HKDF_SHA3_256' => self::HKDF_SHA3_256,
459 1
            self::class . '::HKDF_SHA3_384' => self::HKDF_SHA3_384,
460 1
            self::class . '::HKDF_SHA3_512' => self::HKDF_SHA3_512,
461 1
            self::class . '::HKDF_RIPEMD_128' => self::HKDF_RIPEMD_128,
462 1
            self::class . '::HKDF_RIPEMD_160' => self::HKDF_RIPEMD_160,
463 1
            self::class . '::HKDF_RIPEMD_256' => self::HKDF_RIPEMD_256,
464 1
            self::class . '::HKDF_RIPEMD_320' => self::HKDF_RIPEMD_320,
465 2
            self::class . '::HKDF_WHIRLPOOL' => self::HKDF_WHIRLPOOL,
466
        ];
467
    }
468
469
    /**
470
     * Get the array of containing all supported password-based derivation algorithms by the factory.
471
     *
472
     * @return array An array of available password-based derivation algorithms.
473
     */
474 2
    protected static function getPasswordDerivationAlgorithms()
475
    {
476
        return [
477 2
            self::class . '::PBKDF2_MD5' => self::PBKDF2_MD5,
478 1
            self::class . '::PBKDF2_SHA1' => self::PBKDF2_SHA1,
479 1
            self::class . '::PBKDF2_SHA2_224' => self::PBKDF2_SHA2_224,
480 1
            self::class . '::PBKDF2_SHA2_256' => self::PBKDF2_SHA2_256,
481 1
            self::class . '::PBKDF2_SHA2_384' => self::PBKDF2_SHA2_384,
482 1
            self::class . '::PBKDF2_SHA2_512' => self::PBKDF2_SHA2_512,
483 1
            self::class . '::PBKDF2_SHA3_224' => self::PBKDF2_SHA3_224,
484 1
            self::class . '::PBKDF2_SHA3_256' => self::PBKDF2_SHA3_256,
485 1
            self::class . '::PBKDF2_SHA3_384' => self::PBKDF2_SHA3_384,
486 1
            self::class . '::PBKDF2_SHA3_512' => self::PBKDF2_SHA3_512,
487 1
            self::class . '::PBKDF2_RIPEMD_128' => self::PBKDF2_RIPEMD_128,
488 1
            self::class . '::PBKDF2_RIPEMD_160' => self::PBKDF2_RIPEMD_160,
489 1
            self::class . '::PBKDF2_RIPEMD_256' => self::PBKDF2_RIPEMD_256,
490 1
            self::class . '::PBKDF2_RIPEMD_320' => self::PBKDF2_RIPEMD_320,
491 1
            self::class . '::PBKDF2_WHIRLPOOL' => self::PBKDF2_WHIRLPOOL,
492 1
            self::class . '::BCRYPT' => self::BCRYPT,
493 2
            self::class . '::ARGON2' => self::ARGON2,
494
        ];
495
    }
496
497
    /**
498
     * Create a hash algorithm object.
499
     *
500
     * @param string|null $type The algorithm class name as type for creation.
501
     *
502
     * @return HashAlgorithm|object|null A hash algorithm object or null.
503
     */
504 6
    public function create($type)
505
    {
506 6
        return self::createInstance($type);
507
    }
508
509
    /**
510
     * Create a hash algorithm object
511
     *
512
     * @param string|null $type The algorithm class name as type for creation.
513
     *
514
     * @return HashAlgorithm|object|null A hash algorithm object or null.
515
     */
516 8
    public static function createInstance($type)
517
    {
518
        /**
519
         * Check if class exists and has a correct base class
520
         *
521
         * @var HashAlgorithm|null $exception Object instance.
522
         */
523 8
        if (class_exists($type) && is_subclass_of($type, HashAlgorithm::class)) {
524 8
            $exception = new $type();
525
        } else {
526 4
            $exception = null; // Invalid type given
527
        }
528
529 8
        return $exception;
530
    }
531
532
    /**
533
     * Get debug information for the class instance.
534
     *
535
     * @return array Debug information.
536
     */
537 2
    public function __debugInfo()
538
    {
539 2
        $supportedAlgorithms = array_merge(self::getUnkeyedHashAlgorithms(), self::getKeyedHashAlgorithms());
540 2
        $supportedAlgorithms = array_merge($supportedAlgorithms, self::getKeyDerivationAlgorithms());
541 2
        $supportedAlgorithms = array_merge($supportedAlgorithms, self::getPasswordDerivationAlgorithms());
542
543 2
        return $supportedAlgorithms;
544
    }
545
}
546