Index: src/filesystem/Filesystem.php =================================================================== --- src/filesystem/Filesystem.php +++ src/filesystem/Filesystem.php @@ -393,37 +393,32 @@ */ public static function readRandomBytes($number_of_bytes) { - if (phutil_is_windows()) { - 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.'); - } + if (function_exists('openssl_random_pseudo_bytes')) { $strong = true; - return openssl_random_pseudo_bytes($number_of_bytes, $strong); - } + $data = openssl_random_pseudo_bytes($number_of_bytes, $strong); + } else { + $urandom = @fopen('/dev/urandom', 'rb'); + if (!$urandom) { + throw new FilesystemException( + '/dev/urandom', + 'Failed to open /dev/urandom for reading!'); + } - $urandom = @fopen('/dev/urandom', 'rb'); - if (!$urandom) { - throw new FilesystemException( - '/dev/urandom', - 'Failed to open /dev/urandom for reading!'); + $data = @fread($urandom, $number_of_bytes); + @fclose($urandom); + if (strlen($data) != $number_of_bytes) { + throw new FilesystemException( + '/dev/urandom', + 'Failed to read random bytes!'); + } } - $data = @fread($urandom, $number_of_bytes); if (strlen($data) != $number_of_bytes) { - throw new FilesystemException( - '/dev/urandom', - 'Failed to read random bytes!'); + throw new Exception( + 'Filesystem::readRandomBytes() requires at least PHP 5.3 or + /dev/urandom'); } - @fclose($urandom); - return $data; } Index: src/filesystem/__tests__/FilesystemTestCase.php =================================================================== --- src/filesystem/__tests__/FilesystemTestCase.php +++ src/filesystem/__tests__/FilesystemTestCase.php @@ -60,4 +60,10 @@ $this->assertEqual(true, ($f != $g)); } + public function testReadRandomBytes() { + $number_of_bytes = 1024; + $data = Filesystem::readRandomBytes($number_of_bytes); + $this->assertEqual(true, (strlen($data) == $number_of_bytes)); + } + }