Completed
Push — master ( e4d297...14927f )
by Tony Karavasilev (Тони
16:15
created

setDerivationSalt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/**
4
 * Abstraction for the slow iterative derivation algorithm objects.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\MessageDigestion;
8
9
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractPasswordBasedDerivationFunction as PasswordDerivation;
10
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationSaltingInterface as DerivationSalting;
11
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationDigestLengthInterface as DerivationDigestLength;
12
use \CryptoManana\Core\Interfaces\MessageDigestion\DerivationIterationControlInterface as DerivationIterationControl;
13
use \CryptoManana\Core\Interfaces\MessageDigestion\SecureVerificationInterface as DataVerification;
14
use \CryptoManana\Core\Traits\MessageDigestion\DerivationSaltingTrait as DerivationSaltingCapabilities;
15
use \CryptoManana\Core\Traits\MessageDigestion\DerivationDigestLengthTrait as DerivationDigestLengthCapabilities;
16
use \CryptoManana\Core\Traits\MessageDigestion\DerivationIterationControlTrait as IterationControlCapabilities;
17
use \CryptoManana\Core\Traits\MessageDigestion\SecureVerificationTrait as VerifyDataAndPasswords;
18
use \CryptoManana\Core\StringBuilder as StringBuilder;
19
20
/**
21
 * Class AbstractIterativeSlowDerivation - The iterative derivation algorithm abstraction representation.
22
 *
23
 * @package CryptoManana\Core\Abstractions\MessageDigestion
24
 *
25
 * @mixin DerivationSaltingCapabilities
26
 * @mixin DerivationDigestLengthCapabilities
27
 * @mixin IterationControlCapabilities
28
 * @mixin VerifyDataAndPasswords
29
 */
30
abstract class AbstractIterativeSlowDerivation extends PasswordDerivation implements
31
    DerivationSalting,
32
    DerivationDigestLength,
33
    DerivationIterationControl,
34
    DataVerification
35
{
36
    /**
37
     * Derivation data salting capabilities.
38
     *
39
     * {@internal Reusable implementation of `DerivationSaltingInterface`. }}
40
     */
41
    use DerivationSaltingCapabilities;
42
43
    /**
44
     * Derivation control over the outputting digest length capabilities.
45
     *
46
     * {@internal Reusable implementation of `DerivationDigestLengthInterface`. }}
47
     */
48
    use DerivationDigestLengthCapabilities;
49
50
    /**
51
     * Derivation internal iterations control capabilities.
52
     *
53
     * {@internal Reusable implementation of `DerivationIterationControlInterface`. }}
54
     */
55
    use IterationControlCapabilities;
56
57
    /**
58
     * Secure password and data verification capabilities.
59
     *
60
     * {@internal Reusable implementation of `SecureVerificationInterface`. }}
61
     */
62
    use VerifyDataAndPasswords;
63
64
    /**
65
     * The derivation salt string property storage.
66
     *
67
     * @var string The derivation salting string value.
68
     */
69
    protected $derivationSalt = '';
70
71
    /**
72
     * The derivation output digest size in bytes length property storage.
73
     *
74
     * @var int The derivation output digest size in bytes length value.
75
     *
76
     * @internal The default output size in bytes for this algorithm.
77
     */
78
    protected $outputLength = 0;
79
80
    /**
81
     * The derivation internal iteration count property storage.
82
     *
83
     * @var int The number of internal iterations to perform for the derivation.
84
     */
85
    protected $numberOfIterations = 1;
86
87
    /**
88
     * Password-based key derivation algorithm constructor.
89
     */
90 418
    public function __construct()
91
    {
92 418
        parent::__construct();
93 418
    }
94
95
    /**
96
     * Calculates a hash value for the given data.
97
     *
98
     * @param string $data The input string.
99
     *
100
     * @return string The digest.
101
     * @throws \Exception Validation errors.
102
     */
103 140
    public function hashData($data)
104
    {
105 140
        if (!is_string($data)) {
1 ignored issue
show
introduced by
The condition is_string($data) is always true.
Loading history...
106 14
            throw new \InvalidArgumentException('The data for hashing must be a string or a binary string.');
107
        }
108
109 126
        $data = $this->addSaltString($data);
110
111 126
        $digest = hash_pbkdf2(
112 126
            static::ALGORITHM_NAME,
113
            $data,
114 126
            $this->derivationSalt,
115 126
            $this->numberOfIterations,
116 126
            $this->outputLength,
117 126
            true // The format here by default is `self::DIGEST_OUTPUT_RAW`
118
        );
119
120 126
        if ($this->digestFormat !== self::DIGEST_OUTPUT_RAW) {
121 126
            $digest = bin2hex($digest);
122
        }
123
124 126
        $digest = $this->changeOutputFormat($digest);
125
126 126
        return $digest;
127
    }
128
129
    /**
130
     * Setter for the derivation salt string property.
131
     *
132
     * @param string $derivationSalt The derivation salt string.
133
     *
134
     * @return $this The hash algorithm object.
135
     * @throws \Exception Validation errors.
136
     */
137 44
    public function setDerivationSalt($derivationSalt)
138
    {
139
        /**
140
         * {@internal An extra specification for the PBKDF2 digestion generation logic. }}
141
         */
142 44
        if (!is_string($derivationSalt) || StringBuilder::stringLength($derivationSalt) > PHP_INT_MAX - 4) {
1 ignored issue
show
introduced by
The condition is_string($derivationSalt) is always true.
Loading history...
143 22
            throw new \InvalidArgumentException(
144 22
                'The derivation salt must be of type string and be smaller than `PHP_INT_MAX - 4`.'
145
            );
146
        }
147
148 22
        $this->derivationSalt = $derivationSalt;
149
150 22
        return $this;
151
    }
152
153
    /**
154
     * Get debug information for the class instance.
155
     *
156
     * @return array Debug information.
157
     */
158 22
    public function __debugInfo()
159
    {
160
        return [
161 22
            'standard' => static::ALGORITHM_NAME,
162 22
            'type' => 'key stretching or password-based key derivation',
163 22
            'salt' => $this->salt,
164 22
            'mode' => $this->saltingMode,
165 22
            'derivation salt' => $this->derivationSalt,
166 22
            'digestion output length in bytes' => $this->outputLength,
167 22
            'number of internal iterations' => $this->numberOfIterations,
168
        ];
169
    }
170
}
171