|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of the cipher/encryption data output formats for encryption algorithms. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageEncryption; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageEncryption\CipherDataFormatsInterface as CipherDataFormatsSpecification; |
|
10
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait CipherDataFormatsTrait - Reusable implementation of `CipherDataFormatsInterface`. |
|
14
|
|
|
* |
|
15
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\CipherDataFormatsInterface The abstract specification. |
|
16
|
|
|
* |
|
17
|
|
|
* @package CryptoManana\Core\Traits\MessageEncryption |
|
18
|
|
|
* |
|
19
|
|
|
* @property int $cipherFormat The output cipher format property storage. |
|
20
|
|
|
* |
|
21
|
|
|
* @mixin CipherDataFormatsSpecification |
|
22
|
|
|
*/ |
|
23
|
|
|
trait CipherDataFormatsTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Internal method for converting format after encryption 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
|
120 |
|
protected function encryptionFormat(&$bytes) |
|
33
|
|
|
{ |
|
34
|
120 |
|
switch ($this->cipherFormat) { |
|
35
|
120 |
|
case self::ENCRYPTION_OUTPUT_HEX_LOWER: |
|
36
|
12 |
|
$bytes = bin2hex($bytes); |
|
37
|
12 |
|
break; |
|
38
|
120 |
|
case self::ENCRYPTION_OUTPUT_HEX_UPPER: |
|
39
|
12 |
|
$bytes = StringBuilder::stringToUpper(bin2hex($bytes)); |
|
40
|
12 |
|
break; |
|
41
|
120 |
|
case self::ENCRYPTION_OUTPUT_BASE_64: |
|
42
|
12 |
|
$bytes = base64_encode($bytes); |
|
43
|
12 |
|
break; |
|
44
|
120 |
|
case self::ENCRYPTION_OUTPUT_BASE_64_URL: |
|
45
|
120 |
|
$bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], base64_encode($bytes)); |
|
46
|
120 |
|
break; |
|
47
|
|
|
default: // Then nothing |
|
48
|
|
|
break; |
|
49
|
|
|
} |
|
50
|
120 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Internal method for converting format after decryption operations. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $bytes The bytes for conversion. |
|
56
|
|
|
* |
|
57
|
|
|
* @internal The parameter is passed via reference from the main logical method for performance reasons. |
|
58
|
|
|
*/ |
|
59
|
96 |
|
protected function decryptionFormat(&$bytes) |
|
60
|
|
|
{ |
|
61
|
|
|
$isHex = ( |
|
62
|
96 |
|
$this->cipherFormat === self::ENCRYPTION_OUTPUT_HEX_LOWER || |
|
|
|
|
|
|
63
|
96 |
|
$this->cipherFormat === self::ENCRYPTION_OUTPUT_HEX_UPPER |
|
|
|
|
|
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
96 |
|
if ($isHex) { |
|
67
|
12 |
|
if (preg_match('/^[a-f0-9]+$/', StringBuilder::stringToLower($bytes))) { |
|
68
|
12 |
|
$bytes = hex2bin(StringBuilder::stringToLower($bytes)); |
|
69
|
|
|
} |
|
70
|
96 |
|
} elseif ($this->cipherFormat === self::ENCRYPTION_OUTPUT_BASE_64) { |
|
|
|
|
|
|
71
|
12 |
|
if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { |
|
72
|
12 |
|
$bytes = base64_decode($bytes); |
|
73
|
|
|
} |
|
74
|
96 |
|
} elseif ($this->cipherFormat === self::ENCRYPTION_OUTPUT_BASE_64_URL) { |
|
|
|
|
|
|
75
|
96 |
|
if (preg_match('/^[a-zA-Z0-9_-]+$/', $bytes)) { |
|
76
|
84 |
|
$bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); |
|
77
|
84 |
|
$bytes .= str_repeat('=', StringBuilder::stringLength($bytes) % 4); |
|
78
|
84 |
|
$bytes = base64_decode($bytes); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
96 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Internal method for converting the output format representation via the chosen format. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $bytes The bytes for conversion. |
|
87
|
|
|
* @param bool|int|null $direction Flag for encryption direction (encrypt => `true` or decrypt => `false`). |
|
88
|
|
|
* |
|
89
|
|
|
* @return string The formatted bytes. |
|
90
|
|
|
*/ |
|
91
|
156 |
|
protected function changeOutputFormat($bytes, $direction = true) |
|
92
|
|
|
{ |
|
93
|
156 |
|
if ($this->cipherFormat === self::ENCRYPTION_OUTPUT_RAW) { |
|
94
|
36 |
|
return $bytes; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
132 |
|
if ($direction == true) { |
|
98
|
120 |
|
$this->encryptionFormat($bytes); |
|
99
|
|
|
} else { |
|
100
|
96 |
|
$this->decryptionFormat($bytes); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
132 |
|
return $bytes; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Setter for the output cipher format code property. |
|
108
|
|
|
* |
|
109
|
|
|
* @param int $cipherFormat The output cipher format code. |
|
110
|
|
|
* |
|
111
|
|
|
* @return $this The encryption algorithm object. |
|
112
|
|
|
* @throws \Exception Validation errors. |
|
113
|
|
|
*/ |
|
114
|
48 |
|
public function setCipherFormat($cipherFormat) |
|
115
|
|
|
{ |
|
116
|
48 |
|
$cipherFormat = filter_var( |
|
117
|
48 |
|
$cipherFormat, |
|
118
|
48 |
|
FILTER_VALIDATE_INT, |
|
119
|
|
|
[ |
|
120
|
|
|
"options" => [ |
|
121
|
48 |
|
"min_range" => self::ENCRYPTION_OUTPUT_RAW, |
|
122
|
48 |
|
"max_range" => self::ENCRYPTION_OUTPUT_BASE_64_URL, |
|
123
|
|
|
], |
|
124
|
|
|
] |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
48 |
|
if ($cipherFormat === false) { |
|
128
|
12 |
|
throw new \InvalidArgumentException( |
|
129
|
|
|
'The output format mode must be an integer between ' . |
|
130
|
12 |
|
self::ENCRYPTION_OUTPUT_RAW . ' and ' . self::ENCRYPTION_OUTPUT_BASE_64_URL . '.' |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
36 |
|
$this->cipherFormat = $cipherFormat; |
|
135
|
|
|
|
|
136
|
36 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Getter for the output cipher format code property. |
|
141
|
|
|
* |
|
142
|
|
|
* @return int The output cipher format code. |
|
143
|
|
|
*/ |
|
144
|
48 |
|
public function getCipherFormat() |
|
145
|
|
|
{ |
|
146
|
48 |
|
return $this->cipherFormat; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|