|
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\Traits\CommonValidations\FileNameValidationTrait as ValidateFileNames; |
|
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
|
|
|
* @mixin ValidateFileNames |
|
23
|
|
|
*/ |
|
24
|
|
|
trait FileHashingTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* File name and path validations. |
|
28
|
|
|
* |
|
29
|
|
|
* {@internal Reusable implementation of the common file name validation. }} |
|
30
|
|
|
*/ |
|
31
|
|
|
use ValidateFileNames; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Internal method for checking if native file hashing should be used by force. |
|
35
|
|
|
* |
|
36
|
|
|
* @return bool Is native hashing needed for the current salting mode. |
|
37
|
|
|
*/ |
|
38
|
44 |
|
protected function isFileSaltingForcingNativeHashing() |
|
39
|
|
|
{ |
|
40
|
|
|
return ( |
|
41
|
|
|
( |
|
42
|
|
|
// If there is an non-empty salt string set and salting is enabled |
|
43
|
44 |
|
$this->salt !== '' && |
|
44
|
44 |
|
$this->saltingMode !== self::SALTING_MODE_NONE |
|
45
|
|
|
) || ( |
|
46
|
|
|
// If there is an empty salt string set and the salting mode duplicates/manipulates the input |
|
47
|
44 |
|
$this->salt === '' && |
|
48
|
44 |
|
in_array($this->saltingMode, [self::SALTING_MODE_INFIX_SALT, self::SALTING_MODE_PALINDROME_MIRRORING]) |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Calculates a hash value for the content of the given filename and location. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $filename The full path and name of the file for hashing. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string The digest. |
|
59
|
|
|
* @throws \Exception Validation errors. |
|
60
|
|
|
*/ |
|
61
|
|
|
abstract public function hashFile($filename); |
|
62
|
|
|
} |
|
63
|
|
|
|