|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of file hashing for digestion algorithms. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageDigestion; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageDigestion\FileHashingInterface as FileHashingSpecification; |
|
10
|
|
|
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as AnyDerivedHashAlgorithm; |
|
11
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait FileHashingTrait - Reusable implementation of `FileHashingInterface`. |
|
15
|
|
|
* |
|
16
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\FileHashingInterface The abstract specification. |
|
17
|
|
|
* |
|
18
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
|
19
|
|
|
* |
|
20
|
|
|
* @mixin FileHashingSpecification |
|
21
|
|
|
* @mixin AnyDerivedHashAlgorithm |
|
22
|
|
|
*/ |
|
23
|
|
|
trait FileHashingTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Internal method for location and filename validation. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $filename The filename and location. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Exception Validation errors. |
|
31
|
|
|
*/ |
|
32
|
88 |
|
protected function validateFileNamePath($filename) |
|
33
|
|
|
{ |
|
34
|
88 |
|
$filename = StringBuilder::stringReplace("\0", '', $filename); // (ASCII 0 (0x00)) |
|
35
|
88 |
|
$filename = realpath($filename); // Path traversal escape and absolute path fetching |
|
36
|
|
|
|
|
37
|
|
|
// Clear path cache |
|
38
|
88 |
|
if (!empty($filename)) { |
|
39
|
44 |
|
clearstatcache(true, $filename); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Check if path is valid and the file is readable |
|
43
|
88 |
|
if ($filename === false || !file_exists($filename) || !is_readable($filename) || !is_file($filename)) { |
|
44
|
44 |
|
throw new \RuntimeException('File is not found or can not be accessed.'); |
|
45
|
|
|
} |
|
46
|
44 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Internal method for checking if native file hashing should be used by force. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool Is native hashing needed for the current salting mode. |
|
52
|
|
|
*/ |
|
53
|
44 |
|
protected function isFileSaltingForcingNativeHashing() |
|
54
|
|
|
{ |
|
55
|
|
|
return ( |
|
56
|
|
|
( |
|
57
|
|
|
// If there is an non-empty salt string set and salting is enabled |
|
58
|
44 |
|
$this->salt !== '' && |
|
59
|
44 |
|
$this->saltingMode !== self::SALTING_MODE_NONE |
|
|
|
|
|
|
60
|
|
|
) || ( |
|
61
|
|
|
// If there is an empty salt string set and the salting mode duplicates/manipulates the input |
|
62
|
44 |
|
$this->salt === '' && |
|
63
|
44 |
|
in_array($this->saltingMode, [self::SALTING_MODE_INFIX_SALT, self::SALTING_MODE_PALINDROME_MIRRORING]) |
|
|
|
|
|
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Calculates a hash value for the content of the given filename and location. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $filename The full path and name of the file for hashing. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string The digest. |
|
74
|
|
|
* @throws \Exception Validation errors. |
|
75
|
|
|
*/ |
|
76
|
|
|
abstract public function hashFile($filename); |
|
77
|
|
|
} |
|
78
|
|
|
|