Completed
Push — master ( 526008...58f651 )
by Tony Karavasilev (Тони
06:48
created

SaltingCapabilitiesTrait::setSalt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * Trait implementation of the salting capabilities for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface as SaltingCapabilitiesSpecification;
10
11
/**
12
 * Trait SaltingCapabilitiesTrait - Reusable implementation of `SaltingCapabilitiesInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageDigestion
17
 *
18
 * @property string $salt The salt string property storage.
19
 * @property int $saltingMode The salting mode property storage.
20
 *
21
 * @mixin SaltingCapabilitiesSpecification
22
 */
23
trait SaltingCapabilitiesTrait
24
{
25
    /**
26
     * Internal method for adding the salt string to the input data via the chosen salting mode.
27
     *
28
     * @param string $data The input data for hashing.
29
     *
30
     * @return string The input data with proper salting.
31
     */
32
    abstract protected function addSaltString($data);
33
34
    /**
35
     * Setter for the salt string property.
36
     *
37
     * @param string $salt The salt string.
38
     *
39
     * @return $this The hash algorithm object.
40
     * @throw \Exception Validation errors.
41
     */
42 88
    public function setSalt($salt)
43
    {
44 88
        if (!is_string($salt)) {
45 22
            throw new \InvalidArgumentException('Salt must be of type string.');
46
        }
47
48 66
        $this->salt = $salt;
49
50 66
        return $this;
51
    }
52
53
    /**
54
     * Getter for the salt string property.
55
     *
56
     * @return string The salt string.
57
     */
58 44
    public function getSalt()
59
    {
60 44
        return $this->salt;
61
    }
62
63
    /**
64
     * Setter for the salting mode code property.
65
     *
66
     * @param int $saltingMode The salting mode code.
67
     *
68
     * @return $this The hash algorithm object.
69
     * @throw \Exception Validation errors.
70
     */
71 88
    public function setSaltingMode($saltingMode)
72
    {
73 88
        $saltingMode = filter_var(
74 88
            $saltingMode,
75 88
            FILTER_VALIDATE_INT,
76
            [
77
                "options" => [
78 88
                    "min_range" => self::SALTING_MODE_NONE, // -1
79 88
                    "max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8
80
                ],
81
            ]
82
        );
83
84 88
        if ($saltingMode === false) {
85 22
            throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.');
86
        }
87
88 66
        $this->saltingMode = $saltingMode;
89
90 66
        return $this;
91
    }
92
93
    /**
94
     * Getter for the salt mode code property.
95
     *
96
     * @return int The salt mode code.
97
     */
98 44
    public function getSaltingMode()
99
    {
100 44
        return $this->saltingMode;
101
    }
102
}
103