Page MenuHomePhabricator

D7528.id16983.diff
No OneTemporary

D7528.id16983.diff

Index: src/filesystem/Filesystem.php
===================================================================
--- src/filesystem/Filesystem.php
+++ 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;
}
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));
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 22, 5:07 PM (1 h, 2 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6919089
Default Alt Text
D7528.id16983.diff (2 KB)

Event Timeline