@@ -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 | } |
@@ -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 | } |
@@ -27,164 +27,164 @@ |
||
| 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 https://www.2uo.de/myths-about-urandom |
|
| 40 | - * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers |
|
| 41 | - * |
|
| 42 | - * @param int $bytes |
|
| 43 | - * |
|
| 44 | - * @throws Exception |
|
| 45 | - * |
|
| 46 | - * @return string |
|
| 47 | - */ |
|
| 48 | - function random_bytes($bytes) |
|
| 49 | - { |
|
| 50 | - /** @var resource $fp */ |
|
| 51 | - 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 https://www.2uo.de/myths-about-urandom |
|
| 40 | + * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers |
|
| 41 | + * |
|
| 42 | + * @param int $bytes |
|
| 43 | + * |
|
| 44 | + * @throws Exception |
|
| 45 | + * |
|
| 46 | + * @return string |
|
| 47 | + */ |
|
| 48 | + function random_bytes($bytes) |
|
| 49 | + { |
|
| 50 | + /** @var resource $fp */ |
|
| 51 | + static $fp = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * This block should only be run once |
|
| 55 | - */ |
|
| 56 | - if (empty($fp)) { |
|
| 57 | - /** |
|
| 58 | - * We don't want to ever read C:\dev\random, only /dev/urandom on |
|
| 59 | - * Unix-like operating systems. While we guard against this |
|
| 60 | - * condition in random.php, it doesn't hurt to be defensive in depth |
|
| 61 | - * here. |
|
| 62 | - * |
|
| 63 | - * To that end, we only try to open /dev/urandom if we're on a Unix- |
|
| 64 | - * like operating system (which means the directory separator is set |
|
| 65 | - * to "/" not "\". |
|
| 66 | - */ |
|
| 67 | - if (DIRECTORY_SEPARATOR === '/') { |
|
| 68 | - if (!is_readable('/dev/urandom')) { |
|
| 69 | - throw new Exception( |
|
| 70 | - 'Environment misconfiguration: ' . |
|
| 71 | - '/dev/urandom cannot be read.' |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - /** |
|
| 75 | - * We use /dev/urandom if it is a char device. |
|
| 76 | - * We never fall back to /dev/random |
|
| 77 | - */ |
|
| 78 | - /** @var resource|bool $fp */ |
|
| 79 | - $fp = fopen('/dev/urandom', 'rb'); |
|
| 80 | - if (is_resource($fp)) { |
|
| 81 | - /** @var array<string, int> $st */ |
|
| 82 | - $st = fstat($fp); |
|
| 83 | - if (($st['mode'] & 0170000) !== 020000) { |
|
| 84 | - fclose($fp); |
|
| 85 | - $fp = false; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - } |
|
| 53 | + /** |
|
| 54 | + * This block should only be run once |
|
| 55 | + */ |
|
| 56 | + if (empty($fp)) { |
|
| 57 | + /** |
|
| 58 | + * We don't want to ever read C:\dev\random, only /dev/urandom on |
|
| 59 | + * Unix-like operating systems. While we guard against this |
|
| 60 | + * condition in random.php, it doesn't hurt to be defensive in depth |
|
| 61 | + * here. |
|
| 62 | + * |
|
| 63 | + * To that end, we only try to open /dev/urandom if we're on a Unix- |
|
| 64 | + * like operating system (which means the directory separator is set |
|
| 65 | + * to "/" not "\". |
|
| 66 | + */ |
|
| 67 | + if (DIRECTORY_SEPARATOR === '/') { |
|
| 68 | + if (!is_readable('/dev/urandom')) { |
|
| 69 | + throw new Exception( |
|
| 70 | + 'Environment misconfiguration: ' . |
|
| 71 | + '/dev/urandom cannot be read.' |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + /** |
|
| 75 | + * We use /dev/urandom if it is a char device. |
|
| 76 | + * We never fall back to /dev/random |
|
| 77 | + */ |
|
| 78 | + /** @var resource|bool $fp */ |
|
| 79 | + $fp = fopen('/dev/urandom', 'rb'); |
|
| 80 | + if (is_resource($fp)) { |
|
| 81 | + /** @var array<string, int> $st */ |
|
| 82 | + $st = fstat($fp); |
|
| 83 | + if (($st['mode'] & 0170000) !== 020000) { |
|
| 84 | + fclose($fp); |
|
| 85 | + $fp = false; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - if (is_resource($fp)) { |
|
| 91 | - /** |
|
| 92 | - * stream_set_read_buffer() does not exist in HHVM |
|
| 93 | - * |
|
| 94 | - * If we don't set the stream's read buffer to 0, PHP will |
|
| 95 | - * internally buffer 8192 bytes, which can waste entropy |
|
| 96 | - * |
|
| 97 | - * stream_set_read_buffer returns 0 on success |
|
| 98 | - */ |
|
| 99 | - if (is_callable('stream_set_read_buffer')) { |
|
| 100 | - stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 101 | - } |
|
| 102 | - if (is_callable('stream_set_chunk_size')) { |
|
| 103 | - stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 90 | + if (is_resource($fp)) { |
|
| 91 | + /** |
|
| 92 | + * stream_set_read_buffer() does not exist in HHVM |
|
| 93 | + * |
|
| 94 | + * If we don't set the stream's read buffer to 0, PHP will |
|
| 95 | + * internally buffer 8192 bytes, which can waste entropy |
|
| 96 | + * |
|
| 97 | + * stream_set_read_buffer returns 0 on success |
|
| 98 | + */ |
|
| 99 | + if (is_callable('stream_set_read_buffer')) { |
|
| 100 | + stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 101 | + } |
|
| 102 | + if (is_callable('stream_set_chunk_size')) { |
|
| 103 | + stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - try { |
|
| 109 | - /** @var int $bytes */ |
|
| 110 | - $bytes = RandomCompat_intval($bytes); |
|
| 111 | - } catch (TypeError $ex) { |
|
| 112 | - throw new TypeError( |
|
| 113 | - 'random_bytes(): $bytes must be an integer' |
|
| 114 | - ); |
|
| 115 | - } |
|
| 108 | + try { |
|
| 109 | + /** @var int $bytes */ |
|
| 110 | + $bytes = RandomCompat_intval($bytes); |
|
| 111 | + } catch (TypeError $ex) { |
|
| 112 | + throw new TypeError( |
|
| 113 | + 'random_bytes(): $bytes must be an integer' |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - if ($bytes < 1) { |
|
| 118 | - throw new Error( |
|
| 119 | - 'Length must be greater than 0' |
|
| 120 | - ); |
|
| 121 | - } |
|
| 117 | + if ($bytes < 1) { |
|
| 118 | + throw new Error( |
|
| 119 | + 'Length must be greater than 0' |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * This if() block only runs if we managed to open a file handle |
|
| 125 | - * |
|
| 126 | - * It does not belong in an else {} block, because the above |
|
| 127 | - * if (empty($fp)) line is logic that should only be run once per |
|
| 128 | - * page load. |
|
| 129 | - */ |
|
| 130 | - if (is_resource($fp)) { |
|
| 131 | - /** |
|
| 132 | - * @var int |
|
| 133 | - */ |
|
| 134 | - $remaining = $bytes; |
|
| 123 | + /** |
|
| 124 | + * This if() block only runs if we managed to open a file handle |
|
| 125 | + * |
|
| 126 | + * It does not belong in an else {} block, because the above |
|
| 127 | + * if (empty($fp)) line is logic that should only be run once per |
|
| 128 | + * page load. |
|
| 129 | + */ |
|
| 130 | + if (is_resource($fp)) { |
|
| 131 | + /** |
|
| 132 | + * @var int |
|
| 133 | + */ |
|
| 134 | + $remaining = $bytes; |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * @var string|bool |
|
| 138 | - */ |
|
| 139 | - $buf = ''; |
|
| 136 | + /** |
|
| 137 | + * @var string|bool |
|
| 138 | + */ |
|
| 139 | + $buf = ''; |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * We use fread() in a loop to protect against partial reads |
|
| 143 | - */ |
|
| 144 | - do { |
|
| 145 | - /** |
|
| 146 | - * @var string|bool |
|
| 147 | - */ |
|
| 148 | - $read = fread($fp, $remaining); |
|
| 149 | - if (!is_string($read)) { |
|
| 150 | - /** |
|
| 151 | - * We cannot safely read from the file. Exit the |
|
| 152 | - * do-while loop and trigger the exception condition |
|
| 153 | - * |
|
| 154 | - * @var string|bool |
|
| 155 | - */ |
|
| 156 | - $buf = false; |
|
| 157 | - break; |
|
| 158 | - } |
|
| 159 | - /** |
|
| 160 | - * Decrease the number of bytes returned from remaining |
|
| 161 | - */ |
|
| 162 | - $remaining -= RandomCompat_strlen($read); |
|
| 163 | - /** |
|
| 164 | - * @var string $buf |
|
| 165 | - */ |
|
| 166 | - $buf .= $read; |
|
| 167 | - } while ($remaining > 0); |
|
| 141 | + /** |
|
| 142 | + * We use fread() in a loop to protect against partial reads |
|
| 143 | + */ |
|
| 144 | + do { |
|
| 145 | + /** |
|
| 146 | + * @var string|bool |
|
| 147 | + */ |
|
| 148 | + $read = fread($fp, $remaining); |
|
| 149 | + if (!is_string($read)) { |
|
| 150 | + /** |
|
| 151 | + * We cannot safely read from the file. Exit the |
|
| 152 | + * do-while loop and trigger the exception condition |
|
| 153 | + * |
|
| 154 | + * @var string|bool |
|
| 155 | + */ |
|
| 156 | + $buf = false; |
|
| 157 | + break; |
|
| 158 | + } |
|
| 159 | + /** |
|
| 160 | + * Decrease the number of bytes returned from remaining |
|
| 161 | + */ |
|
| 162 | + $remaining -= RandomCompat_strlen($read); |
|
| 163 | + /** |
|
| 164 | + * @var string $buf |
|
| 165 | + */ |
|
| 166 | + $buf .= $read; |
|
| 167 | + } while ($remaining > 0); |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Is our result valid? |
|
| 171 | - * @var string|bool $buf |
|
| 172 | - */ |
|
| 173 | - if (is_string($buf)) { |
|
| 174 | - if (RandomCompat_strlen($buf) === $bytes) { |
|
| 175 | - /** |
|
| 176 | - * Return our random entropy buffer here: |
|
| 177 | - */ |
|
| 178 | - return $buf; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - } |
|
| 169 | + /** |
|
| 170 | + * Is our result valid? |
|
| 171 | + * @var string|bool $buf |
|
| 172 | + */ |
|
| 173 | + if (is_string($buf)) { |
|
| 174 | + if (RandomCompat_strlen($buf) === $bytes) { |
|
| 175 | + /** |
|
| 176 | + * Return our random entropy buffer here: |
|
| 177 | + */ |
|
| 178 | + return $buf; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * If we reach here, PHP has failed us. |
|
| 185 | - */ |
|
| 186 | - throw new Exception( |
|
| 187 | - 'Error reading from source device' |
|
| 188 | - ); |
|
| 189 | - } |
|
| 183 | + /** |
|
| 184 | + * If we reach here, PHP has failed us. |
|
| 185 | + */ |
|
| 186 | + throw new Exception( |
|
| 187 | + 'Error reading from source device' |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | 190 | } |