HmacShaThree256   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hashData() 0 21 3
1
<?php
2
3
/**
4
 * The SHA-3 family HMAC-SHA-256 hashing algorithm class.
5
 */
6
7
namespace CryptoManana\Hashing;
8
9
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractKeyedHashFunction as KeyedHashAlgorithm;
10
11
/**
12
 * Class HmacShaThree256 - The SHA-3 family HMAC-SHA-256 hashing algorithm object.
13
 *
14
 * @package CryptoManana\Hashing
15
 */
16
class HmacShaThree256 extends KeyedHashAlgorithm
17
{
18
    /**
19
     * The internal name of the algorithm.
20
     */
21
    const ALGORITHM_NAME = 'sha3-256';
22
23
    /**
24
     * Keyed digestion algorithm constructor.
25
     */
26 26
    public function __construct()
27
    {
28 26
        parent::__construct();
29
30 26
        $this->useNative = !in_array(static::ALGORITHM_NAME, hash_hmac_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 13
    public function hashData($data)
42
    {
43 13
        if (!is_string($data)) {
44 2
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
45
        }
46
47 11
        $data = $this->addSaltString($data);
48
49 11
        $rawOutput = ($this->digestFormat === self::DIGEST_OUTPUT_RAW);
50
51
        /**
52
         * {@internal Backward compatibility native realization for SHA-3 must be used. }}
53
         */
54 11
        $digest = ($this->useNative) ?
55 2
            \CryptoManana\Compatibility\NativeHmacSha3::digest256($data, $this->key, $rawOutput)
56 11
            :
57 11
            hash_hmac(static::ALGORITHM_NAME, $data, $this->key, $rawOutput);
58
59 11
        $digest = $this->changeOutputFormat($digest);
60
61 11
        return $digest;
62
    }
63
}
64