1 | <?php |
||
29 | final class Random |
||
30 | { |
||
31 | /** |
||
32 | * Get random bytes from Mcrypt |
||
33 | * |
||
34 | * @param int $bytes Number of bytes to get |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | 20 | private static function fromMcrypt($bytes) |
|
39 | { |
||
40 | 20 | $ret = \mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); |
|
41 | |||
42 | 20 | if ($ret === false) { |
|
43 | self::toss(); // @codeCoverageIgnore |
||
44 | } |
||
45 | |||
46 | 20 | return $ret; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Return securely generated random bytes. |
||
51 | * |
||
52 | * @param int $bytes Number of bytes to get |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 20 | public static function bytes($bytes) |
|
57 | { |
||
58 | 20 | if (\function_exists('random_bytes')) { |
|
59 | return \random_bytes($bytes); |
||
60 | 20 | } elseif (\function_exists('mcrypt_create_iv')) { |
|
61 | 20 | return self::fromMcrypt($bytes); |
|
62 | } |
||
63 | |||
64 | self::toss(); // @codeCoverageIgnore |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Throw an error when a failure occurs. |
||
69 | * |
||
70 | * @codeCoverageIgnore |
||
71 | */ |
||
72 | private static function toss() |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Deterministic seeded array shuffle function. |
||
81 | * |
||
82 | * @param array $array |
||
83 | * @param string $seed |
||
84 | * @param bool $secure |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public static function shuffle($array, $seed, $secure = true) |
|
120 | } |
||
121 |