1 | <?php |
||
2 | |||
3 | namespace mattvb91\TronTrx\Support; |
||
4 | |||
5 | /** |
||
6 | * @codeCoverageIgnore |
||
7 | */ |
||
8 | class Crypto |
||
9 | { |
||
10 | public static function bc2bin($num) |
||
11 | { |
||
12 | return self::dec2base($num, 256); |
||
13 | } |
||
14 | |||
15 | public static function dec2base($dec, $base, $digits = false) |
||
16 | { |
||
17 | if ($base < 2 || $base > 256) { |
||
18 | die("Invalid Base: " . $base); |
||
0 ignored issues
–
show
|
|||
19 | } |
||
20 | |||
21 | bcscale(0); |
||
22 | $value = ""; |
||
23 | |||
24 | if (!$digits) { |
||
25 | $digits = self::digits($base); |
||
26 | } |
||
27 | |||
28 | while ($dec > $base - 1) { |
||
29 | $rest = bcmod($dec, $base); |
||
30 | $dec = bcdiv($dec, $base); |
||
31 | $value = $digits[$rest] . $value; |
||
32 | } |
||
33 | $value = $digits[intval($dec)] . $value; |
||
34 | |||
35 | return (string)$value; |
||
36 | } |
||
37 | |||
38 | public static function base2dec($value, $base, $digits = false) |
||
39 | { |
||
40 | if ($base < 2 || $base > 256) { |
||
41 | die("Invalid Base: " . $base); |
||
0 ignored issues
–
show
|
|||
42 | } |
||
43 | |||
44 | bcscale(0); |
||
45 | |||
46 | if ($base < 37) { |
||
47 | $value = strtolower($value); |
||
48 | } |
||
49 | if (!$digits) { |
||
50 | $digits = self::digits($base); |
||
51 | } |
||
52 | |||
53 | $size = strlen($value); |
||
54 | $dec = "0"; |
||
55 | |||
56 | for ($loop = 0; $loop < $size; $loop++) { |
||
57 | $element = strpos($digits, $value[$loop]); |
||
58 | $power = bcpow($base, $size - $loop - 1); |
||
59 | $dec = bcadd($dec, bcmul($element, $power)); |
||
60 | } |
||
61 | |||
62 | return (string)$dec; |
||
63 | } |
||
64 | |||
65 | public static function digits($base) |
||
66 | { |
||
67 | if ($base > 64) { |
||
68 | $digits = ""; |
||
69 | for ($loop = 0; $loop < 256; $loop++) { |
||
70 | $digits .= chr($loop); |
||
71 | } |
||
72 | } else { |
||
73 | $digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
||
74 | $digits .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; |
||
75 | } |
||
76 | $digits = substr($digits, 0, $base); |
||
77 | |||
78 | return (string)$digits; |
||
79 | } |
||
80 | |||
81 | public static function bin2bc($num) |
||
82 | { |
||
83 | return self::base2dec($num, 256); |
||
84 | } |
||
85 | } |
||
86 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.