GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Binary   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 105
c 1
b 0
f 0
dl 0
loc 220
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 119 7
A setKey() 0 5 1
A encrypt() 0 20 4
A decrypt() 0 12 3
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Encryptions;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class Binary
20
 *
21
 * @package O2System\Security\Encryptions
22
 */
23
class Binary
24
{
25
    /**
26
     * Binary::$charactersMap
27
     *
28
     * @var array
29
     */
30
    private static $charactersMap = [];
31
    /**
32
     * Binary::$crypt
33
     *
34
     * Crypt instance.
35
     *
36
     * @var Crypt
37
     */
38
    private $crypt;
39
40
    // ------------------------------------------------------------------------
41
42
    /**
43
     * Binary::__construct
44
     */
45
    public function __construct()
46
    {
47
        $this->crypt = new Crypt();
48
49
        /**
50
         * Special character
51
         */
52
        $specialCharacters = [
53
            "\n",
54
            "\r",
55
            "\t",
56
            '~',
57
            '`',
58
            '!',
59
            '@',
60
            '#',
61
            '$',
62
            '%',
63
            '^',
64
            '&',
65
            '*',
66
            '(',
67
            ')',
68
            '-',
69
            '_',
70
            '=',
71
            '+',
72
            '{',
73
            '}',
74
            '[',
75
            ']',
76
            ':',
77
            ';',
78
            '"',
79
            "'",
80
            '<',
81
            '>',
82
            '/',
83
            '?',
84
            '\\',
85
            '|',
86
            '.',
87
            ',',
88
        ];
89
90
        // ------------------------------------------------------------------------
91
92
        /**
93
         * Lowercase letter character
94
         */
95
        $lowerLetterCharacter = [
96
            'a',
97
            'b',
98
            'c',
99
            'd',
100
            'e',
101
            'f',
102
            'g',
103
            'h',
104
            'i',
105
            'j',
106
            'k',
107
            'l',
108
            'm',
109
            'n',
110
            'o',
111
            'p',
112
            'q',
113
            'r',
114
            's',
115
            't',
116
            'u',
117
            'v',
118
            'w',
119
            'x',
120
            'y',
121
            'z',
122
        ];
123
124
        // ------------------------------------------------------------------------
125
126
        /**
127
         * Uppercase letter character
128
         */
129
        $upperLetterCharacter = array_map('strtoupper', $lowerLetterCharacter);
130
131
        // ------------------------------------------------------------------------
132
133
        /**
134
         * Binary character
135
         */
136
        $numericCharacter = range(0, 9, 1);
137
138
        // ------------------------------------------------------------------------
139
140
        static::$charactersMap = array_merge(
0 ignored issues
show
Bug introduced by
Since $charactersMap is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $charactersMap to at least protected.
Loading history...
141
            $specialCharacters,
142
            $lowerLetterCharacter,
143
            $upperLetterCharacter,
144
            $numericCharacter
145
        );
146
147
        if (class_exists('\O2System\Framework', false) or class_exists('\O2System\Reactor', false)) {
148
            $key = config()->getItem('security')->offsetGet('encryptionKey');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
            $key = /** @scrutinizer ignore-call */ config()->getItem('security')->offsetGet('encryptionKey');
Loading history...
149
            $letters = str_split($key);
150
            $cryptoKey = 0;
151
152
            foreach ($letters as $letter) {
153
                if ($number = array_search($letter, static::$charactersMap)) {
154
                    $cryptoKey = $cryptoKey + $number;
155
                }
156
            }
157
158
            $charactersMap = static::$charactersMap;
159
            static::$charactersMap = [];
160
161
            if ($cryptoKey > 0) {
162
                foreach ($charactersMap as $key => $value) {
163
                    static::$charactersMap[ $key * $cryptoKey ] = $value;
164
                }
165
            }
166
        }
167
    }
168
169
    // ------------------------------------------------------------------------
170
171
    /**
172
     * Binary::encrypt
173
     *
174
     * Encrypt string into numbers.
175
     *
176
     * @param string $string String to be encrypted.
177
     *
178
     * @return string
179
     */
180
    public function encrypt($string)
181
    {
182
        $numbers = [];
183
        $letters = str_split($this->crypt->encrypt($string));
184
185
        $i = 0;
186
        foreach ($letters as $letter) {
187
            if ($number = array_search($letter, static::$charactersMap)) {
0 ignored issues
show
Bug introduced by
Since $charactersMap is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $charactersMap to at least protected.
Loading history...
188
                if ($i == 20) {
189
                    $number = $number . PHP_EOL;
190
                    $i = 0;
191
                }
192
193
                $numbers[] = $number;
194
195
                $i++;
196
            }
197
        }
198
199
        return implode(' ', $numbers);
200
    }
201
202
    // ------------------------------------------------------------------------
203
204
    /**
205
     * Binary::decrypt
206
     *
207
     * Decrypt numbers.
208
     *
209
     * @param string $numbers Numbers to be decrypted.
210
     *
211
     * @return string
212
     */
213
    public function decrypt($numbers)
214
    {
215
        $letters = [];
216
        $numbers = explode(' ', str_replace(PHP_EOL, '', $numbers));
217
218
        foreach ($numbers as $number) {
219
            if (array_key_exists($number, static::$charactersMap)) {
0 ignored issues
show
Bug introduced by
Since $charactersMap is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $charactersMap to at least protected.
Loading history...
220
                $letters[] = static::$charactersMap[ $number ];
221
            }
222
        }
223
224
        return $this->crypt->decrypt(implode('', $letters));
225
    }
226
227
    // ------------------------------------------------------------------------
228
229
    /**
230
     * Binary::setKey
231
     *
232
     * Sets numeric encryption protection key.
233
     *
234
     * @param string $key Custom encryption key.
235
     *
236
     * @return static
237
     */
238
    public function setKey($key)
239
    {
240
        $this->crypt->setKey($key);
241
242
        return $this;
243
    }
244
}