1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 5.4 and 7 |
4
|
|
|
* |
5
|
|
|
* @package Payever\Core |
6
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
7
|
|
|
* @copyright 2017-2019 payever GmbH |
8
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Payever\ExternalIntegration\Core; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHP version 5.4 and 7 |
15
|
|
|
* |
16
|
|
|
* @package Payever\Core |
17
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
18
|
|
|
* @copyright 2017-2019 payever GmbH |
19
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
20
|
|
|
*/ |
21
|
|
|
class PseudoRandomStringGenerator |
22
|
|
|
{ |
23
|
|
|
protected static $fallbackKeyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generates a random string, using a cryptographically secure pseudo-random number generator. |
27
|
|
|
* Falling back to non-CSPRNG generation when no source of random available. |
28
|
|
|
* |
29
|
|
|
* @param int $length |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
* |
33
|
|
|
* @throws \Exception when CSPRNG source failed to provide random bytes |
34
|
|
|
*/ |
35
|
|
|
public function generate($length = 64) |
36
|
|
|
{ |
37
|
|
|
$binaryString = false; |
38
|
|
|
|
39
|
|
|
if (function_exists('random_bytes')) { |
40
|
|
|
$binaryString = random_bytes($length); |
41
|
|
|
} elseif (function_exists('mcrypt_create_iv')) { |
42
|
|
|
$binaryString = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); |
|
|
|
|
43
|
|
|
} elseif (function_exists('openssl_random_pseudo_bytes')) { |
44
|
|
|
$binaryString = openssl_random_pseudo_bytes($length); |
45
|
|
|
} elseif (($stream = fopen('/dev/urandom', 'rb'))) { |
46
|
|
|
stream_set_read_buffer($stream, 0); |
47
|
|
|
$binaryString = fread($stream, $length); |
48
|
|
|
fclose($stream); |
49
|
|
|
} else { |
50
|
|
|
// well, we've tried |
51
|
|
|
$chain = ''; |
52
|
|
|
$max = mb_strlen(static::$fallbackKeyspace, '8bit') - 1; |
53
|
|
|
for ($i = 0; $i < $length; ++$i) { |
54
|
|
|
$chain .= static::$fallbackKeyspace[rand(0, $max)]; |
55
|
|
|
} |
56
|
|
|
$binaryString = hex2bin($chain); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($binaryString === false) { |
60
|
|
|
throw new \RuntimeException("Unable to generate random string."); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->binToHex($binaryString, $length); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $binaryData |
68
|
|
|
* @param $length |
69
|
|
|
* |
70
|
|
|
* @return bool|string |
71
|
|
|
*/ |
72
|
|
|
protected function binToHex($binaryData, $length) |
73
|
|
|
{ |
74
|
|
|
return substr(bin2hex($binaryData), 0, $length); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.