1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Part of the IPFuscator package. |
5
|
|
|
* Author: Kashyap Merai <[email protected]> |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace kamerk22\IPFuscator; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use kamerk22\IPFuscator\Exception\InvalidArgument; |
14
|
|
|
use kamerk22\IPFuscator\Helper\Helper; |
15
|
|
|
|
16
|
|
|
class IPFuscator |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* getDecimal |
21
|
|
|
* |
22
|
|
|
* @param string $ip |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
* @throws InvalidArgument |
26
|
|
|
*/ |
27
|
5 |
|
public static function getDecimal($ip): string |
28
|
|
|
{ |
29
|
5 |
|
$parts = Helper::getParts($ip); |
30
|
4 |
|
return (int)$parts[0] * (2 ** 24) + (int)$parts[1] * (2 ** 16) + (int)$parts[2] * (2 ** 8) + (int)$parts[3]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* getHexadecimal |
35
|
|
|
* |
36
|
|
|
* @param string $ip |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
* @throws InvalidArgument |
40
|
|
|
*/ |
41
|
1 |
|
public static function getHexadecimal(string $ip): string |
42
|
|
|
{ |
43
|
1 |
|
return '0x' . dechex((int)self::getDecimal($ip)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* getOctal |
48
|
|
|
* |
49
|
|
|
* @param string $ip |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
* @throws InvalidArgument |
53
|
|
|
*/ |
54
|
1 |
|
public static function getOctal(string $ip): string |
55
|
|
|
{ |
56
|
1 |
|
return '0' . decoct((int)self::getDecimal($ip)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* getFullHex |
61
|
|
|
* |
62
|
|
|
* @param string $ip |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* @throws InvalidArgument |
66
|
|
|
*/ |
67
|
1 |
|
public static function getFullHex(string $ip): string |
68
|
|
|
{ |
69
|
1 |
|
return implode('.', Helper::getHexParts($ip)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* getFullOct |
74
|
|
|
* |
75
|
|
|
* @param string $ip |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
* @throws InvalidArgument |
79
|
|
|
*/ |
80
|
1 |
|
public static function getFullOct(string $ip): string |
81
|
|
|
{ |
82
|
|
|
|
83
|
1 |
|
return implode('.', Helper::getOctalParts($ip)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* getRandomHexPad |
88
|
|
|
* |
89
|
|
|
* @param string $ip |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* @throws \Exception |
93
|
|
|
* @throws InvalidArgument |
94
|
|
|
*/ |
95
|
1 |
|
public static function getRandomHexPad(string $ip): string |
96
|
|
|
{ |
97
|
1 |
|
$randHex = ''; |
98
|
1 |
|
foreach (Helper::getHexParts($ip) as $parts) { |
99
|
1 |
|
$randHex .= str_replace('0x', str_pad('0x0', random_int(1, 30), '0'), $parts) . '.'; |
100
|
|
|
} |
101
|
1 |
|
return rtrim($randHex, '.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* getRandomOctPad |
106
|
|
|
* |
107
|
|
|
* @param string $ip |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
* @throws \Exception |
111
|
|
|
* @throws InvalidArgument |
112
|
|
|
*/ |
113
|
1 |
|
public static function getRandomOctPad(string $ip): string |
114
|
|
|
{ |
115
|
1 |
|
$randOctal = ''; |
116
|
1 |
|
foreach (Helper::getOctalParts($ip) as $parts) { |
117
|
1 |
|
$randOctal .= str_pad('0', random_int(1, 30), '0') . $parts . '.'; |
118
|
|
|
} |
119
|
1 |
|
return rtrim($randOctal, '.'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* getRandomBase |
124
|
|
|
* |
125
|
|
|
* @param string $ip |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
* @throws \Exception |
129
|
|
|
* @throws InvalidArgument |
130
|
|
|
*/ |
131
|
1 |
|
public static function getRandomBase(string $ip): string |
132
|
|
|
{ |
133
|
1 |
|
$parts = Helper::getParts($ip); |
134
|
1 |
|
$Octparts = Helper::getOctalParts($ip); |
135
|
1 |
|
$Hexparts = Helper::getHexParts($ip); |
136
|
1 |
|
$randomBase = ''; |
137
|
1 |
|
for ($i = 0; $i < 4; $i++) { |
138
|
1 |
|
if ($i === 0) { |
139
|
1 |
|
$randomBase .= $parts[random_int(0,3)]. '.'; |
140
|
1 |
|
} elseif ($i === 1) { |
141
|
1 |
|
$randomBase .= $Hexparts[random_int(0,3)]. '.'; |
142
|
|
|
} else { |
143
|
1 |
|
$randomBase .= $Octparts[random_int(0,3)]. '.'; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
1 |
|
return rtrim($randomBase, '.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* getRandomBaseWithPad |
153
|
|
|
* |
154
|
|
|
* @param string $ip |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
* @throws \Exception |
158
|
|
|
* @throws InvalidArgument |
159
|
|
|
*/ |
160
|
1 |
|
public static function getRandomBaseWithRandomPad(string $ip): string |
161
|
|
|
{ |
162
|
1 |
|
$parts = Helper::getParts($ip); |
163
|
1 |
|
$Octparts = Helper::getOctalParts($ip); |
164
|
1 |
|
$Hexparts = Helper::getHexParts($ip); |
165
|
1 |
|
$randomBasePad = ''; |
166
|
1 |
|
for ($i = 0; $i < 4; $i++) { |
167
|
1 |
|
if ($i === 0) { |
168
|
1 |
|
$randomBasePad .= $parts[random_int(0,3)]. '.'; |
169
|
1 |
|
} elseif ($i === 1) { |
170
|
1 |
|
$randomBasePad .= str_replace('0x', str_pad('0x0', random_int(1, 30), '0'), $Hexparts[random_int(0,3)]) . '.'; |
171
|
|
|
} else { |
172
|
1 |
|
$randomBasePad .= str_pad('0', random_int(1, 30), '0') . $Octparts[random_int(0,3)]. '.'; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
1 |
|
return rtrim($randomBasePad, '.'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
} |