1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Laminas\Validator\File;
use Laminas\Validator\AbstractValidator;
use Laminas\Validator\Exception;
use function array_key_exists;
use function array_unique;
use function array_values;
use function func_get_arg;
use function func_num_args;
use function get_class;
use function gettype;
use function hash_algos;
use function hash_file;
use function in_array;
use function is_array;
use function is_object;
use function is_readable;
use function is_scalar;
use function is_string;
use function sprintf;
/**
* Validator for the hash of given files
*/
class Hash extends AbstractValidator
{
use FileInformationTrait;
/**
* @const string Error constants
*/
public const DOES_NOT_MATCH = 'fileHashDoesNotMatch';
public const NOT_DETECTED = 'fileHashHashNotDetected';
public const NOT_FOUND = 'fileHashNotFound';
/** @var array Error message templates */
protected $messageTemplates = [
self::DOES_NOT_MATCH => 'File does not match the given hashes',
self::NOT_DETECTED => 'A hash could not be evaluated for the given file',
self::NOT_FOUND => 'File is not readable or does not exist',
];
/**
* Options for this validator
*
* @var string
*/
protected $options = [
'algorithm' => 'crc32',
'hash' => null,
];
/**
* Sets validator options
*
* @param string|array $options
*/
public function __construct($options = null)
{
if (
is_scalar($options) ||
(is_array($options) && ! array_key_exists('hash', $options))
) {
$options = ['hash' => $options];
}
if (1 < func_num_args()) {
$options['algorithm'] = func_get_arg(1);
}
parent::__construct($options);
}
/**
* Returns the set hash values as array, the hash as key and the algorithm the value
*
* @return array
*/
public function getHash()
{
return $this->options['hash'];
}
/**
* Sets the hash for one or multiple files
*
* @param string|array $options
* @return $this Provides a fluent interface
*/
public function setHash($options)
{
$this->options['hash'] = null;
$this->addHash($options);
return $this;
}
/**
* Adds the hash for one or multiple files
*
* @param string|array $options
* @throws Exception\InvalidArgumentException
* @return $this Provides a fluent interface
*/
public function addHash($options)
{
if (is_string($options)) {
$options = [$options];
} elseif (! is_array($options)) {
throw new Exception\InvalidArgumentException('False parameter given');
}
$known = hash_algos();
if (! isset($options['algorithm'])) {
$algorithm = $this->options['algorithm'];
} else {
$algorithm = $options['algorithm'];
unset($options['algorithm']);
}
if (! in_array($algorithm, $known)) {
throw new Exception\InvalidArgumentException("Unknown algorithm '{$algorithm}'");
}
foreach ($options as $value) {
if (! is_string($value)) {
throw new Exception\InvalidArgumentException(sprintf(
'Hash must be a string, %s received',
is_object($value) ? get_class($value) : gettype($value)
));
}
$this->options['hash'][$value] = $algorithm;
}
return $this;
}
/**
* Returns true if and only if the given file confirms the set hash
*
* @param string|array $value File to check for hash
* @param array $file File data from \Laminas\File\Transfer\Transfer (optional)
* @return bool
*/
public function isValid($value, $file = null)
{
$fileInfo = $this->getFileInfo($value, $file);
$this->setValue($fileInfo['filename']);
// Is file readable ?
if (empty($fileInfo['file']) || false === is_readable($fileInfo['file'])) {
$this->error(self::NOT_FOUND);
return false;
}
$algos = array_unique(array_values($this->getHash()));
foreach ($algos as $algorithm) {
$filehash = hash_file($algorithm, $fileInfo['file']);
if ($filehash === false) {
$this->error(self::NOT_DETECTED);
return false;
}
if (isset($this->getHash()[$filehash]) && $this->getHash()[$filehash] === $algorithm) {
return true;
}
}
$this->error(self::DOES_NOT_MATCH);
return false;
}
}