@@ -27,67 +27,67 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | if (!is_callable('random_bytes')) { |
| 30 | - /** |
|
| 31 | - * If the libsodium PHP extension is loaded, we'll use it above any other |
|
| 32 | - * solution. |
|
| 33 | - * |
|
| 34 | - * libsodium-php project: |
|
| 35 | - * @ref https://github.com/jedisct1/libsodium-php |
|
| 36 | - * |
|
| 37 | - * @param int $bytes |
|
| 38 | - * |
|
| 39 | - * @throws Exception |
|
| 40 | - * |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - function random_bytes($bytes) |
|
| 44 | - { |
|
| 45 | - try { |
|
| 46 | - /** @var int $bytes */ |
|
| 47 | - $bytes = RandomCompat_intval($bytes); |
|
| 48 | - } catch (TypeError $ex) { |
|
| 49 | - throw new TypeError( |
|
| 50 | - 'random_bytes(): $bytes must be an integer' |
|
| 51 | - ); |
|
| 52 | - } |
|
| 30 | + /** |
|
| 31 | + * If the libsodium PHP extension is loaded, we'll use it above any other |
|
| 32 | + * solution. |
|
| 33 | + * |
|
| 34 | + * libsodium-php project: |
|
| 35 | + * @ref https://github.com/jedisct1/libsodium-php |
|
| 36 | + * |
|
| 37 | + * @param int $bytes |
|
| 38 | + * |
|
| 39 | + * @throws Exception |
|
| 40 | + * |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + function random_bytes($bytes) |
|
| 44 | + { |
|
| 45 | + try { |
|
| 46 | + /** @var int $bytes */ |
|
| 47 | + $bytes = RandomCompat_intval($bytes); |
|
| 48 | + } catch (TypeError $ex) { |
|
| 49 | + throw new TypeError( |
|
| 50 | + 'random_bytes(): $bytes must be an integer' |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if ($bytes < 1) { |
|
| 55 | - throw new Error( |
|
| 56 | - 'Length must be greater than 0' |
|
| 57 | - ); |
|
| 58 | - } |
|
| 54 | + if ($bytes < 1) { |
|
| 55 | + throw new Error( |
|
| 56 | + 'Length must be greater than 0' |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @var string |
|
| 62 | - */ |
|
| 63 | - $buf = ''; |
|
| 60 | + /** |
|
| 61 | + * @var string |
|
| 62 | + */ |
|
| 63 | + $buf = ''; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be |
|
| 67 | - * generated in one invocation. |
|
| 68 | - */ |
|
| 69 | - if ($bytes > 2147483647) { |
|
| 70 | - for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
| 71 | - $n = ($bytes - $i) > 1073741824 |
|
| 72 | - ? 1073741824 |
|
| 73 | - : $bytes - $i; |
|
| 74 | - $buf .= Sodium::randombytes_buf((int) $n); |
|
| 75 | - } |
|
| 76 | - } else { |
|
| 77 | - $buf .= Sodium::randombytes_buf((int) $bytes); |
|
| 78 | - } |
|
| 65 | + /** |
|
| 66 | + * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be |
|
| 67 | + * generated in one invocation. |
|
| 68 | + */ |
|
| 69 | + if ($bytes > 2147483647) { |
|
| 70 | + for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
| 71 | + $n = ($bytes - $i) > 1073741824 |
|
| 72 | + ? 1073741824 |
|
| 73 | + : $bytes - $i; |
|
| 74 | + $buf .= Sodium::randombytes_buf((int) $n); |
|
| 75 | + } |
|
| 76 | + } else { |
|
| 77 | + $buf .= Sodium::randombytes_buf((int) $bytes); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - if (is_string($buf)) { |
|
| 81 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
| 82 | - return $buf; |
|
| 83 | - } |
|
| 84 | - } |
|
| 80 | + if (is_string($buf)) { |
|
| 81 | + if (RandomCompat_strlen($buf) === $bytes) { |
|
| 82 | + return $buf; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * If we reach here, PHP has failed us. |
|
| 88 | - */ |
|
| 89 | - throw new Exception( |
|
| 90 | - 'Could not gather sufficient random data' |
|
| 91 | - ); |
|
| 92 | - } |
|
| 86 | + /** |
|
| 87 | + * If we reach here, PHP has failed us. |
|
| 88 | + */ |
|
| 89 | + throw new Exception( |
|
| 90 | + 'Could not gather sufficient random data' |
|
| 91 | + ); |
|
| 92 | + } |
|
| 93 | 93 | } |
@@ -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 | } |
@@ -27,65 +27,65 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | if (!is_callable('random_bytes')) { |
| 30 | - /** |
|
| 31 | - * If the libsodium PHP extension is loaded, we'll use it above any other |
|
| 32 | - * solution. |
|
| 33 | - * |
|
| 34 | - * libsodium-php project: |
|
| 35 | - * @ref https://github.com/jedisct1/libsodium-php |
|
| 36 | - * |
|
| 37 | - * @param int $bytes |
|
| 38 | - * |
|
| 39 | - * @throws Exception |
|
| 40 | - * |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - function random_bytes($bytes) |
|
| 44 | - { |
|
| 45 | - try { |
|
| 46 | - /** @var int $bytes */ |
|
| 47 | - $bytes = RandomCompat_intval($bytes); |
|
| 48 | - } catch (TypeError $ex) { |
|
| 49 | - throw new TypeError( |
|
| 50 | - 'random_bytes(): $bytes must be an integer' |
|
| 51 | - ); |
|
| 52 | - } |
|
| 30 | + /** |
|
| 31 | + * If the libsodium PHP extension is loaded, we'll use it above any other |
|
| 32 | + * solution. |
|
| 33 | + * |
|
| 34 | + * libsodium-php project: |
|
| 35 | + * @ref https://github.com/jedisct1/libsodium-php |
|
| 36 | + * |
|
| 37 | + * @param int $bytes |
|
| 38 | + * |
|
| 39 | + * @throws Exception |
|
| 40 | + * |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + function random_bytes($bytes) |
|
| 44 | + { |
|
| 45 | + try { |
|
| 46 | + /** @var int $bytes */ |
|
| 47 | + $bytes = RandomCompat_intval($bytes); |
|
| 48 | + } catch (TypeError $ex) { |
|
| 49 | + throw new TypeError( |
|
| 50 | + 'random_bytes(): $bytes must be an integer' |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if ($bytes < 1) { |
|
| 55 | - throw new Error( |
|
| 56 | - 'Length must be greater than 0' |
|
| 57 | - ); |
|
| 58 | - } |
|
| 54 | + if ($bytes < 1) { |
|
| 55 | + throw new Error( |
|
| 56 | + 'Length must be greater than 0' |
|
| 57 | + ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be |
|
| 62 | - * generated in one invocation. |
|
| 63 | - */ |
|
| 64 | - /** @var string|bool $buf */ |
|
| 65 | - if ($bytes > 2147483647) { |
|
| 66 | - $buf = ''; |
|
| 67 | - for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
| 68 | - $n = ($bytes - $i) > 1073741824 |
|
| 69 | - ? 1073741824 |
|
| 70 | - : $bytes - $i; |
|
| 71 | - $buf .= \Sodium\randombytes_buf($n); |
|
| 72 | - } |
|
| 73 | - } else { |
|
| 74 | - /** @var string|bool $buf */ |
|
| 75 | - $buf = \Sodium\randombytes_buf($bytes); |
|
| 76 | - } |
|
| 60 | + /** |
|
| 61 | + * \Sodium\randombytes_buf() doesn't allow more than 2147483647 bytes to be |
|
| 62 | + * generated in one invocation. |
|
| 63 | + */ |
|
| 64 | + /** @var string|bool $buf */ |
|
| 65 | + if ($bytes > 2147483647) { |
|
| 66 | + $buf = ''; |
|
| 67 | + for ($i = 0; $i < $bytes; $i += 1073741824) { |
|
| 68 | + $n = ($bytes - $i) > 1073741824 |
|
| 69 | + ? 1073741824 |
|
| 70 | + : $bytes - $i; |
|
| 71 | + $buf .= \Sodium\randombytes_buf($n); |
|
| 72 | + } |
|
| 73 | + } else { |
|
| 74 | + /** @var string|bool $buf */ |
|
| 75 | + $buf = \Sodium\randombytes_buf($bytes); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - if (is_string($buf)) { |
|
| 79 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
| 80 | - return $buf; |
|
| 81 | - } |
|
| 82 | - } |
|
| 78 | + if (is_string($buf)) { |
|
| 79 | + if (RandomCompat_strlen($buf) === $bytes) { |
|
| 80 | + return $buf; |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * If we reach here, PHP has failed us. |
|
| 86 | - */ |
|
| 87 | - throw new Exception( |
|
| 88 | - 'Could not gather sufficient random data' |
|
| 89 | - ); |
|
| 90 | - } |
|
| 84 | + /** |
|
| 85 | + * If we reach here, PHP has failed us. |
|
| 86 | + */ |
|
| 87 | + throw new Exception( |
|
| 88 | + 'Could not gather sufficient random data' |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | 91 | } |
@@ -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 | } |