|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\OrdersAdmin\Tools; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class designed to deal with encrypting strings sent to it, either using XOR |
|
7
|
|
|
* or AES encryption. |
|
8
|
|
|
* |
|
9
|
|
|
* To use this class you create it, then set the encryption type and then call |
|
10
|
|
|
* encrypt(). |
|
11
|
|
|
* |
|
12
|
|
|
* EG: |
|
13
|
|
|
* |
|
14
|
|
|
* Using simple XOR encryption: |
|
15
|
|
|
* |
|
16
|
|
|
* $encrypt = StringEncryptor::create('encrypt this') |
|
17
|
|
|
* ->setHash('hashcode') |
|
18
|
|
|
* ->encrypt() |
|
19
|
|
|
* ->get(); |
|
20
|
|
|
* |
|
21
|
|
|
* |
|
22
|
|
|
* XOR encryption then base 64 encoded: |
|
23
|
|
|
* |
|
24
|
|
|
* $encrypt = StringEncryptor::create('encrypt this') |
|
25
|
|
|
* ->setHash('hashcode') |
|
26
|
|
|
* ->encrypt() |
|
27
|
|
|
* ->encode() |
|
28
|
|
|
* ->get(); |
|
29
|
|
|
* |
|
30
|
|
|
* MCrypt AES encryption: |
|
31
|
|
|
* |
|
32
|
|
|
* $encrypt = StringEncryptor::create('encrypt this') |
|
33
|
|
|
* ->setHash('hashcode') |
|
34
|
|
|
* ->setEncryption('MCRYPT') |
|
35
|
|
|
* ->encrypt() |
|
36
|
|
|
* ->get(); |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
class StringEncryptor |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* String that we are going to encrypt |
|
45
|
|
|
* @var String |
|
46
|
|
|
*/ |
|
47
|
|
|
private $data; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* String that is being encrypted |
|
51
|
|
|
* @var String |
|
52
|
|
|
*/ |
|
53
|
|
|
private $encrypted_data; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Choose encryption type |
|
57
|
|
|
* @var String |
|
58
|
|
|
* @default XOR |
|
59
|
|
|
*/ |
|
60
|
|
|
private $encryption = 'XOR'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Hash used to encrypt our string |
|
64
|
|
|
* @var String |
|
65
|
|
|
*/ |
|
66
|
|
|
private $hash; |
|
67
|
|
|
|
|
68
|
|
|
private function __construct($string) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->data = $string; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Factory method allowing chaining |
|
75
|
|
|
* |
|
76
|
|
|
* @para, $string string to encrypt |
|
77
|
|
|
* @return StringEncryptor |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function create($string) |
|
80
|
|
|
{ |
|
81
|
|
|
return new StringEncryptor($string); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Set our encryption type |
|
86
|
|
|
* |
|
87
|
|
|
* @param $type Type of encryption |
|
|
|
|
|
|
88
|
|
|
* @return self |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setEncryption($type) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->encryption = $type; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set our hash |
|
98
|
|
|
* |
|
99
|
|
|
* @param $hash |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setHash($hash) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->hash = $hash; |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get our encrypted data |
|
110
|
|
|
* |
|
111
|
|
|
* @return String |
|
112
|
|
|
*/ |
|
113
|
|
|
public function get() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->encrypted_data; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Perform our data encryption |
|
120
|
|
|
* |
|
121
|
|
|
* @return self |
|
122
|
|
|
*/ |
|
123
|
|
|
public function encrypt() |
|
124
|
|
|
{ |
|
125
|
|
|
if ($this->encryption == 'XOR') { |
|
126
|
|
|
$this->encrypted_data = $this->simplexor(); |
|
127
|
|
|
} elseif ($this->encryption == 'MCRYPT') { |
|
128
|
|
|
$this->encrypted_data = $this->mcrypt(); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Base 64 encode the data, ready for transit |
|
136
|
|
|
* |
|
137
|
|
|
* @return self |
|
138
|
|
|
*/ |
|
139
|
|
|
public function encode() |
|
140
|
|
|
{ |
|
141
|
|
|
// Encode data string |
|
142
|
|
|
$this->encrypted_data = base64_encode($this->encrypted_data); |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* SimpleXor encryption algorithm |
|
150
|
|
|
* |
|
151
|
|
|
* return self |
|
152
|
|
|
*/ |
|
153
|
|
|
private function simplexor() |
|
154
|
|
|
{ |
|
155
|
|
|
$KeyList = array(); |
|
156
|
|
|
$output = ""; |
|
157
|
|
|
|
|
158
|
|
|
// Convert $Key into array of ASCII values |
|
159
|
|
|
for ($i = 0; $i < strlen($this->hash); $i++) { |
|
160
|
|
|
$KeyList[$i] = ord(substr($this->hash, $i, 1)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// Step through string a character at a time |
|
164
|
|
|
for ($i = 0; $i < strlen($this->data); $i++) { |
|
165
|
|
|
$output.= chr(ord(substr($this->data, $i, 1)) ^ ($KeyList[$i % strlen($this->hash)])); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Return the result |
|
169
|
|
|
return $output; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Encrypt our data using PHP mcrypt and AES with PKCS5 padding |
|
174
|
|
|
* |
|
175
|
|
|
* @return self |
|
176
|
|
|
*/ |
|
177
|
|
|
private function mcrypt() |
|
178
|
|
|
{ |
|
179
|
|
|
$strIV = $this->hash; |
|
180
|
|
|
|
|
181
|
|
|
// add PKCS5 padding to the text to be encypted |
|
182
|
|
|
$strIn = $this->addPKCS5Padding(); |
|
183
|
|
|
|
|
184
|
|
|
// perform encryption with PHP's MCRYPT module |
|
185
|
|
|
$output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->hash, $strIn, MCRYPT_MODE_CBC, $strIV); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
// perform hex encoding and return with @ symbol |
|
188
|
|
|
return bin2hex($output); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* PHP's mcrypt does not have built in PKCS5 Padding, so we use this |
|
193
|
|
|
*/ |
|
194
|
|
|
private function addPKCS5Padding() |
|
195
|
|
|
{ |
|
196
|
|
|
$blocksize = 16; |
|
197
|
|
|
$padding = ""; |
|
198
|
|
|
|
|
199
|
|
|
// Pad input to an even block size boundary |
|
200
|
|
|
$padlength = $blocksize - (strlen($this->data) % $blocksize); |
|
201
|
|
|
for ($i = 1; $i <= $padlength; $i++) { |
|
202
|
|
|
$padding .= chr($padlength); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $this->data . $padding; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths