AbstractKeyMaterialDerivationFunction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 34
c 1
b 0
f 0
dl 0
loc 112
ccs 27
cts 27
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __debugInfo() 0 10 1
A hashData() 0 23 4
1
<?php
2
3
/**
4
 * Abstraction for key stretching and key derivation objects like the HKDF algorithm.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\MessageDigestion;
8
9
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractKeyStretchingFunction as KeyStretchingAlgorithm;
10
use CryptoManana\Core\Interfaces\MessageDigestion\DerivationSaltingInterface as DerivationSalting;
11
use CryptoManana\Core\Interfaces\MessageDigestion\DerivationContextInterface as DerivationContext;
12
use CryptoManana\Core\Interfaces\MessageDigestion\DerivationDigestLengthInterface as DerivationDigestLength;
13
use CryptoManana\Core\Interfaces\MessageDigestion\RepetitiveHashingInterface as RecursiveHashing;
14
use CryptoManana\Core\Traits\MessageDigestion\DerivationSaltingTrait as DerivationSaltingCapabilities;
15
use CryptoManana\Core\Traits\MessageDigestion\DerivationContextTrait as DerivationContextualCapabilities;
16
use CryptoManana\Core\Traits\MessageDigestion\DerivationDigestLengthTrait as DerivationDigestLengthCapabilities;
17
use CryptoManana\Core\Traits\MessageDigestion\RepetitiveHashingTrait as HashRepetitively;
18
19
/**
20
 * Class AbstractKeyMaterialDerivationFunction - Abstraction for output key material derivation classes.
21
 *
22
 * @package CryptoManana\Core\Abstractions\MessageDigestion
23
 *
24
 * @mixin DerivationSaltingCapabilities
25
 * @mixin DerivationContextualCapabilities
26
 * @mixin DerivationDigestLengthCapabilities
27
 * @mixin RecursiveHashing
28
 */
29
abstract class AbstractKeyMaterialDerivationFunction extends KeyStretchingAlgorithm implements
30
    DerivationSalting,
31
    DerivationContext,
32
    DerivationDigestLength,
33
    RecursiveHashing
34
{
35
    /**
36
     * Derivation data salting capabilities.
37
     *
38
     * {@internal Reusable implementation of `DerivationSaltingInterface`. }}
39
     */
40
    use DerivationSaltingCapabilities;
41
42
    /**
43
     * Derivation application/context information salting capabilities.
44
     *
45
     * {@internal Reusable implementation of `DerivationContextInterface`. }}
46
     */
47
    use DerivationContextualCapabilities;
48
49
    /**
50
     * Derivation control over the outputting digest length capabilities.
51
     *
52
     * {@internal Reusable implementation of `DerivationDigestLengthInterface`. }}
53
     */
54
    use DerivationDigestLengthCapabilities;
55
56
    /**
57
     * Repetitive/recursive hashing capabilities.
58
     *
59
     * {@internal Reusable implementation of `RepetitiveHashingInterface`. }}
60
     */
61
    use HashRepetitively;
62
63
    /**
64
     * The derivation salt string property storage.
65
     *
66
     * @var string The derivation salting string value.
67
     */
68
    protected $derivationSalt = '';
69
70
    /**
71
     * The derivation context/application information string property storage.
72
     *
73
     * @var string The derivation context/application information string value.
74
     */
75
    protected $contextualString = '';
76
77
    /**
78
     * The derivation output digest size in bytes length property storage.
79
     *
80
     * @var int The derivation output digest size in bytes length value.
81
     */
82
    protected $outputLength = 0;
83
84
    /**
85
     * Key material derivation algorithm constructor.
86
     */
87 309
    public function __construct()
88
    {
89 309
        parent::__construct();
90
    }
91
92
    /**
93
     * Get debug information for the class instance.
94
     *
95
     * @return array Debug information.
96
     */
97 15
    public function __debugInfo()
98
    {
99 15
        return [
100 15
            'standard' => static::ALGORITHM_NAME,
101 15
            'type' => 'key stretching or key material derivation',
102 15
            'salt' => $this->salt,
103 15
            'mode' => $this->saltingMode,
104 15
            'derivation salt' => $this->derivationSalt,
105 15
            'context information string' => $this->contextualString,
106 15
            'digestion output length in bytes' => $this->outputLength,
107 15
        ];
108
    }
109
110
    /**
111
     * Calculates a hash value for the given data.
112
     *
113
     * @param string $data The input string.
114
     *
115
     * @return string The digest.
116
     * @throws \Exception Validation errors.
117
     */
118 131
    public function hashData($data)
119
    {
120 131
        if (!is_string($data)) {
121 22
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
122
        }
123
124 109
        $data = $this->addSaltString(($data === '') ? ' ' : $data);
125
126 109
        $digest = hash_hkdf(
127 109
            static::ALGORITHM_NAME,
128 109
            $data,
129 109
            $this->outputLength,
130 109
            $this->contextualString,
131 109
            $this->derivationSalt
132 109
        ); // The format here by default is `self::DIGEST_OUTPUT_RAW`
133
134 109
        if ($this->digestFormat !== self::DIGEST_OUTPUT_RAW) {
135 109
            $digest = bin2hex($digest);
136
        }
137
138 109
        $digest = $this->changeOutputFormat($digest);
139
140 109
        return $digest;
141
    }
142
}
143