1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-12-28 08:20:56 +01:00

[Wilds] Use "random_bytes()" if it is available (after PHP7)

Summary:
Ref T13209. In PHP7 and newer, the function `random_bytes()` gives us simple access to cryptographic randomness across platforms. Prefer it if it's available.

Notably, the other sources often aren't available on Windows, and particularly aren't available on a clean/default install with modern software versions. So the major motivation is to make things work better out-of-the-box on Windows.

Test Plan: On Windows, saw Filesystem unit tests which call `readRandomBytes()` now pass.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13209

Differential Revision: https://secure.phabricator.com/D19726
This commit is contained in:
epriestley 2018-10-02 10:44:35 -07:00
parent 9ac0b69798
commit 5a4c489916

View file

@ -421,6 +421,12 @@ final class Filesystem extends Phobject {
throw new Exception(pht('You must generate at least 1 byte of entropy.'));
}
// Under PHP7, the "random_bytes()" function provides a simpler and more
// portable way to accomplish what the rest of this function accomplishes.
if (function_exists('random_bytes')) {
return random_bytes($number_of_bytes);
}
// Try to use `openssl_random_pseudo_bytes()` if it's available. This source
// is the most widely available source, and works on Windows/Linux/OSX/etc.
@ -481,9 +487,8 @@ final class Filesystem extends Phobject {
pht(
'%s requires the PHP OpenSSL extension to be installed and enabled '.
'to access an entropy source. On Windows, this extension is usually '.
'installed but not enabled by default. Enable it in your "s".',
__METHOD__.'()',
'php.ini'));
'installed but not enabled by default. Enable it in your "php.ini".',
__METHOD__.'()'));
}
throw new Exception(