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

AbstractUnkeyedHashFunction::hashFile()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 31
ccs 17
cts 17
cp 1
rs 9.0444
cc 6
nc 6
nop 1
crap 6
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\Traits\MessageDigestion\FileHashingTrait as HashFiles;
14
15
/**
16
 * Class AbstractUnkeyedHashFunction - Abstraction for unkeyed hash classes.
17
 *
18
 * @package CryptoManana\Core\Abstractions\MessageDigestion
19
 *
20
 * @mixin HashObjects
21
 * @mixin HashFiles
22
 */
23
abstract class AbstractUnkeyedHashFunction extends HashAlgorithm implements ObjectHashing, FileHashing
24
{
25
    /**
26
     * Object hashing capabilities.
27
     *
28
     * {@internal Reusable implementation of `ObjectHashingInterface`. }}
29
     */
30
    use HashObjects;
31
32
    /**
33
     * File hashing capabilities.
34
     *
35
     * {@internal Reusable implementation of `FileHashingInterface`. }}
36
     */
37
    use HashFiles;
38
39
    /**
40
     * The internal name of the algorithm.
41
     */
42
    const ALGORITHM_NAME = 'none';
43
44
    /**
45
     * Flag to force native code polyfill realizations, if available.
46
     *
47
     * @var bool Flag to force native realizations.
48
     */
49
    protected $useNative = false;
50
51
    /**
52
     * Unkeyed hash algorithm constructor.
53
     */
54 374
    public function __construct()
55
    {
56 374
    }
57
58
    /**
59
     * Calculates a hash value for the given data.
60
     *
61
     * @param string $data The input string.
62
     *
63
     * @return string The digest.
64
     * @throws \Exception Validation errors.
65
     */
66 140
    public function hashData($data)
67
    {
68 140
        if (!is_string($data)) {
69 14
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
70
        }
71
72 126
        $data = $this->addSaltString($data);
73
74 126
        $digest = hash(
75 126
            static::ALGORITHM_NAME,
76
            $data,
77 126
            ($this->digestFormat === self::DIGEST_OUTPUT_RAW)
78
        );
79
80 126
        $digest = $this->changeOutputFormat($digest);
81
82 126
        return $digest;
83
    }
84
85
    /**
86
     * Calculates a hash value for the content of the given filename and location.
87
     *
88
     * @param string $filename The full path and name of the file for hashing.
89
     *
90
     * @return string The digest.
91
     * @throws \Exception Validation errors.
92
     */
93 66
    public function hashFile($filename)
94
    {
95 66
        if (!is_string($filename)) {
96 22
            throw new \InvalidArgumentException('The file path must be of type string.');
97
        }
98
99 44
        $this->validateFileNamePath($filename);
100
101 22
        $useFileSalting = $this->isFileSaltingForcingNativeHashing();
102
103 22
        if ($this->useNative || $useFileSalting) {
104 22
            $oldSalt = $this->salt;
105 22
            $oldMode = $this->saltingMode;
106
107 22
            $this->salt = ($useFileSalting) ? $this->salt : '';
108 22
            $this->saltingMode = ($useFileSalting) ? $this->saltingMode : self::SALTING_MODE_NONE;
109
110 22
            $digest = $this->hashData(file_get_contents($filename));
111
112 22
            $this->setSalt($oldSalt)->setSaltingMode($oldMode);
113
        } else {
114 22
            $digest = hash_file(
115 22
                static::ALGORITHM_NAME,
116
                $filename,
117 22
                ($this->digestFormat === self::DIGEST_OUTPUT_RAW)
118
            );
119
120 22
            $digest = $this->changeOutputFormat($digest);
121
        }
122
123 22
        return $digest;
124
    }
125
126
    /**
127
     * Get debug information for the class instance.
128
     *
129
     * @return array Debug information.
130
     */
131 22
    public function __debugInfo()
132
    {
133
        return [
134 22
            'standard' => static::ALGORITHM_NAME,
135 22
            'type' => 'unkeyed digestion or checksum',
136 22
            'salt' => $this->salt,
137 22
            'mode' => $this->saltingMode,
138
        ];
139
    }
140
}
141