Completed
Push — master ( 115690...6ac2d3 )
by Rick
02:27
created

Hash::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 11
cts 12
cp 0.9167
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
crap 3.0052
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use Exception;
12
use Particle\Validator\Rule;
13
14
/**
15
 * This rule is for validating if a value is a valid cryptographic hash.
16
 *
17
 * @package Particle\Validator\Rule
18
 */
19
class Hash extends Rule
20
{
21
    const ALGO_MD5 = 'md5';
22
    const ALGO_SHA1 = 'sha1';
23
    const ALGO_SHA256 = 'sha256';
24
    const ALGO_SHA512 = 'sha512';
25
    const ALGO_CRC32 = 'crc32';
26
27
    /**
28
     * A constant that will be used when the value is not a valid cryptographic hash.
29
     */
30
    const INVALID_FORMAT = 'Hash::INVALID_FORMAT';
31
32
    /**
33
     * The message templates which can be returned by this validator.
34
     *
35
     * @var array
36
     */
37
    protected $messageTemplates = [
38
        self::INVALID_FORMAT => '{{ name }} must be a valid hash'
39
    ];
40
41
    /**
42
     * @var string
43
     */
44
    protected $hashAlgorithm;
45
46
    /**
47
     * @var bool
48
     */
49
    protected $allowUppercase;
50
51
    /**
52
     * Construct the Hash validator.
53
     *
54
     * @param string $hashAlgorithm
55
     * @param bool $allowUppercase
56
     */
57 12
    public function __construct($hashAlgorithm, $allowUppercase = false)
58
    {
59 12
        $this->hashAlgorithm = $hashAlgorithm;
60 12
        $this->allowUppercase = $allowUppercase;
61
62 12
        $this->messageTemplates = [
63 12
            self::INVALID_FORMAT => sprintf('{{ name }} must be a valid %s hash', $hashAlgorithm)
64 12
        ];
65 12
    }
66
67
    /**
68
     * Validates if the value is a valid cryptographic hash.
69
     *
70
     * @param mixed $value
71
     * @return bool
72
     * @throws Exception
73
     */
74 12
    public function validate($value)
75
    {
76
        $algorithmsLengths = [
77 12
            self::ALGO_MD5 => 32,
78 12
            self::ALGO_SHA1 => 40,
79 12
            self::ALGO_SHA256 => 64,
80 12
            self::ALGO_SHA512 => 128,
81 12
            self::ALGO_CRC32 => 8,
82 12
        ];
83
84 12
        if (!isset($algorithmsLengths[$this->hashAlgorithm])) {
85
            throw new Exception('an invalid hashAlgorithm has been provided.');
86
        }
87
88 12
        if ($this->validateHexString($value, $algorithmsLengths[$this->hashAlgorithm])) {
89 6
            return true;
90
        }
91
92 6
        return $this->error(self::INVALID_FORMAT);
93
    }
94
95
    /**
96
     * @param string $value
97
     * @param int $length
98
     *
99
     * @return bool
100
     */
101 12
    private function validateHexString($value, $length)
102
    {
103 12
        $caseSensitive = $this->allowUppercase ? 'i' : '';
104
105 12
        return preg_match(sprintf('/^[0-9a-f]{%s}$/%s', $length, $caseSensitive), $value) === 1;
106
    }
107
}
108