Completed
Push — master ( e70e28...7f2cdc )
by Tony Karavasilev (Тони
06:07
created

SaltingCapabilitiesTrait::getSalt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 176
    protected function addSaltString($data)
33
    {
34 176
        switch ($this->getSaltingMode()) {
35 176
            case self::SALTING_MODE_APPEND: // passwordSALT
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...it::SALTING_MODE_APPEND was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36 154
                $data .= $this->salt;
37 154
                break;
38 44
            case self::SALTING_MODE_PREPEND: // SALTpassword
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...t::SALTING_MODE_PREPEND was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39 44
                $data = $this->salt . $data;
40 44
                break;
41 22
            case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...ALTING_MODE_INFIX_INPUT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42 22
                $data = $this->salt . $data . strrev($this->salt);
43 22
                break;
44 22
            case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...SALTING_MODE_INFIX_SALT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45 22
                $data = $data . $this->salt . strrev($data);
46 22
                break;
47 22
            case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...ING_MODE_REVERSE_APPEND was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48 22
                $data .= strrev($this->salt);
49 22
                break;
50 22
            case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...NG_MODE_REVERSE_PREPEND was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
51 22
                $data = strrev($this->salt) . $data;
52 22
                break;
53 22
            case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...G_MODE_DUPLICATE_SUFFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
54 22
                $data = $data . $this->salt . strrev($this->salt);
55 22
                break;
56 22
            case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...G_MODE_DUPLICATE_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57 22
                $data = $this->salt . strrev($this->salt) . $data;
58 22
                break;
59 22
            case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DE_PALINDROME_MIRRORING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60 22
                $data = $this->salt . $data . strrev($data) . strrev($this->salt);
61 22
                break;
62
            default: // case self::SALTING_MODE_NONE:
63 22
                break;
64
        }
65
66 176
        return $data;
67
    }
68
69
    /**
70
     * Setter for the salt string property.
71
     *
72
     * @param string $salt The salt string.
73
     *
74
     * @return $this The hash algorithm object.
75
     * @throw \Exception Validation errors.
76
     */
77 66
    public function setSalt($salt)
78
    {
79 66
        if (!is_string($salt)) {
1 ignored issue
show
introduced by
The condition is_string($salt) is always true.
Loading history...
80 22
            throw new \InvalidArgumentException('Salt must be of type string.');
81
        }
82
83 44
        $this->salt = $salt;
84
85 44
        return $this;
86
    }
87
88
    /**
89
     * Getter for the salt string property.
90
     *
91
     * @return string The salt string.
92
     */
93 22
    public function getSalt()
94
    {
95 22
        return $this->salt;
96
    }
97
98
    /**
99
     * Setter for the salting mode code property.
100
     *
101
     * @param int $saltingMode The salting mode code.
102
     *
103
     * @return $this The hash algorithm object.
104
     * @throw \Exception Validation errors.
105
     */
106 66
    public function setSaltingMode($saltingMode)
107
    {
108 66
        $saltingMode = filter_var(
109 66
            $saltingMode,
110 66
            FILTER_VALIDATE_INT,
111
            [
112
                "options" => [
113 66
                    "min_range" => self::SALTING_MODE_NONE, // -1
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...
114 66
                    "max_range" => self::SALTING_MODE_PALINDROME_MIRRORING, // 8
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DE_PALINDROME_MIRRORING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
115
                ],
116
            ]
117
        );
118
119 66
        if ($saltingMode === false) {
120 22
            throw new \InvalidArgumentException('Salting mode must be an integer between -1 and 8.');
121
        }
122
123 44
        $this->saltingMode = $saltingMode;
124
125 44
        return $this;
126
    }
127
128
    /**
129
     * Getter for the salt mode code property.
130
     *
131
     * @return int The salt mode code.
132
     */
133 176
    public function getSaltingMode()
134
    {
135 176
        return $this->saltingMode;
136
    }
137
}
138