Completed
Push — master ( 456c00...ec5070 )
by Tony Karavasilev (Тони
08:30
created

FileHashingTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 13
c 1
b 0
f 0
dl 0
loc 54
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateFileNamePath() 0 13 6
A isFileSaltingForcingNativeHashing() 0 11 4
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
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...rait::SALTING_MODE_NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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])
2 ignored issues
show
Bug introduced by
The constant CryptoManana\Core\Traits...DE_PALINDROME_MIRRORING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant CryptoManana\Core\Traits...SALTING_MODE_INFIX_SALT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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