@@ -220,7 +220,7 @@ |
||
| 220 | 220 | ), |
| 221 | 221 | ), |
| 222 | 222 | ); |
| 223 | - */ |
|
| 223 | + */ |
|
| 224 | 224 | |
| 225 | 225 | // Are we using right-to-left orientation? |
| 226 | 226 | $first = $context['right_to_left'] ? 'last' : 'first'; |
@@ -27,146 +27,146 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | if (!defined('RANDOM_COMPAT_READ_BUFFER')) { |
| 30 | - define('RANDOM_COMPAT_READ_BUFFER', 8); |
|
| 30 | + define('RANDOM_COMPAT_READ_BUFFER', 8); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | if (!is_callable('random_bytes')) { |
| 34 | - /** |
|
| 35 | - * Unless open_basedir is enabled, use /dev/urandom for |
|
| 36 | - * random numbers in accordance with best practices |
|
| 37 | - * |
|
| 38 | - * Why we use /dev/urandom and not /dev/random |
|
| 39 | - * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers |
|
| 40 | - * |
|
| 41 | - * @param int $bytes |
|
| 42 | - * |
|
| 43 | - * @throws Exception |
|
| 44 | - * |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - function random_bytes($bytes) |
|
| 48 | - { |
|
| 49 | - /** @var resource $fp */ |
|
| 50 | - static $fp = null; |
|
| 34 | + /** |
|
| 35 | + * Unless open_basedir is enabled, use /dev/urandom for |
|
| 36 | + * random numbers in accordance with best practices |
|
| 37 | + * |
|
| 38 | + * Why we use /dev/urandom and not /dev/random |
|
| 39 | + * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers |
|
| 40 | + * |
|
| 41 | + * @param int $bytes |
|
| 42 | + * |
|
| 43 | + * @throws Exception |
|
| 44 | + * |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + function random_bytes($bytes) |
|
| 48 | + { |
|
| 49 | + /** @var resource $fp */ |
|
| 50 | + static $fp = null; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * This block should only be run once |
|
| 54 | - */ |
|
| 55 | - if (empty($fp)) { |
|
| 56 | - /** |
|
| 57 | - * We use /dev/urandom if it is a char device. |
|
| 58 | - * We never fall back to /dev/random |
|
| 59 | - */ |
|
| 60 | - /** @var resource|bool $fp */ |
|
| 61 | - $fp = fopen('/dev/urandom', 'rb'); |
|
| 62 | - if (is_resource($fp)) { |
|
| 63 | - /** @var array<string, int> $st */ |
|
| 64 | - $st = fstat($fp); |
|
| 65 | - if (($st['mode'] & 0170000) !== 020000) { |
|
| 66 | - fclose($fp); |
|
| 67 | - $fp = false; |
|
| 68 | - } |
|
| 69 | - } |
|
| 52 | + /** |
|
| 53 | + * This block should only be run once |
|
| 54 | + */ |
|
| 55 | + if (empty($fp)) { |
|
| 56 | + /** |
|
| 57 | + * We use /dev/urandom if it is a char device. |
|
| 58 | + * We never fall back to /dev/random |
|
| 59 | + */ |
|
| 60 | + /** @var resource|bool $fp */ |
|
| 61 | + $fp = fopen('/dev/urandom', 'rb'); |
|
| 62 | + if (is_resource($fp)) { |
|
| 63 | + /** @var array<string, int> $st */ |
|
| 64 | + $st = fstat($fp); |
|
| 65 | + if (($st['mode'] & 0170000) !== 020000) { |
|
| 66 | + fclose($fp); |
|
| 67 | + $fp = false; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - if (is_resource($fp)) { |
|
| 72 | - /** |
|
| 73 | - * stream_set_read_buffer() does not exist in HHVM |
|
| 74 | - * |
|
| 75 | - * If we don't set the stream's read buffer to 0, PHP will |
|
| 76 | - * internally buffer 8192 bytes, which can waste entropy |
|
| 77 | - * |
|
| 78 | - * stream_set_read_buffer returns 0 on success |
|
| 79 | - */ |
|
| 80 | - if (is_callable('stream_set_read_buffer')) { |
|
| 81 | - stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 82 | - } |
|
| 83 | - if (is_callable('stream_set_chunk_size')) { |
|
| 84 | - stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 71 | + if (is_resource($fp)) { |
|
| 72 | + /** |
|
| 73 | + * stream_set_read_buffer() does not exist in HHVM |
|
| 74 | + * |
|
| 75 | + * If we don't set the stream's read buffer to 0, PHP will |
|
| 76 | + * internally buffer 8192 bytes, which can waste entropy |
|
| 77 | + * |
|
| 78 | + * stream_set_read_buffer returns 0 on success |
|
| 79 | + */ |
|
| 80 | + if (is_callable('stream_set_read_buffer')) { |
|
| 81 | + stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 82 | + } |
|
| 83 | + if (is_callable('stream_set_chunk_size')) { |
|
| 84 | + stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - try { |
|
| 90 | - /** @var int $bytes */ |
|
| 91 | - $bytes = RandomCompat_intval($bytes); |
|
| 92 | - } catch (TypeError $ex) { |
|
| 93 | - throw new TypeError( |
|
| 94 | - 'random_bytes(): $bytes must be an integer' |
|
| 95 | - ); |
|
| 96 | - } |
|
| 89 | + try { |
|
| 90 | + /** @var int $bytes */ |
|
| 91 | + $bytes = RandomCompat_intval($bytes); |
|
| 92 | + } catch (TypeError $ex) { |
|
| 93 | + throw new TypeError( |
|
| 94 | + 'random_bytes(): $bytes must be an integer' |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - if ($bytes < 1) { |
|
| 99 | - throw new Error( |
|
| 100 | - 'Length must be greater than 0' |
|
| 101 | - ); |
|
| 102 | - } |
|
| 98 | + if ($bytes < 1) { |
|
| 99 | + throw new Error( |
|
| 100 | + 'Length must be greater than 0' |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * This if() block only runs if we managed to open a file handle |
|
| 106 | - * |
|
| 107 | - * It does not belong in an else {} block, because the above |
|
| 108 | - * if (empty($fp)) line is logic that should only be run once per |
|
| 109 | - * page load. |
|
| 110 | - */ |
|
| 111 | - if (is_resource($fp)) { |
|
| 112 | - /** |
|
| 113 | - * @var int |
|
| 114 | - */ |
|
| 115 | - $remaining = $bytes; |
|
| 104 | + /** |
|
| 105 | + * This if() block only runs if we managed to open a file handle |
|
| 106 | + * |
|
| 107 | + * It does not belong in an else {} block, because the above |
|
| 108 | + * if (empty($fp)) line is logic that should only be run once per |
|
| 109 | + * page load. |
|
| 110 | + */ |
|
| 111 | + if (is_resource($fp)) { |
|
| 112 | + /** |
|
| 113 | + * @var int |
|
| 114 | + */ |
|
| 115 | + $remaining = $bytes; |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * @var string|bool |
|
| 119 | - */ |
|
| 120 | - $buf = ''; |
|
| 117 | + /** |
|
| 118 | + * @var string|bool |
|
| 119 | + */ |
|
| 120 | + $buf = ''; |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * We use fread() in a loop to protect against partial reads |
|
| 124 | - */ |
|
| 125 | - do { |
|
| 126 | - /** |
|
| 127 | - * @var string|bool |
|
| 128 | - */ |
|
| 129 | - $read = fread($fp, $remaining); |
|
| 130 | - if (!is_string($read)) { |
|
| 131 | - if ($read === false) { |
|
| 132 | - /** |
|
| 133 | - * We cannot safely read from the file. Exit the |
|
| 134 | - * do-while loop and trigger the exception condition |
|
| 135 | - * |
|
| 136 | - * @var string|bool |
|
| 137 | - */ |
|
| 138 | - $buf = false; |
|
| 139 | - break; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - /** |
|
| 143 | - * Decrease the number of bytes returned from remaining |
|
| 144 | - */ |
|
| 145 | - $remaining -= RandomCompat_strlen($read); |
|
| 146 | - /** |
|
| 147 | - * @var string|bool |
|
| 148 | - */ |
|
| 149 | - $buf = $buf . $read; |
|
| 150 | - } while ($remaining > 0); |
|
| 122 | + /** |
|
| 123 | + * We use fread() in a loop to protect against partial reads |
|
| 124 | + */ |
|
| 125 | + do { |
|
| 126 | + /** |
|
| 127 | + * @var string|bool |
|
| 128 | + */ |
|
| 129 | + $read = fread($fp, $remaining); |
|
| 130 | + if (!is_string($read)) { |
|
| 131 | + if ($read === false) { |
|
| 132 | + /** |
|
| 133 | + * We cannot safely read from the file. Exit the |
|
| 134 | + * do-while loop and trigger the exception condition |
|
| 135 | + * |
|
| 136 | + * @var string|bool |
|
| 137 | + */ |
|
| 138 | + $buf = false; |
|
| 139 | + break; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + /** |
|
| 143 | + * Decrease the number of bytes returned from remaining |
|
| 144 | + */ |
|
| 145 | + $remaining -= RandomCompat_strlen($read); |
|
| 146 | + /** |
|
| 147 | + * @var string|bool |
|
| 148 | + */ |
|
| 149 | + $buf = $buf . $read; |
|
| 150 | + } while ($remaining > 0); |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Is our result valid? |
|
| 154 | - */ |
|
| 155 | - if (is_string($buf)) { |
|
| 156 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
| 157 | - /** |
|
| 158 | - * Return our random entropy buffer here: |
|
| 159 | - */ |
|
| 160 | - return $buf; |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 152 | + /** |
|
| 153 | + * Is our result valid? |
|
| 154 | + */ |
|
| 155 | + if (is_string($buf)) { |
|
| 156 | + if (RandomCompat_strlen($buf) === $bytes) { |
|
| 157 | + /** |
|
| 158 | + * Return our random entropy buffer here: |
|
| 159 | + */ |
|
| 160 | + return $buf; |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * If we reach here, PHP has failed us. |
|
| 167 | - */ |
|
| 168 | - throw new Exception( |
|
| 169 | - 'Error reading from source device' |
|
| 170 | - ); |
|
| 171 | - } |
|
| 165 | + /** |
|
| 166 | + * If we reach here, PHP has failed us. |
|
| 167 | + */ |
|
| 168 | + throw new Exception( |
|
| 169 | + 'Error reading from source device' |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -27,65 +27,65 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | if (!is_callable('random_bytes')) { |
| 30 | - /** |
|
| 31 | - * Windows with PHP < 5.3.0 will not have the function |
|
| 32 | - * openssl_random_pseudo_bytes() available, so let's use |
|
| 33 | - * CAPICOM to work around this deficiency. |
|
| 34 | - * |
|
| 35 | - * @param int $bytes |
|
| 36 | - * |
|
| 37 | - * @throws Exception |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - function random_bytes($bytes) |
|
| 42 | - { |
|
| 43 | - try { |
|
| 44 | - /** @var int $bytes */ |
|
| 45 | - $bytes = RandomCompat_intval($bytes); |
|
| 46 | - } catch (TypeError $ex) { |
|
| 47 | - throw new TypeError( |
|
| 48 | - 'random_bytes(): $bytes must be an integer' |
|
| 49 | - ); |
|
| 50 | - } |
|
| 30 | + /** |
|
| 31 | + * Windows with PHP < 5.3.0 will not have the function |
|
| 32 | + * openssl_random_pseudo_bytes() available, so let's use |
|
| 33 | + * CAPICOM to work around this deficiency. |
|
| 34 | + * |
|
| 35 | + * @param int $bytes |
|
| 36 | + * |
|
| 37 | + * @throws Exception |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + function random_bytes($bytes) |
|
| 42 | + { |
|
| 43 | + try { |
|
| 44 | + /** @var int $bytes */ |
|
| 45 | + $bytes = RandomCompat_intval($bytes); |
|
| 46 | + } catch (TypeError $ex) { |
|
| 47 | + throw new TypeError( |
|
| 48 | + 'random_bytes(): $bytes must be an integer' |
|
| 49 | + ); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - if ($bytes < 1) { |
|
| 53 | - throw new Error( |
|
| 54 | - 'Length must be greater than 0' |
|
| 55 | - ); |
|
| 56 | - } |
|
| 52 | + if ($bytes < 1) { |
|
| 53 | + throw new Error( |
|
| 54 | + 'Length must be greater than 0' |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** @var string $buf */ |
|
| 59 | - $buf = ''; |
|
| 60 | - if (!class_exists('COM')) { |
|
| 61 | - throw new Error( |
|
| 62 | - 'COM does not exist' |
|
| 63 | - ); |
|
| 64 | - } |
|
| 65 | - /** @var COM $util */ |
|
| 66 | - $util = new COM('CAPICOM.Utilities.1'); |
|
| 67 | - $execCount = 0; |
|
| 58 | + /** @var string $buf */ |
|
| 59 | + $buf = ''; |
|
| 60 | + if (!class_exists('COM')) { |
|
| 61 | + throw new Error( |
|
| 62 | + 'COM does not exist' |
|
| 63 | + ); |
|
| 64 | + } |
|
| 65 | + /** @var COM $util */ |
|
| 66 | + $util = new COM('CAPICOM.Utilities.1'); |
|
| 67 | + $execCount = 0; |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Let's not let it loop forever. If we run N times and fail to |
|
| 71 | - * get N bytes of random data, then CAPICOM has failed us. |
|
| 72 | - */ |
|
| 73 | - do { |
|
| 74 | - $buf .= base64_decode((string) $util->GetRandom($bytes, 0)); |
|
| 75 | - if (RandomCompat_strlen($buf) >= $bytes) { |
|
| 76 | - /** |
|
| 77 | - * Return our random entropy buffer here: |
|
| 78 | - */ |
|
| 79 | - return (string) RandomCompat_substr($buf, 0, $bytes); |
|
| 80 | - } |
|
| 81 | - ++$execCount; |
|
| 82 | - } while ($execCount < $bytes); |
|
| 69 | + /** |
|
| 70 | + * Let's not let it loop forever. If we run N times and fail to |
|
| 71 | + * get N bytes of random data, then CAPICOM has failed us. |
|
| 72 | + */ |
|
| 73 | + do { |
|
| 74 | + $buf .= base64_decode((string) $util->GetRandom($bytes, 0)); |
|
| 75 | + if (RandomCompat_strlen($buf) >= $bytes) { |
|
| 76 | + /** |
|
| 77 | + * Return our random entropy buffer here: |
|
| 78 | + */ |
|
| 79 | + return (string) RandomCompat_substr($buf, 0, $bytes); |
|
| 80 | + } |
|
| 81 | + ++$execCount; |
|
| 82 | + } while ($execCount < $bytes); |
|
| 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 | } |
| 92 | 92 | \ No newline at end of file |
@@ -28,50 +28,50 @@ |
||
| 28 | 28 | |
| 29 | 29 | if (!is_callable('RandomCompat_intval')) { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Cast to an integer if we can, safely. |
|
| 33 | - * |
|
| 34 | - * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
| 35 | - * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
| 36 | - * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
| 37 | - * lose precision, so the <= and => operators might accidentally let a float |
|
| 38 | - * through. |
|
| 39 | - * |
|
| 40 | - * @param int|float $number The number we want to convert to an int |
|
| 41 | - * @param bool $fail_open Set to true to not throw an exception |
|
| 42 | - * |
|
| 43 | - * @return float|int |
|
| 44 | - * @psalm-suppress InvalidReturnType |
|
| 45 | - * |
|
| 46 | - * @throws TypeError |
|
| 47 | - */ |
|
| 48 | - function RandomCompat_intval($number, $fail_open = false) |
|
| 49 | - { |
|
| 50 | - if (is_int($number) || is_float($number)) { |
|
| 51 | - $number += 0; |
|
| 52 | - } elseif (is_numeric($number)) { |
|
| 53 | - /** @psalm-suppress InvalidOperand */ |
|
| 54 | - $number += 0; |
|
| 55 | - } |
|
| 56 | - /** @var int|float $number */ |
|
| 31 | + /** |
|
| 32 | + * Cast to an integer if we can, safely. |
|
| 33 | + * |
|
| 34 | + * If you pass it a float in the range (~PHP_INT_MAX, PHP_INT_MAX) |
|
| 35 | + * (non-inclusive), it will sanely cast it to an int. If you it's equal to |
|
| 36 | + * ~PHP_INT_MAX or PHP_INT_MAX, we let it fail as not an integer. Floats |
|
| 37 | + * lose precision, so the <= and => operators might accidentally let a float |
|
| 38 | + * through. |
|
| 39 | + * |
|
| 40 | + * @param int|float $number The number we want to convert to an int |
|
| 41 | + * @param bool $fail_open Set to true to not throw an exception |
|
| 42 | + * |
|
| 43 | + * @return float|int |
|
| 44 | + * @psalm-suppress InvalidReturnType |
|
| 45 | + * |
|
| 46 | + * @throws TypeError |
|
| 47 | + */ |
|
| 48 | + function RandomCompat_intval($number, $fail_open = false) |
|
| 49 | + { |
|
| 50 | + if (is_int($number) || is_float($number)) { |
|
| 51 | + $number += 0; |
|
| 52 | + } elseif (is_numeric($number)) { |
|
| 53 | + /** @psalm-suppress InvalidOperand */ |
|
| 54 | + $number += 0; |
|
| 55 | + } |
|
| 56 | + /** @var int|float $number */ |
|
| 57 | 57 | |
| 58 | - if ( |
|
| 59 | - is_float($number) |
|
| 60 | - && |
|
| 61 | - $number > ~PHP_INT_MAX |
|
| 62 | - && |
|
| 63 | - $number < PHP_INT_MAX |
|
| 64 | - ) { |
|
| 65 | - $number = (int) $number; |
|
| 66 | - } |
|
| 58 | + if ( |
|
| 59 | + is_float($number) |
|
| 60 | + && |
|
| 61 | + $number > ~PHP_INT_MAX |
|
| 62 | + && |
|
| 63 | + $number < PHP_INT_MAX |
|
| 64 | + ) { |
|
| 65 | + $number = (int) $number; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - if (is_int($number)) { |
|
| 69 | - return (int) $number; |
|
| 70 | - } elseif (!$fail_open) { |
|
| 71 | - throw new TypeError( |
|
| 72 | - 'Expected an integer.' |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - return $number; |
|
| 76 | - } |
|
| 68 | + if (is_int($number)) { |
|
| 69 | + return (int) $number; |
|
| 70 | + } elseif (!$fail_open) { |
|
| 71 | + throw new TypeError( |
|
| 72 | + 'Expected an integer.' |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + return $number; |
|
| 76 | + } |
|
| 77 | 77 | } |