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 Particle\Validator\Rule; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This rule is for validating if a value is a valid cryptographic hash. |
15
|
|
|
* |
16
|
|
|
* @package Particle\Validator\Rule |
17
|
|
|
*/ |
18
|
|
|
class Hash extends Rule |
19
|
|
|
{ |
20
|
|
|
const ALGO_MD5 = 'md5'; |
21
|
|
|
const ALGO_SHA1 = 'sha1'; |
22
|
|
|
const ALGO_SHA256 = 'sha256'; |
23
|
|
|
const ALGO_SHA512 = 'sha512'; |
24
|
|
|
const ALGO_CRC32 = 'crc32'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A constant that will be used when the value is not a valid cryptographic hash. |
28
|
|
|
*/ |
29
|
|
|
const INVALID_FORMAT = 'Hash::INVALID_FORMAT'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The message templates which can be returned by this validator. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $messageTemplates = [ |
37
|
|
|
self::INVALID_FORMAT => '{{ name }} must be a valid hash' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $hashAlgorithm; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $allowUppercase; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Construct the Hash validator. |
52
|
|
|
* |
53
|
|
|
* @param string $hashAlgorithm |
54
|
|
|
* @param bool $allowUppercase |
55
|
|
|
*/ |
56
|
12 |
|
public function __construct($hashAlgorithm, $allowUppercase = false) |
57
|
|
|
{ |
58
|
12 |
|
$this->hashAlgorithm = $hashAlgorithm; |
59
|
12 |
|
$this->allowUppercase = $allowUppercase; |
60
|
|
|
|
61
|
12 |
|
$this->messageTemplates = [ |
62
|
12 |
|
self::INVALID_FORMAT => sprintf('{{ name }} must be a valid %s hash', $hashAlgorithm) |
63
|
12 |
|
]; |
64
|
12 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validates if the value is a valid cryptographic hash. |
68
|
|
|
* |
69
|
|
|
* @param mixed $value |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
12 |
|
public function validate($value) |
73
|
|
|
{ |
74
|
12 |
|
$caseSensitive = $this->allowUppercase ? 'i' : ''; |
75
|
|
|
|
76
|
12 |
|
switch ($this->hashAlgorithm) { |
77
|
12 |
View Code Duplication |
case self::ALGO_MD5: |
|
|
|
|
78
|
4 |
|
if (preg_match('/^[0-9a-f]{32}$/' . $caseSensitive, $value) === 1) { |
79
|
2 |
|
return true; |
80
|
|
|
} |
81
|
2 |
|
break; |
82
|
8 |
View Code Duplication |
case self::ALGO_SHA1: |
|
|
|
|
83
|
2 |
|
if (preg_match('/^[0-9a-f]{40}$/' . $caseSensitive, $value) === 1) { |
84
|
1 |
|
return true; |
85
|
|
|
} |
86
|
1 |
|
break; |
87
|
6 |
View Code Duplication |
case self::ALGO_SHA256: |
|
|
|
|
88
|
2 |
|
if (preg_match('/^[0-9a-f]{64}$/' . $caseSensitive, $value) === 1) { |
89
|
1 |
|
return true; |
90
|
|
|
} |
91
|
1 |
|
break; |
92
|
4 |
View Code Duplication |
case self::ALGO_SHA512: |
|
|
|
|
93
|
2 |
|
if (preg_match('/^[0-9a-f]{128}$/' . $caseSensitive, $value) === 1) { |
94
|
1 |
|
return true; |
95
|
|
|
} |
96
|
1 |
|
break; |
97
|
2 |
View Code Duplication |
case self::ALGO_CRC32: |
|
|
|
|
98
|
2 |
|
if (preg_match('/^[0-9a-f]{8}$/' . $caseSensitive, $value) === 1) { |
99
|
1 |
|
return true; |
100
|
|
|
} |
101
|
1 |
|
break; |
102
|
6 |
|
} |
103
|
|
|
|
104
|
6 |
|
return $this->error(self::INVALID_FORMAT); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.