Completed
Push — master ( e2fc6a...8f4253 )
by Tony Karavasilev (Тони
18:35
created

SignatureDataFormatsTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
eloc 56
c 1
b 0
f 0
dl 0
loc 153
ccs 63
cts 63
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A verificationFormatBase64() 0 8 4
A verificationFormat() 0 18 3
A getSignatureFormat() 0 3 1
A verificationFormatHex() 0 6 3
A setSignatureFormat() 0 23 2
A changeOutputFormat() 0 13 3
A signingFormat() 0 15 5
1
<?php
2
3
/**
4
 * Trait implementation of the signature data output formats for signature algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use \CryptoManana\Core\Interfaces\MessageEncryption\SignatureDataFormatsInterface as SignatureDataFormatsSpecification;
10
use \CryptoManana\Core\StringBuilder as StringBuilder;
11
12
/**
13
 * Trait SignatureDataFormatsTrait - Reusable implementation of `SignatureDataFormatsInterface`.
14
 *
15
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\SignatureDataFormatsInterface The abstract specification.
16
 *
17
 * @package CryptoManana\Core\Traits\MessageEncryption
18
 *
19
 * @property int $signatureFormat The output signature format property storage.
20
 *
21
 * @mixin SignatureDataFormatsSpecification
22
 */
23
trait SignatureDataFormatsTrait
24
{
25
    /**
26
     * Internal method for converting format after signing operations.
27
     *
28
     * @param string $bytes The bytes for conversion.
29
     *
30
     * @internal The parameter is passed via reference from the main logical method for performance reasons.
31
     */
32 112
    protected function signingFormat(&$bytes)
33
    {
34 112
        switch ($this->signatureFormat) {
35 112
            case self::SIGNATURE_OUTPUT_HEX_LOWER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...NATURE_OUTPUT_HEX_LOWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36 24
                $bytes = bin2hex($bytes);
37 24
                break;
38 96
            case self::SIGNATURE_OUTPUT_HEX_UPPER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...NATURE_OUTPUT_HEX_UPPER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39 96
                $bytes = StringBuilder::stringToUpper(bin2hex($bytes));
40 96
                break;
41 8
            case self::SIGNATURE_OUTPUT_BASE_64:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...IGNATURE_OUTPUT_BASE_64 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42 8
                $bytes = base64_encode($bytes);
43 8
                break;
44 8
            case self::SIGNATURE_OUTPUT_BASE_64_URL:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...TURE_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
45 8
                $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], base64_encode($bytes));
46 8
                break;
47
        }
48 112
    }
49
50
    /**
51
     * Internal method for converting from HEX formatted string after verification operations.
52
     *
53
     * @param string $bytes The bytes for conversion.
54
     *
55
     * @internal The parameter is passed via reference from the main logical method for performance reasons.
56
     */
57 72
    protected function verificationFormatHex(&$bytes)
58
    {
59 72
        if (preg_match('/^[a-f0-9]+$/', $bytes)) {
60 24
            $bytes = hex2bin($bytes);
61 56
        } elseif (preg_match('/^[A-F0-9]+$/', $bytes)) {
62 48
            $bytes = hex2bin(StringBuilder::stringToLower($bytes));
63
        }
64 72
    }
65
66
    /**
67
     * Internal method for converting from Base64 formatted string after verification operations.
68
     *
69
     * @param string $bytes The bytes for conversion.
70
     *
71
     * @internal The parameter is passed via reference from the main logical method for performance reasons.
72
     */
73 8
    protected function verificationFormatBase64(&$bytes)
74
    {
75 8
        if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) {
76 8
            $bytes = base64_decode($bytes);
77 8
        } elseif (preg_match('/^[a-zA-Z0-9_-]+$/', $bytes)) {
78 8
            $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes);
79 8
            $bytes .= str_repeat('=', StringBuilder::stringLength($bytes) % 4);
80 8
            $bytes = base64_decode($bytes);
81
        }
82 8
    }
83
84
    /**
85
     * Internal method for converting format after verification operations.
86
     *
87
     * @param string $bytes The bytes for conversion.
88
     *
89
     * @internal The parameter is passed via reference from the main logical method for performance reasons.
90
     */
91 72
    protected function verificationFormat(&$bytes)
92
    {
93 72
        $isHex = in_array(
94 72
            $this->signatureFormat,
95 72
            [self::SIGNATURE_OUTPUT_HEX_LOWER, self::SIGNATURE_OUTPUT_HEX_UPPER],
2 ignored issues
show
Bug introduced by
The constant CryptoManana\Core\Traits...NATURE_OUTPUT_HEX_LOWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant CryptoManana\Core\Traits...NATURE_OUTPUT_HEX_UPPER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
96 72
            true
97
        );
98
99 72
        $isBase64 = in_array(
100 72
            $this->signatureFormat,
101 72
            [self::SIGNATURE_OUTPUT_BASE_64, self::SIGNATURE_OUTPUT_BASE_64_URL],
2 ignored issues
show
Bug introduced by
The constant CryptoManana\Core\Traits...TURE_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant CryptoManana\Core\Traits...IGNATURE_OUTPUT_BASE_64 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102 72
            true
103
        );
104
105 72
        if ($isHex) {
106 72
            $this->verificationFormatHex($bytes);
107 8
        } elseif ($isBase64) {
108 8
            $this->verificationFormatBase64($bytes);
109
        }
110 72
    }
111
112
    /**
113
     * Internal method for converting the output format representation via the chosen format.
114
     *
115
     * @param string $bytes The bytes for conversion.
116
     * @param bool|int|null $direction Flag for signing direction (sign => `true` or verify => `false`).
117
     *
118
     * @return string The formatted bytes.
119
     */
120 120
    protected function changeOutputFormat($bytes, $direction = true)
121
    {
122 120
        if ($this->signatureFormat === self::SIGNATURE_OUTPUT_RAW) {
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...t::SIGNATURE_OUTPUT_RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
123 8
            return $bytes;
124
        }
125
126 120
        if ($direction == true) {
127 112
            $this->signingFormat($bytes);
128
        } else {
129 72
            $this->verificationFormat($bytes);
130
        }
131
132 120
        return $bytes;
133
    }
134
135
    /**
136
     * Setter for the output signature format code property.
137
     *
138
     * @param int $signatureFormat The output signature format code.
139
     *
140
     * @return $this The signature algorithm object.
141
     * @throws \Exception Validation errors.
142
     */
143 32
    public function setSignatureFormat($signatureFormat)
144
    {
145 32
        $signatureFormat = filter_var(
146 32
            $signatureFormat,
147 32
            FILTER_VALIDATE_INT,
148
            [
149
                "options" => [
150 32
                    "min_range" => self::SIGNATURE_OUTPUT_RAW,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...t::SIGNATURE_OUTPUT_RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
151 32
                    "max_range" => self::SIGNATURE_OUTPUT_BASE_64_URL,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...TURE_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
152
                ],
153
            ]
154
        );
155
156 32
        if ($signatureFormat === false) {
157 8
            throw new \InvalidArgumentException(
158
                'The output format mode must be an integer between ' .
159 8
                self::SIGNATURE_OUTPUT_RAW . ' and ' . self::SIGNATURE_OUTPUT_BASE_64_URL . '.'
160
            );
161
        }
162
163 24
        $this->signatureFormat = $signatureFormat;
164
165 24
        return $this;
166
    }
167
168
    /**
169
     * Getter for the output signature format code property.
170
     *
171
     * @return int The output signature format code.
172
     */
173 24
    public function getSignatureFormat()
174
    {
175 24
        return $this->signatureFormat;
176
    }
177
}
178