add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Core / BaseConverter.php
1 <?php
2
3 /**
4 * A Utility class for converting between raw binary strings and a given
5 * list of characters
6 *
7 * PHP version 5.3
8 *
9 * @category PHPCryptLib
10 * @package Core
11 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
12 * @copyright 2011 The Authors
13 * @license http://www.opensource.org/licenses/mit-license.html MIT License
14 * @version Build @@version@@
15 */
16
17 namespace CryptLib\Core;
18
19 /**
20 * A Utility class for converting between raw binary strings and a given
21 * list of characters
22 *
23 * @category PHPCryptLib
24 * @package Core
25 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
26 */
27 class BaseConverter {
28
29 /**
30 * Convert from a raw binary string to a string of characters
31 *
32 * @param string $string The string to convert from
33 * @param string $characters The list of characters to convert to
34 *
35 * @return string The converted string
36 */
37 public static function convertFromBinary($string, $characters) {
38 if ($string === '' || empty($characters)) {
39 return '';
40 }
41 $string = str_split($string);
42 $callback = function($str) {
43 return ord($str);
44 };
45 $string = array_map($callback, $string);
46 $converted = static::baseConvert($string, 256, strlen($characters));
47 $callback = function ($num) use ($characters) {
48 return $characters[$num];
49 };
50 $ret = implode('', array_map($callback, $converted));
51 return $ret;
52 }
53
54 /**
55 * Convert to a raw binary string from a string of characters
56 *
57 * @param string $string The string to convert from
58 * @param string $characters The list of characters to convert to
59 *
60 * @return string The converted string
61 */
62 public static function convertToBinary($string, $characters) {
63 if (empty($string) || empty($characters)) {
64 return '';
65 }
66 $string = str_split($string);
67 $callback = function($str) use ($characters) {
68 return strpos($characters, $str);
69 };
70 $string = array_map($callback, $string);
71 $converted = static::baseConvert($string, strlen($characters), 256);
72 $callback = function ($num) {
73 return chr($num);
74 };
75 return implode('', array_map($callback, $converted));
76 }
77
78 /**
79 * Convert an array of input blocks to another numeric base
80 *
81 * This function was modified from an implementation found on StackOverflow.
82 * Special Thanks to @KeithRandall for supplying the implementation.
83 *
84 * @param int[] $source The source number, as an array
85 * @param int $srcBase The source base as an integer
86 * @param int $dstBase The destination base as an integer
87 *
88 * @see http://codegolf.stackexchange.com/questions/1620/arb/1626#1626
89 * @return int[] An array of integers in the encoded base
90 */
91 public static function baseConvert(array $source, $srcBase, $dstBase) {
92 if ($dstBase < 2) {
93 $message = sprintf('Invalid Destination Base: %d', $dstBase);
94 throw new \InvalidArgumentException($message);
95 }
96 $result = array();
97 $count = count($source);
98 while ($count) {
99 $itMax = $count;
100 $remainder = $count = $i = 0;
101 while($i < $itMax) {
102 $dividend = $source[$i++] + $remainder * $srcBase;
103 $remainder = $dividend % $dstBase;
104 $res = ($dividend - $remainder) / $dstBase;
105 if ($count || $res) {
106 $source[$count++] = $res;
107 }
108 }
109 $result[] = $remainder;
110 }
111 return array_reverse($result);
112 }
113
114 }