@@ -26,7 +26,8 @@ discard block |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!is_callable('random_bytes')) { |
|
29 | +if (!is_callable('random_bytes')) |
|
30 | +{ |
|
30 | 31 | /** |
31 | 32 | * If the libsodium PHP extension is loaded, we'll use it above any other |
32 | 33 | * solution. |
@@ -42,16 +43,20 @@ discard block |
||
42 | 43 | */ |
43 | 44 | function random_bytes($bytes) |
44 | 45 | { |
45 | - try { |
|
46 | + try |
|
47 | + { |
|
46 | 48 | /** @var int $bytes */ |
47 | 49 | $bytes = RandomCompat_intval($bytes); |
48 | - } catch (TypeError $ex) { |
|
50 | + } |
|
51 | + catch (TypeError $ex) |
|
52 | + { |
|
49 | 53 | throw new TypeError( |
50 | 54 | 'random_bytes(): $bytes must be an integer' |
51 | 55 | ); |
52 | 56 | } |
53 | 57 | |
54 | - if ($bytes < 1) { |
|
58 | + if ($bytes < 1) |
|
59 | + { |
|
55 | 60 | throw new Error( |
56 | 61 | 'Length must be greater than 0' |
57 | 62 | ); |
@@ -66,19 +71,25 @@ discard block |
||
66 | 71 | * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be |
67 | 72 | * generated in one invocation. |
68 | 73 | */ |
69 | - if ($bytes > 2147483647) { |
|
70 | - for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
74 | + if ($bytes > 2147483647) |
|
75 | + { |
|
76 | + for ($i = 0; $i < $bytes; $i += 1073741824) |
|
77 | + { |
|
71 | 78 | $n = ($bytes - $i) > 1073741824 |
72 | 79 | ? 1073741824 |
73 | 80 | : $bytes - $i; |
74 | 81 | $buf .= Sodium::randombytes_buf((int) $n); |
75 | 82 | } |
76 | - } else { |
|
83 | + } |
|
84 | + else |
|
85 | + { |
|
77 | 86 | $buf .= Sodium::randombytes_buf((int) $bytes); |
78 | 87 | } |
79 | 88 | |
80 | - if (is_string($buf)) { |
|
81 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
89 | + if (is_string($buf)) |
|
90 | + { |
|
91 | + if (RandomCompat_strlen($buf) === $bytes) |
|
92 | + { |
|
82 | 93 | return $buf; |
83 | 94 | } |
84 | 95 | } |
@@ -26,7 +26,8 @@ discard block |
||
26 | 26 | * SOFTWARE. |
27 | 27 | */ |
28 | 28 | |
29 | -if (!is_callable('random_bytes')) { |
|
29 | +if (!is_callable('random_bytes')) |
|
30 | +{ |
|
30 | 31 | /** |
31 | 32 | * If the libsodium PHP extension is loaded, we'll use it above any other |
32 | 33 | * solution. |
@@ -42,16 +43,20 @@ discard block |
||
42 | 43 | */ |
43 | 44 | function random_bytes($bytes) |
44 | 45 | { |
45 | - try { |
|
46 | + try |
|
47 | + { |
|
46 | 48 | /** @var int $bytes */ |
47 | 49 | $bytes = RandomCompat_intval($bytes); |
48 | - } catch (TypeError $ex) { |
|
50 | + } |
|
51 | + catch (TypeError $ex) |
|
52 | + { |
|
49 | 53 | throw new TypeError( |
50 | 54 | 'random_bytes(): $bytes must be an integer' |
51 | 55 | ); |
52 | 56 | } |
53 | 57 | |
54 | - if ($bytes < 1) { |
|
58 | + if ($bytes < 1) |
|
59 | + { |
|
55 | 60 | throw new Error( |
56 | 61 | 'Length must be greater than 0' |
57 | 62 | ); |
@@ -62,21 +67,27 @@ discard block |
||
62 | 67 | * generated in one invocation. |
63 | 68 | */ |
64 | 69 | /** @var string|bool $buf */ |
65 | - if ($bytes > 2147483647) { |
|
70 | + if ($bytes > 2147483647) |
|
71 | + { |
|
66 | 72 | $buf = ''; |
67 | - for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
73 | + for ($i = 0; $i < $bytes; $i += 1073741824) |
|
74 | + { |
|
68 | 75 | $n = ($bytes - $i) > 1073741824 |
69 | 76 | ? 1073741824 |
70 | 77 | : $bytes - $i; |
71 | 78 | $buf .= \Sodium\randombytes_buf($n); |
72 | 79 | } |
73 | - } else { |
|
80 | + } |
|
81 | + else |
|
82 | + { |
|
74 | 83 | /** @var string|bool $buf */ |
75 | 84 | $buf = \Sodium\randombytes_buf($bytes); |
76 | 85 | } |
77 | 86 | |
78 | - if (is_string($buf)) { |
|
79 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
87 | + if (is_string($buf)) |
|
88 | + { |
|
89 | + if (RandomCompat_strlen($buf) === $bytes) |
|
90 | + { |
|
80 | 91 | return $buf; |
81 | 92 | } |
82 | 93 | } |