|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Author: Ryan Gilfether |
|
4
|
|
|
* URL: http://www.gilfether.com/phpCrypt |
|
5
|
|
|
* Date: March 26, 2013 |
|
6
|
|
|
* Copyright (C) 2013 Ryan Gilfether |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of phpCrypt |
|
9
|
|
|
* |
|
10
|
|
|
* phpCrypt is free software; you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU General Public License as published by |
|
12
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
13
|
|
|
* (at your option) any later version |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU General Public License |
|
21
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace PHP_Crypt; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A base class that should not be used directly. It is intended as a base |
|
28
|
|
|
* object that should be extended and provides tools that child objects may use. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Ryan Gilfether |
|
31
|
|
|
* @link http://www.gilfether.com/phpcrypt |
|
32
|
|
|
* @copyright 2013 Ryan Gilfether |
|
33
|
|
|
*/ |
|
34
|
|
|
class Core |
|
35
|
|
|
{ |
|
36
|
|
|
/** @type integer HASH_LEN The length of md5() hash string */ |
|
37
|
|
|
const HASH_LEN = 16; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Destructor |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function __destruct() |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Convert hexidecimal to a binary string (ex: "00110110") |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $hex A string containing a hexidecimal number |
|
64
|
|
|
* @return string A string representation of a binary |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function hex2Bin($hex) |
|
67
|
|
|
{ |
|
68
|
|
|
// if we do not have an even number of hex characters |
|
69
|
|
|
// append a 0 to the beginning to make it even |
|
70
|
|
|
if (strlen($hex) % 2) { |
|
71
|
|
|
$hex = "0$hex"; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$parts = str_split($hex, 2); |
|
75
|
|
|
$parts = array_map(function($v) { |
|
76
|
|
|
$v = base_convert($v, 16, 2); |
|
77
|
|
|
return str_pad($v, 8, "0", STR_PAD_LEFT); |
|
78
|
|
|
}, $parts); |
|
79
|
|
|
|
|
80
|
|
|
return implode("", $parts); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Convert hex to a string |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $hex A string representation of Hex (IE: "1a2b3c" not 0x1a2b3c) |
|
88
|
|
|
* @return string a string |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function hex2Str($hex) |
|
91
|
|
|
{ |
|
92
|
|
|
// php version >= 5.4 have a hex2bin function, use it |
|
93
|
|
|
// if it exists |
|
94
|
|
|
if (function_exists("hex2bin")) { |
|
95
|
|
|
return hex2bin($hex); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$parts = str_split($hex, 2); |
|
99
|
|
|
$parts = array_map(function($v) { |
|
100
|
|
|
return chr(Core::hex2Dec($v)); |
|
101
|
|
|
}, $parts); |
|
102
|
|
|
|
|
103
|
|
|
return implode("", $parts); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Converts Hex to Decimal |
|
109
|
|
|
* This function just calls php's hexdec() function, but I |
|
110
|
|
|
* encapsulated it in this function to keep things uniform |
|
111
|
|
|
* and have all possible conversion function available in |
|
112
|
|
|
* the Cipher class |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $hex A hex number to convert to decimal |
|
115
|
|
|
* @return integer A decimal number |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function hex2Dec($hex) |
|
118
|
|
|
{ |
|
119
|
|
|
return hexdec($hex); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Convert binary string (ie 00110110) to hex |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $bin A binary string |
|
127
|
|
|
* @return string A string representation of hexidecimal number |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function bin2Hex($bin) |
|
130
|
|
|
{ |
|
131
|
|
|
$parts = str_split($bin, 8); |
|
132
|
|
|
|
|
133
|
|
|
$parts = array_map(function($v) { |
|
134
|
|
|
$v = str_pad($v, 8, "0", STR_PAD_LEFT); |
|
135
|
|
|
$v = dechex(bindec($v)); |
|
136
|
|
|
return str_pad($v, 2, "0", STR_PAD_LEFT); |
|
137
|
|
|
}, $parts); |
|
138
|
|
|
|
|
139
|
|
|
return implode("", $parts); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Converts a binary representation (ie 01101011) back to a string |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $bin a binary representation string |
|
147
|
|
|
* @return string A string of characters representing the binary |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function bin2Str($bin) |
|
150
|
|
|
{ |
|
151
|
|
|
$hex = self::bin2Hex($bin); |
|
152
|
|
|
return self::hex2Str($hex); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Convert a binary string (ie: 01101011) to a decimal number |
|
158
|
|
|
* |
|
159
|
|
|
* @param string A string representation of a binary number |
|
160
|
|
|
* @param string $bin |
|
161
|
|
|
* @return integer The number converted from the binary string |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function bin2Dec($bin) |
|
164
|
|
|
{ |
|
165
|
|
|
return bindec($bin); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Convert a string to hex |
|
171
|
|
|
* This function calls the PHP bin2hex(), and is here |
|
172
|
|
|
* for consistency with the other string functions |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $str A string |
|
175
|
|
|
* @return string A string representation of hexidecimal number |
|
176
|
|
|
*/ |
|
177
|
|
|
public static function str2Hex($str) |
|
178
|
|
|
{ |
|
179
|
|
|
return bin2hex($str); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Convert a string of characters to a decimal number |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $str The string to convert to decimal |
|
187
|
|
|
* @return integer The integer converted from the string |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function str2Dec($str) |
|
190
|
|
|
{ |
|
191
|
|
|
$hex = self::str2Hex($str); |
|
192
|
|
|
return self::hex2Dec($hex); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Converts a string to binary representation (ie 01101011) |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $str A string |
|
200
|
|
|
* @return string A binary representation of the the string |
|
201
|
|
|
*/ |
|
202
|
|
|
public static function str2Bin($str) |
|
203
|
|
|
{ |
|
204
|
|
|
$hex = self::str2Hex($str); |
|
205
|
|
|
$parts = str_split($hex, 2); |
|
206
|
|
|
|
|
207
|
|
|
$parts = array_map(function($v) { |
|
208
|
|
|
return Core::hex2Bin($v); |
|
209
|
|
|
}, $parts); |
|
210
|
|
|
|
|
211
|
|
|
return implode("", $parts); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Converts Decimal to Hex |
|
217
|
|
|
* This function just calls php's dechex() function, but I |
|
218
|
|
|
* encapsulated it in this function to keep things uniform |
|
219
|
|
|
* and have all possible conversion function available in |
|
220
|
|
|
* the Cipher class |
|
221
|
|
|
* |
|
222
|
|
|
* The parameter $req_bytes will pad the return hex with NULL (00) |
|
223
|
|
|
* until the hex represents the number of bytes given to $req_bytes |
|
224
|
|
|
* This is because dechex() drops null bytes from the Hex, which may |
|
225
|
|
|
* be needed in some cases |
|
226
|
|
|
* |
|
227
|
|
|
* @param integer $dec A decimal number to convert |
|
228
|
|
|
* @param integer $req_bytes Optional, forces the string to be at least |
|
229
|
|
|
* $req_bytes in size, this is needed because on occasion left most null bytes |
|
230
|
|
|
* are dropped in dechex(), causing the string to have a shorter byte |
|
231
|
|
|
* size than the initial integer. |
|
232
|
|
|
* @return string A hexidecimal representation of the decimal number |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function dec2Hex($dec, $req_bytes = 0) |
|
235
|
|
|
{ |
|
236
|
|
|
$hex = dechex($dec); |
|
237
|
|
|
|
|
238
|
|
|
// if we do not have an even number of hex characters |
|
239
|
|
|
// append a 0 to the beginning. dechex() drops leading 0's |
|
240
|
|
|
if (strlen($hex) % 2) { |
|
241
|
|
|
$hex = "0$hex"; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
// if the number of bytes in the hex is less than |
|
245
|
|
|
// what we need it to be, add null bytes to the |
|
246
|
|
|
// front of the hex to padd it to the required size |
|
247
|
|
|
if (($req_bytes * 2) > strlen($hex)) { |
|
248
|
|
|
$hex = str_pad($hex, ($req_bytes * 2), "0", STR_PAD_LEFT); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $hex; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Converts Decimal to Binary |
|
257
|
|
|
* This function just calls php's decbin() function, but I |
|
258
|
|
|
* encapsulated it in this function to keep things uniform |
|
259
|
|
|
* and have all possible conversion function available in |
|
260
|
|
|
* the Cipher class |
|
261
|
|
|
* |
|
262
|
|
|
* @param integer $dec A decimal number to convert |
|
263
|
|
|
* @param integer $req_bytes Optional, forces the string to be at least |
|
264
|
|
|
* $req_bytes in size, this is needed because on occasion left most null bytes |
|
265
|
|
|
* are dropped in dechex(), causing the string to have a shorter byte |
|
266
|
|
|
* size than the initial integer. |
|
267
|
|
|
* @return string A binary representation of the decimal number |
|
268
|
|
|
*/ |
|
269
|
|
|
public static function dec2Bin($dec, $req_bytes = 0) |
|
270
|
|
|
{ |
|
271
|
|
|
$hex = self::dec2Hex($dec, $req_bytes); |
|
272
|
|
|
return self::hex2Bin($hex); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Convert a decimal to a string of bytes |
|
278
|
|
|
* |
|
279
|
|
|
* @param integer $dec A decimal number |
|
280
|
|
|
* @param integer $req_bytes Optional, forces the string to be at least |
|
281
|
|
|
* $req_bytes in size, this is needed because on occasion left most null bytes |
|
282
|
|
|
* are dropped in dechex(), causing the string to have a shorter byte |
|
283
|
|
|
* size than the initial integer. |
|
284
|
|
|
* @return string A string with the number of bytes equal to $dec |
|
285
|
|
|
*/ |
|
286
|
|
|
public static function dec2Str($dec, $req_bytes = 0) |
|
287
|
|
|
{ |
|
288
|
|
|
$hex = self::dec2Hex($dec, $req_bytes); |
|
289
|
|
|
return self::hex2Str($hex); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* XORs two binary strings (representation of binary, ie 01101011), |
|
295
|
|
|
* assumed to be equal length |
|
296
|
|
|
* |
|
297
|
|
|
* @param string $a A string that represents binary |
|
298
|
|
|
* @param string $b A string that represents binary |
|
299
|
|
|
* @return string A representation of binary |
|
300
|
|
|
*/ |
|
301
|
|
|
public static function xorBin($a, $b) |
|
302
|
|
|
{ |
|
303
|
|
|
$len_a = strlen($a); |
|
304
|
|
|
$len_b = strlen($b); |
|
305
|
|
|
$width = $len_a; |
|
|
|
|
|
|
306
|
|
|
|
|
307
|
|
|
// first determine if the two binary strings are the same length, |
|
308
|
|
|
// and if not get them to the same length |
|
309
|
|
|
if ($len_a > $len_b) |
|
310
|
|
|
{ |
|
311
|
|
|
$width = $len_a; |
|
312
|
|
|
$b = str_pad($b, $width, "0", STR_PAD_LEFT); |
|
313
|
|
|
} else if ($len_a < $len_b) |
|
314
|
|
|
{ |
|
315
|
|
|
$width = $len_b; |
|
316
|
|
|
$a = str_pad($a, $width, "0", STR_PAD_LEFT); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
// fortunately PHP knows how to XOR each byte in a string |
|
320
|
|
|
// so we don't have to loop to do it |
|
321
|
|
|
$bin = self::bin2Str($a) ^ self::bin2Str($b); |
|
322
|
|
|
return self::str2Bin($bin); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* ExclusiveOR hex values. Supports an unlimited number of parameters. |
|
328
|
|
|
* The values are string representations of hex values |
|
329
|
|
|
* IE: "0a1b2c3d" not 0x0a1b2c3d |
|
330
|
|
|
* |
|
331
|
|
|
* @param string Unlimited number parameters, each a string representation of hex |
|
332
|
|
|
* @return string A string representation of the result in Hex |
|
333
|
|
|
*/ |
|
334
|
|
|
public static function xorHex() |
|
335
|
|
|
{ |
|
336
|
|
|
$hex = func_get_args(); |
|
337
|
|
|
$count = func_num_args(); |
|
338
|
|
|
|
|
339
|
|
|
// we need a minimum of 2 values |
|
340
|
|
|
if ($count < 2) { |
|
341
|
|
|
return false; |
|
|
|
|
|
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
// first get all hex values to an even number |
|
345
|
|
|
array_walk($hex, function(&$val, $i) { |
|
|
|
|
|
|
346
|
|
|
if (strlen($val) % 2) { |
|
347
|
|
|
$val = "0".$val; |
|
348
|
|
|
} |
|
349
|
|
|
}); |
|
350
|
|
|
|
|
351
|
|
|
$res = 0; |
|
352
|
|
|
for ($i = 0; $i < $count; ++$i) |
|
353
|
|
|
{ |
|
354
|
|
|
// if this is the first loop, set the 'result' to the first |
|
355
|
|
|
// hex value |
|
356
|
|
|
if ($i == 0) { |
|
357
|
|
|
$res = $hex[0]; |
|
358
|
|
|
} else |
|
359
|
|
|
{ |
|
360
|
|
|
// to make the code easier to follow |
|
361
|
|
|
$h1 = $res; |
|
362
|
|
|
$h2 = $hex[$i]; |
|
363
|
|
|
|
|
364
|
|
|
// get lengths |
|
365
|
|
|
$len1 = strlen($h1); |
|
366
|
|
|
$len2 = strlen($h2); |
|
367
|
|
|
|
|
368
|
|
|
// now check that both hex values are the same length, |
|
369
|
|
|
// if not pad them with 0's until they are |
|
370
|
|
|
if ($len1 > $len2) { |
|
371
|
|
|
$h2 = str_pad($h2, $len1, "0", STR_PAD_LEFT); |
|
372
|
|
|
} else if ($len1 < $len2) { |
|
373
|
|
|
$h1 = str_pad($h1, $len2, "0", STR_PAD_LEFT); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
// PHP knows how to XOR each byte in a string, so convert the |
|
377
|
|
|
// hex to a string, XOR, and convert back |
|
378
|
|
|
$res = self::hex2Str($h1) ^ self::hex2Str($h2); |
|
379
|
|
|
$res = self::str2Hex($res); |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
return $res; |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Forces an integer to be signed |
|
389
|
|
|
* |
|
390
|
|
|
* @param integer $int An integer |
|
391
|
|
|
* @return integer An signed integer |
|
392
|
|
|
*/ |
|
393
|
|
|
public static function sInt($int) |
|
394
|
|
|
{ |
|
395
|
|
|
$arr = unpack("i", pack("i", $int)); |
|
396
|
|
|
return $arr[1]; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Forces an integer to be unsigned |
|
402
|
|
|
* |
|
403
|
|
|
* @param integer $int A signed integer |
|
404
|
|
|
* @return integer An unsigned integer |
|
405
|
|
|
*/ |
|
406
|
|
|
public static function uInt($int) |
|
407
|
|
|
{ |
|
408
|
|
|
$arr = unpack("I", pack("I", $int)); |
|
409
|
|
|
$ret = $arr[1]; |
|
410
|
|
|
|
|
411
|
|
|
// On 32 bit platforms unpack() and pack() do not convert |
|
412
|
|
|
// from signed to unsigned properly all the time, it will return |
|
413
|
|
|
// the same negative number given to it, the work around is |
|
414
|
|
|
// to use sprintf(). |
|
415
|
|
|
// Tested with php 5.3.x on Windows XP & Linux 32bit |
|
416
|
|
|
if ($ret < 0) { |
|
417
|
|
|
$ret = sprintf("%u", $ret) + 0; |
|
418
|
|
|
} |
|
419
|
|
|
// convert from string to int |
|
420
|
|
|
|
|
421
|
|
|
return $ret; |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Forces an integer to be a 32 bit signed integer |
|
427
|
|
|
* |
|
428
|
|
|
* @param integer $int An integer |
|
429
|
|
|
* @return integer An signed 32 bit integer |
|
430
|
|
|
*/ |
|
431
|
|
View Code Duplication |
public static function sInt32($int) |
|
|
|
|
|
|
432
|
|
|
{ |
|
433
|
|
|
if (PHP_INT_SIZE === 4) { |
|
434
|
|
|
// 32 bit |
|
435
|
|
|
return self::sInt($int); |
|
436
|
|
|
} else // PHP_INT_SIZE === 8 // 64 bit |
|
437
|
|
|
{ |
|
438
|
|
|
$arr = unpack("l", pack("l", $int)); |
|
439
|
|
|
return $arr[1]; |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* Force an integer to be a 32 bit unsigned integer |
|
446
|
|
|
* |
|
447
|
|
|
* @param integer $int An integer |
|
448
|
|
|
* @return integer An unsigned 32 bit integer |
|
449
|
|
|
*/ |
|
450
|
|
View Code Duplication |
public static function uInt32($int) |
|
|
|
|
|
|
451
|
|
|
{ |
|
452
|
|
|
if (PHP_INT_SIZE === 4) { |
|
453
|
|
|
// 32 bit |
|
454
|
|
|
return self::uInt($int); |
|
455
|
|
|
} else // PHP_INT_SIZE === 8 // 64 bit |
|
456
|
|
|
{ |
|
457
|
|
|
$arr = unpack("L", pack("L", $int)); |
|
458
|
|
|
return $arr[1]; |
|
459
|
|
|
} |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* Converts an integer to the value for an signed char |
|
465
|
|
|
* |
|
466
|
|
|
* @param integer $int The integer to convert to a signed char |
|
467
|
|
|
* @return integer A signed integer, representing a signed char |
|
468
|
|
|
*/ |
|
469
|
|
|
public static function sChar($int) |
|
470
|
|
|
{ |
|
471
|
|
|
$arr = unpack("c", pack("c", $int)); |
|
472
|
|
|
return $arr[1]; |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* Converts an integer to the value for an unsigned char |
|
478
|
|
|
* |
|
479
|
|
|
* @param integer $int The integer to convert to a unsigned char |
|
480
|
|
|
* @return integer An unsigned integer, representing a unsigned char |
|
481
|
|
|
*/ |
|
482
|
|
|
public static function uChar($int) |
|
483
|
|
|
{ |
|
484
|
|
|
$arr = unpack("C", pack("C", $int)); |
|
485
|
|
|
return $arr[1]; |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Rotates bits Left, appending the bits pushed off the left onto the right |
|
491
|
|
|
* |
|
492
|
|
|
* @param integer $shifts The number of shifts left to make |
|
493
|
|
|
* @return integer The resulting value from the rotation |
|
494
|
|
|
*/ |
|
495
|
|
|
public static function rotBitsLeft32($i, $shifts) |
|
496
|
|
|
{ |
|
497
|
|
|
if ($shifts <= 0) { |
|
498
|
|
|
return $i; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
$shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
502
|
|
|
|
|
503
|
|
|
// this is causing problems on 32 bit platform |
|
504
|
|
|
//return self::uInt32(($i << $shifts) | ($i >> (32 - $shifts))); |
|
|
|
|
|
|
505
|
|
|
|
|
506
|
|
|
// so lets cheat: convert to binary string, rotate left, and |
|
507
|
|
|
// convert back to decimal |
|
508
|
|
|
$i = self::dec2Bin(self::uInt32($i), 4); |
|
509
|
|
|
$i = substr($i, $shifts).substr($i, 0, $shifts); |
|
510
|
|
|
return self::bin2Dec($i); |
|
511
|
|
|
|
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* Rotates bits right, appending the bits pushed off the right onto the left |
|
517
|
|
|
* |
|
518
|
|
|
* @param integer $shifts The number of shifts right to make |
|
519
|
|
|
* @return integer The resulting value from the rotation |
|
520
|
|
|
*/ |
|
521
|
|
|
public static function rotBitsRight32($i, $shifts) |
|
522
|
|
|
{ |
|
523
|
|
|
if ($shifts <= 0) { |
|
524
|
|
|
return $i; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
$shifts &= 0x1f; /* higher rotates would not bring anything */ |
|
528
|
|
|
|
|
529
|
|
|
// this might cause problems on 32 bit platforms since rotBitsLeft32 was |
|
530
|
|
|
// having a problem with some bit shifts on 32 bits |
|
531
|
|
|
// return self::uInt32(($i >> $shifts) | ($i << (32 - $shifts))); |
|
|
|
|
|
|
532
|
|
|
|
|
533
|
|
|
// so lets cheat: convert to binary string, rotate right, |
|
534
|
|
|
// and convert back to decimal |
|
535
|
|
|
$i = self::dec2Bin($i, 4); |
|
536
|
|
|
$i = substr($i, (-1 * $shifts)).substr($i, 0, (-1 * $shifts)); |
|
537
|
|
|
return self::bin2Dec($i); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
|
|
541
|
|
|
/** |
|
542
|
|
|
* Create a string of random bytes, used for creating an IV |
|
543
|
|
|
* and a random key. See PHP_Crypt::createKey() and PHP_Crypt::createIV() |
|
544
|
|
|
* There are 4 ways to auto generate random bytes by setting $src parameter |
|
545
|
|
|
* PHP_Crypt::RAND - Default, uses mt_rand() |
|
546
|
|
|
* PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
547
|
|
|
* PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
548
|
|
|
* PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
549
|
|
|
* |
|
550
|
|
|
* @param string $src Optional, Use the $src to create the random bytes |
|
551
|
|
|
* by default PHP_Crypt::RAND is used when $src is not specified |
|
552
|
|
|
* @param integer $byte_len The length of the byte string to create |
|
553
|
|
|
* @return string A random string of bytes |
|
554
|
|
|
*/ |
|
555
|
|
|
public static function randBytes($src = PHP_Crypt::RAND, $byte_len = PHP_Crypt::RAND_DEFAULT_SZ) |
|
556
|
|
|
{ |
|
557
|
|
|
$bytes = ""; |
|
558
|
|
|
$err_msg = ""; |
|
559
|
|
|
|
|
560
|
|
|
if ($src == PHP_Crypt::RAND_DEV_RAND) |
|
561
|
|
|
{ |
|
562
|
|
View Code Duplication |
if (file_exists(PHP_Crypt::RAND_DEV_RAND)) { |
|
563
|
|
|
$bytes = file_get_contents(PHP_CRYPT::RAND_DEV_RAND, false, null, 0, $byte_len); |
|
564
|
|
|
} else { |
|
565
|
|
|
$err_msg = PHP_Crypt::RAND_DEV_RAND." not found"; |
|
566
|
|
|
} |
|
567
|
|
|
} else if ($src == PHP_Crypt::RAND_DEV_URAND) |
|
568
|
|
|
{ |
|
569
|
|
View Code Duplication |
if (file_exists(PHP_Crypt::RAND_DEV_URAND)) { |
|
570
|
|
|
$bytes = file_get_contents(PHP_CRYPT::RAND_DEV_URAND, false, null, 0, $byte_len); |
|
571
|
|
|
} else { |
|
572
|
|
|
$err_msg = PHP_Crypt::RAND_DEV_URAND." not found"; |
|
573
|
|
|
} |
|
574
|
|
|
} else if ($src == PHP_Crypt::RAND_WIN_COM) |
|
575
|
|
|
{ |
|
576
|
|
|
if (extension_loaded('com_dotnet')) |
|
577
|
|
|
{ |
|
578
|
|
|
// http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx |
|
579
|
|
|
try |
|
580
|
|
|
{ |
|
581
|
|
|
// request a random number in $byte_len bytes, returned |
|
582
|
|
|
// as base_64 encoded string. This is because PHP munges the |
|
583
|
|
|
// binary data on Windows |
|
584
|
|
|
$com = @new \COM("CAPICOM.Utilities.1"); |
|
585
|
|
|
$bytes = $com->GetRandom($byte_len, 0); |
|
586
|
|
|
} catch (Exception $e) |
|
|
|
|
|
|
587
|
|
|
{ |
|
588
|
|
|
$err_msg = "Windows COM exception: ".$e->getMessage(); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
if (!$bytes) { |
|
592
|
|
|
$err_msg = "Windows COM failed to create random string of bytes"; |
|
593
|
|
|
} |
|
594
|
|
|
} else { |
|
595
|
|
|
$err_msg = "The COM_DOTNET extension is not loaded"; |
|
596
|
|
|
} |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
// trigger a warning if something went wrong |
|
600
|
|
|
if ($err_msg != "") { |
|
601
|
|
|
trigger_error("$err_msg. Defaulting to PHP_Crypt::RAND", E_USER_WARNING); |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
// if the random bytes where not created properly or PHP_Crypt::RAND was |
|
605
|
|
|
// passed as the $src param, create the bytes using mt_rand(). It's not |
|
606
|
|
|
// the most secure option but we have no other choice |
|
607
|
|
|
if (strlen($bytes) < $byte_len) |
|
608
|
|
|
{ |
|
609
|
|
|
$bytes = ""; |
|
610
|
|
|
|
|
611
|
|
|
// md5() hash a random number to get a 16 byte string, keep looping |
|
612
|
|
|
// until we have a string as long or longer than the ciphers block size |
|
613
|
|
|
for ($i = 0; ($i * self::HASH_LEN) < $byte_len; ++$i) { |
|
614
|
|
|
$bytes .= md5(mt_rand(), true); |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
// because $bytes may have come from mt_rand() or /dev/urandom which are not |
|
619
|
|
|
// cryptographically secure, lets add another layer of 'randomness' before |
|
620
|
|
|
// the final md5() below |
|
621
|
|
|
$bytes = str_shuffle($bytes); |
|
622
|
|
|
|
|
623
|
|
|
// md5() the $bytes to add extra randomness. Since md5() only returns |
|
624
|
|
|
// 16 bytes, we may need to loop to generate a string of $bytes big enough for |
|
625
|
|
|
// some ciphers which have a block size larger than 16 bytes |
|
626
|
|
|
$tmp = ""; |
|
627
|
|
|
$loop = ceil(strlen($bytes) / self::HASH_LEN); |
|
628
|
|
|
for ($i = 0; $i < $loop; ++$i) { |
|
629
|
|
|
$tmp .= md5(substr($bytes, ($i * self::HASH_LEN), self::HASH_LEN), true); |
|
630
|
|
|
} |
|
631
|
|
|
|
|
632
|
|
|
// grab the number of bytes equal to the requested $byte_len |
|
633
|
|
|
return substr($tmp, 0, $byte_len); |
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
?> |
|
|
|
|
|
|
637
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.