Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/Filesystem.php
Show First 20 Lines • Show All 383 Lines • ▼ Show 20 Lines | /* -( Files )-------------------------------------------------------------- */ | ||||
* Read random bytes from /dev/urandom or equivalent. See also | * Read random bytes from /dev/urandom or equivalent. See also | ||||
* @{method:readRandomCharacters}. | * @{method:readRandomCharacters}. | ||||
* | * | ||||
* @param int Number of bytes to read. | * @param int Number of bytes to read. | ||||
* @return string Random bytestring of the provided length. | * @return string Random bytestring of the provided length. | ||||
* | * | ||||
* @task file | * @task file | ||||
* | * | ||||
* @phutil-external-symbol class COM | * @phutil-external-symbol class COM | ||||
epriestley: We could remove this, since we no longer use it. | |||||
*/ | */ | ||||
public static function readRandomBytes($number_of_bytes) { | public static function readRandomBytes($number_of_bytes) { | ||||
if (!function_exists('openssl_random_pseudo_bytes')) { | if (function_exists('openssl_random_pseudo_bytes')) { | ||||
if (version_compare(PHP_VERSION, '5.3.0') < 0) { | |||||
throw new Exception( | |||||
'Filesystem::readRandomBytes() requires at least PHP 5.3 under '. | |||||
'Windows.'); | |||||
} | |||||
throw new Exception( | |||||
'Filesystem::readRandomBytes() requires OpenSSL extension under '. | |||||
'Windows.'); | |||||
} | |||||
$strong = true; | $strong = true; | ||||
$data = openssl_random_pseudo_bytes($number_of_bytes, $strong); | $data = openssl_random_pseudo_bytes($number_of_bytes, $strong); | ||||
} else { | |||||
if(strlen($data) != $number_of_bytes) { | |||||
// fallback to /dev/urandom -- if available | |||||
$urandom = @fopen('/dev/urandom', 'rb'); | $urandom = @fopen('/dev/urandom', 'rb'); | ||||
if (!$urandom) { | if (!$urandom) { | ||||
throw new FilesystemException( | throw new FilesystemException( | ||||
'/dev/urandom', | '/dev/urandom', | ||||
'Failed to open /dev/urandom for reading!'); | 'Failed to open /dev/urandom for reading!'); | ||||
} | } | ||||
$data = @fread($urandom, $number_of_bytes); | $data = @fread($urandom, $number_of_bytes); | ||||
@fclose($urandom); | |||||
if (strlen($data) != $number_of_bytes) { | if (strlen($data) != $number_of_bytes) { | ||||
epriestleyUnsubmitted Not Done Inline ActionsIf the caller requests readRandomBytes(0), and the fread fails, the strlen() of false is 0, so this check will pass, and then we'll return false instead of '' (empty string). I think this is fine since readRandomBytes(0) is pathological, I'm just proving that I'm paying attention. The old implementation had the same problem anyway. epriestley: If the caller requests `readRandomBytes(0)`, and the `fread` fails, the `strlen()` of `false`… | |||||
throw new FilesystemException( | throw new FilesystemException( | ||||
'/dev/urandom', | '/dev/urandom', | ||||
'Failed to read random bytes!'); | 'Failed to read random bytes!'); | ||||
} | } | ||||
} | |||||
@fclose($urandom); | if (strlen($data) != $number_of_bytes) { | ||||
throw new Exception( | |||||
'Filesystem::readRandomBytes() requires at least PHP 5.3 or | |||||
/dev/urandom'); | |||||
epriestleyUnsubmitted Not Done Inline ActionsThis string -- and thus the error message -- includes a huge amount of whitespace and a newline. Better as: 'a b c '. 'd' epriestley: This string -- and thus the error message -- includes a huge amount of whitespace and a newline. | |||||
} | } | ||||
return $data; | return $data; | ||||
} | } | ||||
/** | /** | ||||
* Read random alphanumeric characters from /dev/urandom or equivalent. This | * Read random alphanumeric characters from /dev/urandom or equivalent. This | ||||
▲ Show 20 Lines • Show All 588 Lines • Show Last 20 Lines |
We could remove this, since we no longer use it.