HashValidation::getHashLength()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Mohamedahmed01\SimpleMerkele\Types;
3
4
use Mohamedahmed01\SimpleMerkele\Exceptions\InvalidHashException;
5
use Mohamedahmed01\SimpleMerkele\Exceptions\InvalidHashTypeException;
6
7
final class HashValidation
8
{
9
    private function __construct()
10
    {
11
    }
12
    private const HASH_TYPES =[
13
        'md4'=> 32,
14
        'md5'=> 32,
15
        'sha1'=> 40,
16
        'sha224'=> 56,
17
        'sha256'=> 64,
18
        'sha384'=> 96,
19
        'sha512/224'=> 56,
20
        'sha512/256'=> 64,
21
        'sha512'=> 128,
22
        'sha3-224'=> 56,
23
        'sha3-256'=> 64,
24
        'sha3-384'=> 96,
25
        'sha3-512'=> 128,
26
        'ripemd128'=> 32,
27
        'ripemd160'=> 40,
28
        'ripemd256'=> 64,
29
        'ripemd320'=> 80,
30
        'whirlpool'=> 128,
31
        'tiger128,3'=> 32,
32
        'tiger160,3'=> 40,
33
        'tiger192,3'=> 48,
34
        'tiger128,4'=> 32,
35
        'tiger160,4'=> 40,
36
        'tiger192,4'=> 48,
37
        'snefru'=> 64,
38
        'snefru256'=> 64,
39
        'gost'=> 64,
40
        'gost-crypto'=> 64,
41
        'adler32'=> 8,
42
        'crc32'=> 8,
43
        'crc32b'=> 8,
44
        'crc32c'=> 8,
45
        'fnv132'=> 8,
46
        'fnv1a32'=> 8,
47
        'fnv164'=> 16,
48
        'fnv1a64'=> 16,
49
        'joaat'=> 8,
50
        'haval128,3'=> 32,
51
        'haval160,3'=> 40,
52
        'haval192,3'=> 48,
53
        'haval224,3'=> 56,
54
        'haval256,3'=> 64,
55
        'haval128,4'=> 32,
56
        'haval160,4'=> 40,
57
        'haval192,4'=> 48,
58
        'haval224,4'=> 56,
59
        'haval256,4'=> 64,
60
        'haval128,5'=> 32,
61
        'haval160,5'=> 40,
62
        'haval192,5'=> 48,
63
        'haval224,5'=> 56,
64
        'haval256,5'=> 64,
65
];
66
    private const HASH_REGEX= '/[0-9a-f]/i';
67
68
    /**
69
     * @SuppressWarnings(PHPMD.StaticAccess)
70
     */
71 21
    public static function getHashLength(String $type)
72
    {
73 21
        if (!array_key_exists($type, self::HASH_TYPES)) {
74 3
            throw new InvalidHashTypeException();
75
        }
76 18
        return self::HASH_TYPES[$type];
77
    }
78
    
79
    /**
80
     * @SuppressWarnings(PHPMD.StaticAccess)
81
     */
82 15
    public static function checkHashLength(String $hash, String $type)
83
    {
84 15
        if (self::getHashLength($type)!== strlen($hash)) {
85 3
            throw new InvalidHashException();
86
        }
87 12
    }
88
89
    /**
90
     * @SuppressWarnings(PHPMD.StaticAccess)
91
     */
92 24
    public static function validateHashString(String $hash)
93
    {
94 24
        if (!preg_match(self::HASH_REGEX, $hash)) {
95 6
            throw new InvalidHashException();
96
        }
97 18
    }
98
    /**
99
     * @SuppressWarnings(PHPMD.StaticAccess)
100
     */
101 39
    public static function validHashingAlgo(String $algo)
102
    {
103 39
        if (!array_key_exists($algo, self::HASH_TYPES)) {
104 6
            throw new InvalidHashTypeException();
105
        }
106 33
    }
107
}
108