Completed
Push — master ( 10b3db...7ff62b )
by Tony Karavasilev (Тони
06:39
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 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
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
use \CryptoManana\Core\StringBuilder as StringBuilder;
11
12
/**
13
 * Trait SaltingCapabilitiesTrait - Reusable implementation of `SaltingCapabilitiesInterface`.
14
 *
15
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface The abstract specification.
16
 *
17
 * @package CryptoManana\Core\Traits\MessageDigestion
18
 *
19
 * @property string $salt The salt string property storage.
20
 * @property int $saltingMode The salting mode property storage.
21
 *
22
 * @mixin SaltingCapabilitiesSpecification
23
 */
24
trait SaltingCapabilitiesTrait
25
{
26
    /**
27
     * Internal method for adding the salt string to the input data via the chosen salting mode.
28
     *
29
     * @param string $data The input data for hashing.
30
     *
31
     * @return string The input data with proper salting.
32
     */
33 176
    protected function addSaltString($data)
34
    {
35 176
        if ($this->saltingMode === self::SALTING_MODE_NONE) {
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...rait::SALTING_MODE_NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36 44
            return $data;
37
        }
38
39 176
        switch ($this->saltingMode) {
40 176
            case self::SALTING_MODE_APPEND: // passwordSALT
41 154
                return $data . $this->salt;
42 44
            case self::SALTING_MODE_PREPEND: // SALTpassword
43 44
                return $this->salt . $data;
44 22
            case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS
45 22
                return $this->salt . $data . StringBuilder::stringReverse($this->salt);
46 22
            case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap
47 22
                return $data . $this->salt . StringBuilder::stringReverse($data);
48
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
49 22
            case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS
50 22
                return $data . StringBuilder::stringReverse($this->salt);
51 22
            case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword
52 22
                return StringBuilder::stringReverse($this->salt) . $data;
53 22
            case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS
54 22
                return $data . $this->salt . StringBuilder::stringReverse($this->salt);
55 22
            case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword
56 22
                return $this->salt . StringBuilder::stringReverse($this->salt) . $data;
57 22
            case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS
58
                return (
59 22
                    $this->salt . $data .
60 22
                    StringBuilder::stringReverse($data) . StringBuilder::stringReverse($this->salt)
61
                );
62
            default: // case self::SALTING_MODE_NONE:
63
                return $data;
64
        }
65
    }
66
67
    /**
68
     * Setter for the salt string property.
69
     *
70
     * @param string $salt The salt string.
71
     *
72
     * @return $this The hash algorithm object.
73
     * @throw \Exception Validation errors.
74
     */
75 88
    public function setSalt($salt)
76
    {
77 88
        if (!is_string($salt)) {
78 22
            throw new \InvalidArgumentException('Salt must be of type string.');
79
        }
80
81 66
        $this->salt = $salt;
82
83 66
        return $this;
84
    }
85
86
    /**
87
     * Getter for the salt string property.
88
     *
89
     * @return string The salt string.
90
     */
91 44
    public function getSalt()
92
    {
93 44
        return $this->salt;
94
    }
95
96
    /**
97
     * Setter for the salting mode code property.
98
     *
99
     * @param int $saltingMode The salting mode code.
100
     *
101
     * @return $this The hash algorithm object.
102
     * @throw \Exception Validation errors.
103
     */
104 88
    public function setSaltingMode($saltingMode)
105
    {
106 88
        $saltingMode = filter_var(
107 88
            $saltingMode,
108 88
            FILTER_VALIDATE_INT,
109
            [
110
                "options" => [
111 88
                    "min_range" => self::SALTING_MODE_NONE, // -1
112 88
                    "max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8
113
                ],
114
            ]
115
        );
116
117 88
        if ($saltingMode === false) {
118 22
            throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.');
119
        }
120
121 66
        $this->saltingMode = $saltingMode;
122
123 66
        return $this;
124
    }
125
126
    /**
127
     * Getter for the salt mode code property.
128
     *
129
     * @return int The salt mode code.
130
     */
131 44
    public function getSaltingMode()
132
    {
133 44
        return $this->saltingMode;
134
    }
135
}
136