HkdfShaThree384::hashData()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 37
ccs 8
cts 8
cp 1
rs 9.2888
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
 * The SHA-3 family HKDF-SHA-384 hashing algorithm class.
5
 */
6
7
namespace CryptoManana\Hashing;
8
9
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractKeyMaterialDerivationFunction as KeyDerivationAlgorithm;
10
11
/**
12
 * Class HkdfShaThree384 - The SHA-3 family HKDF-SHA-384 hashing algorithm object.
13
 *
14
 * @package CryptoManana\Hashing
15
 */
16
class HkdfShaThree384 extends KeyDerivationAlgorithm
17
{
18
    /**
19
     * The internal name of the algorithm.
20
     */
21
    const ALGORITHM_NAME = 'sha3-384';
22
23
    /**
24
     * The internal maximum length in bytes of the output digest for the algorithm.
25
     *
26
     * @note For the current algorithm: `48 * 255 = 12240`
27
     */
28
    const ALGORITHM_MAXIMUM_OUTPUT = 12240;
29
30
    /**
31
     * The derivation output digest size in bytes length property storage.
32
     *
33
     * @var int The derivation output digest size in bytes length value.
34
     *
35
     * @note The default output size in bytes for this algorithm.
36
     */
37
    protected $outputLength = 48;
38
39
    /**
40
     * Key derivation algorithm constructor.
41
     */
42 21
    public function __construct()
43
    {
44 21
        parent::__construct();
45
46 21
        $this->useNative = !in_array(static::ALGORITHM_NAME, hash_algos(), true);
47
    }
48
49
    /**
50
     * Calculates a hash value for the given data.
51
     *
52
     * @param string $data The input string.
53
     *
54
     * @return string The digest.
55
     * @throws \Exception Validation errors.
56
     */
57 11
    public function hashData($data)
58
    {
59 11
        if (!is_string($data)) {
60 2
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
61
        }
62
63 9
        $data = $this->addSaltString(($data === '') ? ' ' : $data);
64
65
        // @codeCoverageIgnoreStart
66
        /**
67
         * {@internal Backward compatibility native realization for SHA-3 must be used. }}
68
         */
69
        $digest = ($this->useNative) ?
70
            \CryptoManana\Compatibility\NativeHkdfSha3::digest384(
71
                $data,
72
                $this->outputLength,
73
                $this->contextualString,
74
                $this->derivationSalt,
75
                true
76
            )
77
            :
78
            hash_hkdf(
79
                static::ALGORITHM_NAME,
80
                $data,
81
                $this->outputLength,
82
                $this->contextualString,
83
                $this->derivationSalt
84
            );
85
        // @codeCoverageIgnoreEnd
86
87 9
        if ($this->digestFormat !== self::DIGEST_OUTPUT_RAW) {
88 9
            $digest = bin2hex($digest);
89
        }
90
91 9
        $digest = $this->changeOutputFormat($digest);
92
93 9
        return $digest;
94
    }
95
}
96