Page MenuHomePhabricator

D7528.diff

diff --git a/src/filesystem/Filesystem.php b/src/filesystem/Filesystem.php
--- a/src/filesystem/Filesystem.php
+++ b/src/filesystem/Filesystem.php
@@ -388,42 +388,35 @@
* @return string Random bytestring of the provided length.
*
* @task file
- *
- * @phutil-external-symbol class COM
*/
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;
}
diff --git a/src/filesystem/__tests__/FilesystemTestCase.php b/src/filesystem/__tests__/FilesystemTestCase.php
--- a/src/filesystem/__tests__/FilesystemTestCase.php
+++ b/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));
+ }
+
}

File Metadata

Mime Type
text/x-diff
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/jq/b4/mls4rdxd5azdk6rd
Default Alt Text
D7528.diff (2 KB)

Event Timeline