Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| XorEncoding | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| decode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| encode | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Encoding |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Encoding; |
| 16 | |
| 17 | /** |
| 18 | * XOR encoding class |
| 19 | * |
| 20 | * @package phpOMS\Utils\Encoding |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | final class XorEncoding |
| 26 | { |
| 27 | /** |
| 28 | * Decode text |
| 29 | * |
| 30 | * @param string $raw Source to encode |
| 31 | * @param string $key Key used for decoding |
| 32 | * |
| 33 | * @return string |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | public static function decode(string $raw, string $key) : string |
| 38 | { |
| 39 | return self::encode($raw, $key); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Encode source text |
| 44 | * |
| 45 | * @param string $source Source to encode |
| 46 | * @param string $key Key used for encoding |
| 47 | * |
| 48 | * @return string |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public static function encode(string $source, string $key) : string |
| 53 | { |
| 54 | $result = ''; |
| 55 | $length = \strlen($source); |
| 56 | $keyLength = \strlen($key) - 1; |
| 57 | |
| 58 | for ($i = 0, $j = 0; $i < $length; ++$i, ++$j) { |
| 59 | if ($j > $keyLength) { |
| 60 | $j = 0; |
| 61 | } |
| 62 | |
| 63 | $ascii = \ord($source[$i]) ^ \ord($key[$j]); |
| 64 | $result .= \chr($ascii); |
| 65 | } |
| 66 | |
| 67 | return $result; |
| 68 | } |
| 69 | } |