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

AbstractHashAlgorithm::addSaltString()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 35
ccs 31
cts 31
cp 1
rs 7.6666
cc 10
nc 10
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The hash algorithm abstraction specification.
5
 */
6
7
namespace CryptoManana\Core\Abstractions\MessageDigestion;
8
9
use \CryptoManana\Core\Interfaces\MessageDigestion\SaltingCapabilitiesInterface as DataSalting;
10
use \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface as DigestFormatting;
11
use \CryptoManana\Core\Traits\MessageDigestion\SaltingCapabilitiesTrait as SaltingCapabilities;
12
use \CryptoManana\Core\Traits\MessageDigestion\DigestionFormatsTrait as DigestOutputFormats;
13
14
/**
15
 * Class AbstractHashAlgorithm - The hash algorithm abstraction representation.
16
 *
17
 * @package CryptoManana\Core\Abstractions\MessageDigestion
18
 *
19
 * @mixin SaltingCapabilities
20
 * @mixin DigestOutputFormats
21
 */
22
abstract class AbstractHashAlgorithm implements DataSalting, DigestFormatting
23
{
24
    /**
25
     * Data salting capabilities.
26
     *
27
     * {@internal Reusable implementation of `SaltingCapabilitiesInterface`. }}
28
     */
29
    use SaltingCapabilities;
30
31
    /**
32
     * Digest outputting formats.
33
     *
34
     * {@internal Reusable implementation of `DigestionFormatsInterface`. }}
35
     */
36
    use DigestOutputFormats;
37
38
    /**
39
     * The salt string property storage.
40
     *
41
     * @var string The salting string value.
42
     */
43
    protected $salt = '';
44
45
    /**
46
     * The salting mode property storage.
47
     *
48
     * @var int The salting mode integer code value.
49
     */
50
    protected $saltingMode = self::SALTING_MODE_APPEND;
51
52
    /**
53
     * The digest output format property storage.
54
     *
55
     * @var int The output format integer code value.
56
     */
57
    protected $digestFormat = self::DIGEST_OUTPUT_HEX_UPPER;
58
59
    /**
60
     * Internal method for adding the salt string to the input data via the chosen salting mode.
61
     *
62
     * @param string $data The input data for hashing.
63
     *
64
     * @return string The input data with proper salting.
65
     */
66 176
    protected function addSaltString($data)
67
    {
68 176
        switch ($this->saltingMode) {
69 176
            case self::SALTING_MODE_APPEND: // passwordSALT
70 154
                $data .= $this->salt;
71 154
                break;
72 66
            case self::SALTING_MODE_PREPEND: // SALTpassword
73 44
                $data = $this->salt . $data;
74 44
                break;
75 44
            case self::SALTING_MODE_INFIX_INPUT: // SALTpasswordTLAS
76 22
                $data = $this->salt . $data . strrev($this->salt);
77 22
                break;
78 44
            case self::SALTING_MODE_INFIX_SALT: // passwordSALTdrowssap
79 22
                $data = $data . $this->salt . strrev($data);
80 22
                break;
81 44
            case self::SALTING_MODE_REVERSE_APPEND: // passwordTLAS
82 22
                $data .= strrev($this->salt);
83 22
                break;
84 44
            case self::SALTING_MODE_REVERSE_PREPEND: // TLASpassword
85 22
                $data = strrev($this->salt) . $data;
86 22
                break;
87 44
            case self::SALTING_MODE_DUPLICATE_SUFFIX: // passwordSALTTLAS
88 22
                $data = $data . $this->salt . strrev($this->salt);
89 22
                break;
90 44
            case self::SALTING_MODE_DUPLICATE_PREFIX: // SALTTLASpassword
91 22
                $data = $this->salt . strrev($this->salt) . $data;
92 22
                break;
93 44
            case self::SALTING_MODE_PALINDROME_MIRRORING: // SALTpassworddrowssapTLAS
94 22
                $data = $this->salt . $data . strrev($data) . strrev($this->salt);
95 22
                break;
96
            default: // case self::SALTING_MODE_NONE:
97 44
                break;
98
        }
99
100 176
        return $data;
101
    }
102
103
    /**
104
     * Hash algorithm constructor.
105
     */
106
    abstract public function __construct();
107
108
    /**
109
     * Calculates a hash value for the given data.
110
     *
111
     * @param string $data The input string.
112
     *
113
     * @return string The digest.
114
     * @throws \Exception Validation errors.
115
     */
116
    abstract public function hashData($data);
117
}
118