Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit c70bbeb

Browse files
committed
Merge pull request #277 from webimpress/hotfix/hash-validator
Fixes File\Hash validator to work with digit-only hashes and match algo
2 parents f4bfe8b + 40a1976 commit c70bbeb

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/File/Hash.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ public function isValid($value, $file = null)
148148
return false;
149149
}
150150

151-
$algos = array_unique(array_values($this->getHash()));
152-
$hashes = array_unique(array_keys($this->getHash()));
153-
151+
$algos = array_unique(array_values($this->getHash()));
154152
foreach ($algos as $algorithm) {
155153
$filehash = hash_file($algorithm, $fileInfo['file']);
156154

@@ -159,10 +157,8 @@ public function isValid($value, $file = null)
159157
return false;
160158
}
161159

162-
foreach ($hashes as $hash) {
163-
if ($filehash === $hash) {
164-
return true;
165-
}
160+
if (isset($this->getHash()[$filehash]) && $this->getHash()[$filehash] === $algorithm) {
161+
return true;
166162
}
167163
}
168164

test/File/HashTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
namespace ZendTest\Validator\File;
1111

1212
use PHPUnit\Framework\TestCase;
13-
use Zend\Validator\File;
1413
use ReflectionProperty;
1514
use Zend\Validator\Exception\InvalidArgumentException;
15+
use Zend\Validator\File;
1616

1717
/**
1818
* Hash testbed
@@ -253,4 +253,21 @@ public function testInvalidHashProvidedInArrayFormat($hash)
253253
$this->expectExceptionMessage('Hash must be a string');
254254
$validator->addHash([$hash]);
255255
}
256+
257+
public function testIntHash()
258+
{
259+
$validator = new File\Hash('10713230');
260+
261+
self::assertTrue($validator->isValid(__DIR__ . '/_files/crc32-int.pdf'));
262+
}
263+
264+
public function testHashMustMatchWithTheAlgorithm()
265+
{
266+
$validator = new File\Hash();
267+
// swapped hashes for given algorithms
268+
$validator->addHash(['6507f172bceb9ed0cc59246d41569c4d', 'algorithm' => 'crc32']);
269+
$validator->addHash(['10713230', 'algorithm' => 'md5']);
270+
271+
self::assertFalse($validator->isValid(__DIR__ . '/_files/crc32-int.pdf'));
272+
}
256273
}

test/File/_files/crc32-int.pdf

1.04 MB
Binary file not shown.

0 commit comments

Comments
 (0)