Passed
Push — 1.0 ( dbac97...74fb09 )
by Morven
04:14
created

StringEncryptor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A mcrypt() 0 12 1
A create() 0 3 1
A addPKCS5Padding() 0 12 2
A __construct() 0 3 1
A encode() 0 6 1
A encrypt() 0 9 3
A setEncryption() 0 4 1
A get() 0 3 1
A simplexor() 0 17 3
A setHash() 0 4 1
1
<?php
2
3
namespace SilverCommerce\OrdersAdmin\Tools;
4
5
/**
6
 * Class designed to deal with encrypting strings sent to it, either using XOR
7
 * or AES encryption.
8
 *
9
 * To use this class you create it, then set the encryption type and then call
10
 * encrypt().
11
 *
12
 * EG:
13
 *
14
 * Using simple XOR encryption:
15
 *
16
 * $encrypt = StringEncryptor::create('encrypt this')
17
 *              ->setHash('hashcode')
18
 *              ->encrypt()
19
 *              ->get();
20
 *
21
 *
22
 * XOR encryption then base 64 encoded:
23
 *
24
 * $encrypt = StringEncryptor::create('encrypt this')
25
 *              ->setHash('hashcode')
26
 *              ->encrypt()
27
 *              ->encode()
28
 *              ->get();
29
 *
30
 * MCrypt AES encryption:
31
 *
32
 * $encrypt = StringEncryptor::create('encrypt this')
33
 *              ->setHash('hashcode')
34
 *              ->setEncryption('MCRYPT')
35
 *              ->encrypt()
36
 *              ->get();
37
 *
38
 */
39
40
class StringEncryptor
41
{
42
43
    /**
44
     * String that we are going to encrypt
45
     * @var String
46
     */
47
    private $data;
48
49
    /**
50
     * String that is being encrypted
51
     * @var String
52
     */
53
    private $encrypted_data;
54
55
    /**
56
     * Choose encryption type
57
     * @var String
58
     * @default XOR
59
     */
60
    private $encryption = 'XOR';
61
62
    /**
63
     * Hash used to encrypt our string
64
     * @var String
65
     */
66
    private $hash;
67
68
    private function __construct($string)
69
    {
70
        $this->data = $string;
71
    }
72
73
    /**
74
     * Factory method allowing chaining
75
     *
76
     * @para, $string string to encrypt
77
     * @return StringEncryptor
78
     */
79
    public static function create($string)
80
    {
81
        return new StringEncryptor($string);
82
    }
83
84
    /**
85
     * Set our encryption type
86
     *
87
     * @param $type Type of encryption
0 ignored issues
show
Bug introduced by
The type SilverCommerce\OrdersAdmin\Tools\Type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
     * @return self
89
     */
90
    public function setEncryption($type)
91
    {
92
        $this->encryption = $type;
93
        return $this;
94
    }
95
96
    /**
97
     * Set our hash
98
     *
99
     * @param $hash
100
     * @return self
101
     */
102
    public function setHash($hash)
103
    {
104
        $this->hash = $hash;
105
        return $this;
106
    }
107
108
    /**
109
     * Get our encrypted data
110
     *
111
     * @return String
112
     */
113
    public function get()
114
    {
115
        return $this->encrypted_data;
116
    }
117
118
    /**
119
     * Perform our data encryption
120
     *
121
     * @return self
122
     */
123
    public function encrypt()
124
    {
125
        if ($this->encryption == 'XOR') {
126
            $this->encrypted_data = $this->simplexor();
127
        } elseif ($this->encryption == 'MCRYPT') {
128
            $this->encrypted_data = $this->mcrypt();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->mcrypt() of type SilverCommerce\OrdersAdmin\Tools\StringEncryptor is incompatible with the declared type string of property $encrypted_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
129
        }
130
131
        return $this;
132
    }
133
134
    /**
135
     * Base 64 encode the data, ready for transit
136
     *
137
     * @return self
138
     */
139
    public function encode()
140
    {
141
        // Encode data string
142
        $this->encrypted_data = base64_encode($this->encrypted_data);
143
144
        return $this;
145
    }
146
147
148
    /**
149
     * SimpleXor encryption algorithm
150
     *
151
     * return self
152
     */
153
    private function simplexor()
154
    {
155
        $KeyList = array();
156
        $output = "";
157
158
        // Convert $Key into array of ASCII values
159
        for ($i = 0; $i < strlen($this->hash); $i++) {
160
            $KeyList[$i] = ord(substr($this->hash, $i, 1));
161
        }
162
163
        // Step through string a character at a time
164
        for ($i = 0; $i < strlen($this->data); $i++) {
165
            $output.= chr(ord(substr($this->data, $i, 1)) ^ ($KeyList[$i % strlen($this->hash)]));
166
        }
167
168
        // Return the result
169
        return $output;
170
    }
171
172
    /**
173
     * Encrypt our data using PHP mcrypt and AES with PKCS5 padding
174
     *
175
     * @return self
176
     */
177
    private function mcrypt()
178
    {
179
        $strIV = $this->hash;
180
181
        // add PKCS5 padding to the text to be encypted
182
        $strIn = $this->addPKCS5Padding();
183
184
        // perform encryption with PHP's MCRYPT module
185
        $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->hash, $strIn, MCRYPT_MODE_CBC, $strIV);
0 ignored issues
show
Deprecated Code introduced by
The function mcrypt_encrypt() has been deprecated: 7.1 ( Ignorable by Annotation )

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

185
        $output = /** @scrutinizer ignore-deprecated */ mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->hash, $strIn, MCRYPT_MODE_CBC, $strIV);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
186
187
        // perform hex encoding and return with @ symbol
188
        return bin2hex($output);
189
    }
190
191
    /**
192
     * PHP's mcrypt does not have built in PKCS5 Padding, so we use this
193
     */
194
    private function addPKCS5Padding()
195
    {
196
        $blocksize = 16;
197
        $padding = "";
198
199
       // Pad input to an even block size boundary
200
       $padlength = $blocksize - (strlen($this->data) % $blocksize);
201
        for ($i = 1; $i <= $padlength; $i++) {
202
            $padding .= chr($padlength);
203
        }
204
205
        return $this->data . $padding;
206
    }
207
}
208