Completed
Push — master ( bfe7fb...e70e28 )
by Tony Karavasilev (Тони
06:02
created

DigestionFormatsTrait::getDigestFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Trait implementation of the hashing output formats for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface as DigestionFormatsSpecification;
10
11
/**
12
 * Trait DigestionFormatsTrait - Reusable implementation of `DigestionFormatsInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageDigestion
17
 *
18
 * @property int $digestFormat The digest output format property storage.
19
 *
20
 * @mixin DigestionFormatsSpecification
21
 */
22
trait DigestionFormatsTrait
23
{
24
    /**
25
     * Internal method for converting the digest's output format representation via the chosen format.
26
     *
27
     * @param string $digest The output digest.
28
     *
29
     * @return string The input data with proper salting.
30
     */
31
    protected function changeOutputFormat($digest)
32
    {
33
        switch ($this->getDigestFormat()) {
34
            case self::DIGEST_OUTPUT_HEX_LOWER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DIGEST_OUTPUT_HEX_LOWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
                break; // Comes already in lower, can be skipped
36
            case self::DIGEST_OUTPUT_HEX_UPPER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DIGEST_OUTPUT_HEX_UPPER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
37
                $digest = strtoupper($digest);
38
                break;
39
            case self::DIGEST_OUTPUT_BASE_64:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...::DIGEST_OUTPUT_BASE_64 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
                $digest = base64_encode(pack('H*', $digest));
41
                break;
42
            case self::DIGEST_OUTPUT_BASE_64_URL:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...GEST_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43
                $digest = base64_encode(pack('H*', $digest));
44
                $digest = str_replace(['+', '/', '='], ['-', '_', ''], $digest);
45
                break;
46
            default: // case self::DIGEST_OUTPUT_RAW:
47
                break;
48
        }
49
50
        return $digest;
51
    }
52
53
    /**
54
     * Setter for the digest format code property.
55
     *
56
     * @param int $digestFormat The digest format code.
57
     *
58
     * @return $this The hash algorithm object.
59
     * @throw \Exception Validation errors.
60
     */
61
    public function setDigestFormat($digestFormat)
62
    {
63
        $digestFormat = filter_var(
64
            $digestFormat,
65
            FILTER_VALIDATE_INT,
66
            [
67
                "options" => [
68
                    "min_range" => self::DIGEST_OUTPUT_RAW,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...rait::DIGEST_OUTPUT_RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
                    "max_range" => self::DIGEST_OUTPUT_BASE_64_URL,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...GEST_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
                ],
71
            ]
72
        );
73
74
        if ($digestFormat === false) {
75
            throw new \InvalidArgumentException('Digest output format mode must be an integer between -1 and 3.');
76
        }
77
78
        $this->digestFormat = $digestFormat;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Getter for the digest format code property.
85
     *
86
     * @return int The digest format code.
87
     */
88
    public function getDigestFormat()
89
    {
90
        return $this->digestFormat;
91
    }
92
}
93