Completed
Push — master ( a027a8...56c2be )
by Tony Karavasilev (Тони
08:06
created

AbstractUnkeyedHashFunction::hashObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * Abstraction for unkeyed hash objects like checksums and plain cryptographic hash functions.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\MessageDigestion;
8
9
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as HashAlgorithm;
10
use \CryptoManana\Core\Interfaces\MessageDigestion\ObjectHashingInterface as ObjectHashing;
11
use \CryptoManana\Core\Interfaces\MessageDigestion\FileHashingInterface as FileHashing;
12
use \CryptoManana\Core\Traits\MessageDigestion\ObjectHashingTrait as HashObjects;
13
use \CryptoManana\Core\StringBuilder as StringBuilder;
14
15
/**
16
 * Class AbstractUnkeyedHashFunction - Abstraction for unkeyed hash classes.
17
 *
18
 * @package CryptoManana\Core\Abstractions\MessageDigestion
19
 *
20
 * @mixin HashObjects
21
 */
22
abstract class AbstractUnkeyedHashFunction extends HashAlgorithm implements ObjectHashing, FileHashing
23
{
24
    /**
25
     * Object hashing capabilities.
26
     *
27
     * {@internal Reusable implementation of `ObjectHashingInterface`. }}
28
     */
29
    use HashObjects;
30
31
    /**
32
     * The internal name of the algorithm.
33
     */
34
    const ALGORITHM_NAME = 'none';
35
36
    /**
37
     * Flag to force native code polyfill realizations, if available.
38
     *
39
     * @var bool Flag to force native realizations.
40
     */
41
    protected $useNative = false;
42
43
    /**
44
     * Internal method for location and filename validation.
45
     *
46
     * @param string $filename The filename and location.
47
     *
48
     * @throws \Exception Validation errors.
49
     */
50 44
    protected function validateFileNamePath($filename)
51
    {
52 44
        $filename = StringBuilder::stringReplace("\0", '', $filename); // (ASCII 0 (0x00))
53 44
        $filename = realpath($filename); // Path traversal escape and absolute path fetching
54
55
        // Clear path cache
56 44
        if (!empty($filename)) {
57 22
            clearstatcache(true, $filename);
58
        }
59
60
        // Check if path is valid and the file is readable
61 44
        if ($filename === false || !file_exists($filename) || !is_readable($filename) || !is_file($filename)) {
62 22
            throw new \RuntimeException('File is not found or can not be accessed.');
63
        }
64 22
    }
65
66
    /**
67
     * Unkeyed hash algorithm constructor.
68
     */
69 374
    public function __construct()
70
    {
71 374
    }
72
73
    /**
74
     * Calculates a hash value for the given data.
75
     *
76
     * @param string $data The input string.
77
     *
78
     * @return string The digest.
79
     * @throws \Exception Validation errors.
80
     */
81 140
    public function hashData($data)
82
    {
83 140
        if (!is_string($data)) {
84 14
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
85
        }
86
87 126
        $data = $this->addSaltString($data);
88
89 126
        $digest = hash(
90 126
            static::ALGORITHM_NAME,
91
            $data,
92 126
            ($this->digestFormat === self::DIGEST_OUTPUT_RAW)
93
        );
94
95 126
        $digest = $this->changeOutputFormat($digest);
96
97 126
        return $digest;
98
    }
99
100
    /**
101
     * Calculates a hash value for the content of the given filename and location.
102
     *
103
     * @param string $filename The full path and name of the file for hashing.
104
     *
105
     * @return string The digest.
106
     * @throws \Exception Validation errors.
107
     */
108 66
    public function hashFile($filename)
109
    {
110
        // Validate input type
111 66
        if (!is_string($filename)) {
112 22
            throw new \InvalidArgumentException('The file path must be of type string.');
113
        }
114
115 44
        $this->validateFileNamePath($filename);
116
117 22
        if ($this->useNative) {
118 22
            $oldSalt = $this->getSalt();
119 22
            $oldMode = $this->getSaltingMode();
120
121 22
            $this->setSalt('')->setSaltingMode(self::SALTING_MODE_NONE);
122
123 22
            $digest = $this->hashData(file_get_contents($filename));
124
125 22
            $this->setSalt($oldSalt)->setSaltingMode($oldMode);
126
        } else {
127 22
            $digest = hash_file(
128 22
                static::ALGORITHM_NAME,
129
                $filename,
130 22
                ($this->digestFormat === self::DIGEST_OUTPUT_RAW)
131
            );
132
133 22
            $digest = $this->changeOutputFormat($digest);
134
        }
135
136 22
        return $digest;
137
    }
138
139
    /**
140
     * Get debug information for the class instance.
141
     *
142
     * @return array Debug information.
143
     */
144 22
    public function __debugInfo()
145
    {
146
        return [
147 22
            'standard' => static::ALGORITHM_NAME,
148 22
            'type' => 'unkeyed digestion or checksum',
149 22
            'salt' => $this->salt,
150 22
            'mode' => $this->saltingMode,
151
        ];
152
    }
153
}
154