Page MenuHomePhabricator

D7528.id16981.diff
No OneTemporary

D7528.id16981.diff

Index: src/filesystem/Filesystem.php
===================================================================
--- src/filesystem/Filesystem.php
+++ src/filesystem/Filesystem.php
@@ -393,21 +393,10 @@
*/
public static function readRandomBytes($number_of_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;
- $data = openssl_random_pseudo_bytes($number_of_bytes, $strong);
-
- if(strlen($data) != $number_of_bytes) {
- // fallback to /dev/urandom -- if available
+ if (function_exists('openssl_random_pseudo_bytes')) {
+ $strong = true;
+ $data = openssl_random_pseudo_bytes($number_of_bytes, $strong);
+ } else {
$urandom = @fopen('/dev/urandom', 'rb');
if (!$urandom) {
throw new FilesystemException(
@@ -416,13 +405,18 @@
}
$data = @fread($urandom, $number_of_bytes);
+ @fclose($urandom);
if (strlen($data) != $number_of_bytes) {
throw new FilesystemException(
'/dev/urandom',
'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');
}
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
Wed, Jul 16, 10:18 PM (14 h, 57 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
8313032
Default Alt Text
D7528.id16981.diff (2 KB)

Event Timeline