ShaThree512::hashData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * The SHA-3 family SHA-512 hashing algorithm class.
5
 */
6
7
namespace CryptoManana\Hashing;
8
9
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractUnkeyedHashFunction as UnkeyedHashAlgorithm;
10
11
/**
12
 * Class ShaThree512 - The SHA-3 family SHA-512 hashing algorithm object.
13
 *
14
 * @package CryptoManana\Hashing
15
 */
16
class ShaThree512 extends UnkeyedHashAlgorithm
17
{
18
    /**
19
     * The internal name of the algorithm.
20
     */
21
    const ALGORITHM_NAME = 'sha3-512';
22
23
    /**
24
     * Checksum digestion algorithm constructor.
25
     */
26 22
    public function __construct()
27
    {
28 22
        parent::__construct();
29
30 22
        $this->useNative = !in_array(static::ALGORITHM_NAME, hash_algos(), true);
31
    }
32
33
    /**
34
     * Calculates a hash value for the given data.
35
     *
36
     * @param string $data The input string.
37
     *
38
     * @return string The digest.
39
     * @throws \Exception Validation errors.
40
     */
41 12
    public function hashData($data)
42
    {
43 12
        if (!is_string($data)) {
44 2
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
45
        }
46
47 10
        $data = $this->addSaltString($data);
48
49 10
        $rawOutput = ($this->digestFormat === self::DIGEST_OUTPUT_RAW);
50
51
        /**
52
         * {@internal Backward compatibility native realization for SHA-3 must be used. }}
53
         */
54 10
        $digest = ($this->useNative) ?
55 2
            \CryptoManana\Compatibility\NativeSha3::digest512($data, $rawOutput)
56 10
            :
57 10
            hash(static::ALGORITHM_NAME, $data, $rawOutput);
58
59 10
        $digest = $this->changeOutputFormat($digest);
60
61 10
        return $digest;
62
    }
63
}
64