Complex classes like Shamir often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Shamir, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Shamir implements Algorithm, RandomGeneratorAware |
||
19 | { |
||
20 | /** |
||
21 | * Calculation base (decimal) |
||
22 | * |
||
23 | * Changing this will invalid all previously created keys. |
||
24 | * |
||
25 | * @const string |
||
26 | */ |
||
27 | protected const DECIMAL = '0123456789'; |
||
28 | |||
29 | /** |
||
30 | * Target base characters to be used in passwords (shares) |
||
31 | * |
||
32 | * The more characters are used, the shorter the shares might get. |
||
33 | * Changing this will invalid all previously created keys. |
||
34 | * |
||
35 | * @const string |
||
36 | */ |
||
37 | protected const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz.,:;-+*#%'; |
||
38 | |||
39 | /** |
||
40 | * Character to fill up the secret keys |
||
41 | * |
||
42 | * @const string |
||
43 | */ |
||
44 | protected const PAD_CHAR = '='; |
||
45 | |||
46 | /** |
||
47 | * Prime number has to be greater than the maximum number of shares possible |
||
48 | * |
||
49 | * @var float |
||
50 | */ |
||
51 | protected $prime = 257; |
||
52 | |||
53 | /** |
||
54 | * Chunk size in bytes |
||
55 | * |
||
56 | * The secret will be divided equally. This value defines the chunk size and |
||
57 | * how many bytes will get encoded at once. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $chunkSize = 1; |
||
62 | |||
63 | /** |
||
64 | * The random generator |
||
65 | * |
||
66 | * @var Generator |
||
67 | */ |
||
68 | protected $randomGenerator; |
||
69 | |||
70 | /** |
||
71 | * Maximum number of shares required |
||
72 | * |
||
73 | * @var float |
||
74 | */ |
||
75 | protected $maxShares = 3; |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 19 | public function getRandomGenerator(): Generator |
|
88 | |||
89 | /** |
||
90 | * @inheritdoc |
||
91 | * @return Shamir |
||
92 | */ |
||
93 | 64 | public function setRandomGenerator(Generator $generator): Shamir |
|
99 | |||
100 | /** |
||
101 | * Returns chunk size in bytes |
||
102 | * |
||
103 | * @return int |
||
104 | */ |
||
105 | 14 | public function getChunkSize(): int |
|
109 | |||
110 | /** |
||
111 | * Sets chunk size in bytes |
||
112 | * |
||
113 | * If maximum shares have been set already, the chunk |
||
114 | * size might have been set with it. It is not possible |
||
115 | * to set a smaller size than required by shares. |
||
116 | * |
||
117 | * @see |
||
118 | * @param int $chunkSize Size in number of bytes |
||
119 | * @return Shamir |
||
120 | * @throws OutOfRangeException |
||
121 | */ |
||
122 | 37 | public function setChunkSize($chunkSize): Shamir |
|
139 | |||
140 | /** |
||
141 | * Configure encoding parameters |
||
142 | * |
||
143 | * Depending on the number of required shares, we need to change |
||
144 | * prime number, key length, chunk size and more. |
||
145 | * |
||
146 | * If the chunk size has been set already, it will be changed, if |
||
147 | * it is smaller than the necessary size. |
||
148 | * |
||
149 | * @see setChunkSize() |
||
150 | * @param int $max Maximum number of keys needed |
||
151 | * @return Shamir |
||
152 | * @throws \OutOfRangeException |
||
153 | */ |
||
154 | 19 | protected function setMaxShares($max): Shamir |
|
190 | |||
191 | /** |
||
192 | * Calculate modulo of any given number using prime |
||
193 | * |
||
194 | * @param int Number |
||
195 | * @return int Module of number |
||
196 | */ |
||
197 | 18 | protected function modulo($number) |
|
203 | |||
204 | /** |
||
205 | * Returns decomposition of the greatest common divisor of a and b |
||
206 | * |
||
207 | * @param int $a |
||
208 | * @param int $b |
||
209 | * @return array |
||
210 | */ |
||
211 | 18 | protected function gcdD($a, $b): array |
|
223 | |||
224 | /** |
||
225 | * Calculates the inverse modulo |
||
226 | * |
||
227 | * @param int $number |
||
228 | * @return int |
||
229 | */ |
||
230 | 18 | protected function inverseModulo($number) |
|
238 | |||
239 | /** |
||
240 | * Calculates the reverse coefficients |
||
241 | * |
||
242 | * @param array $keyX |
||
243 | * @param int $threshold |
||
244 | * @return array |
||
245 | * @throws \RuntimeException |
||
246 | */ |
||
247 | 18 | protected function reverseCoefficients(array $keyX, $threshold): array |
|
271 | |||
272 | /** |
||
273 | * Generate random coefficients |
||
274 | * |
||
275 | * @param int $threshold Number of coefficients needed |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | 18 | protected function generateCoefficients($threshold): array |
|
292 | |||
293 | /** |
||
294 | * Calculate y values of polynomial curve using horner's method |
||
295 | * |
||
296 | * Horner converts a polynomial formula like |
||
297 | * 11 + 7x - 5x^2 - 4x^3 + 2x^4 |
||
298 | * into a more efficient formula |
||
299 | * 11 + x * ( 7 + x * ( -5 + x * ( -4 + x * 2 ) ) ) |
||
300 | * |
||
301 | * @see https://en.wikipedia.org/wiki/Horner%27s_method |
||
302 | * @param int $xCoordinate X coordinate |
||
303 | * @param array $coefficients Polynomial coefficients |
||
304 | * @return int Y coordinate |
||
305 | */ |
||
306 | 18 | protected function hornerMethod($xCoordinate, array $coefficients) |
|
315 | |||
316 | /** |
||
317 | * Converts from $fromBaseInput to $toBaseInput |
||
318 | * |
||
319 | * @param string $numberInput |
||
320 | * @param string $fromBaseInput |
||
321 | * @param string $toBaseInput |
||
322 | * @return string |
||
323 | */ |
||
324 | 39 | protected static function convBase($numberInput, $fromBaseInput, $toBaseInput) |
|
362 | |||
363 | /** |
||
364 | * Unpack a binary string and convert it into decimals |
||
365 | * |
||
366 | * Convert each chunk of a binary data into decimal numbers. |
||
367 | * |
||
368 | * @param string $string Binary string |
||
369 | * @return array Array with decimal converted numbers |
||
370 | */ |
||
371 | 18 | protected function unpack($string): array |
|
390 | |||
391 | /** |
||
392 | * Returns maximum length of converted string to new base |
||
393 | * |
||
394 | * Calculate the maximum length of a string, which can be |
||
395 | * represented with the number of given bytes and convert |
||
396 | * its base. |
||
397 | * |
||
398 | * @param int $bytes Bytes used to represent a string |
||
399 | * @return int Number of chars |
||
400 | */ |
||
401 | 18 | protected function maxKeyLength($bytes): int |
|
408 | |||
409 | /** |
||
410 | * Divide secret into chunks and calculate coordinates |
||
411 | * |
||
412 | * @param string $secret Secret |
||
413 | * @param int $shares Number of parts to share |
||
414 | * @param int $threshold Minimum number of shares required for decryption |
||
415 | * @return array |
||
416 | */ |
||
417 | 18 | protected function divideSecret($secret, $shares, $threshold): array |
|
435 | |||
436 | /** |
||
437 | * @inheritdoc |
||
438 | */ |
||
439 | 19 | public function share($secret, $shares, $threshold = 2): array |
|
498 | |||
499 | /** |
||
500 | * Decode and merge secret chunks |
||
501 | * |
||
502 | * @param array $keyX Keys for X coordinates |
||
503 | * @param array $keyY Keys for Y coordinates |
||
504 | * @param int $bytes Chunk size in bytes |
||
505 | * @param int $keyLen Key length in chunks |
||
506 | * @param int $threshold Minimum number of shares required for decryption |
||
507 | * @return string |
||
508 | */ |
||
509 | 18 | protected function joinSecret($keyX, $keyY, $bytes, $keyLen, $threshold): string |
|
531 | |||
532 | /** |
||
533 | * @inheritdoc |
||
534 | */ |
||
535 | 18 | public function recover(array $keys): string |
|
598 | } |
||
599 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.