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

Commit 9330e98

Browse files
committed
Merge branch 'hotfix/277'
Close #277 Fix #276
2 parents f4bfe8b + 31d593a commit 9330e98

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#277](https://github.com/zendframework/zend-validator/pull/277) fixes `File\Hash` validator in case
26+
when the file hash contains only digits.
27+
28+
- [#277](https://github.com/zendframework/zend-validator/pull/277) fixes `File\Hash` validator to match
29+
hash with the given hashing algorithm.
2630

2731
## 2.12.1 - 2019-10-12
2832

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)