mirror of
https://github.com/phpseclib/phpseclib.git
synced 2024-12-28 12:10:59 +00:00
Merge pull request #794 from cweagans/php7-random-bytes-2.0
Use random_bytes() when appropriate (2.0) * cweagans/php7-random-bytes-2.0: Remove duplicate allow_failures section Test on PHP 7 & allow failures Coding standards fix Switch from EngineException to Error Remove string assignment Use random_bytes() when appropriate.
This commit is contained in:
commit
9691924b31
@ -53,6 +53,18 @@ class Random
|
||||
*/
|
||||
public static function string($length)
|
||||
{
|
||||
if (version_compare(PHP_VERSION, '7.0.0', '>=')) {
|
||||
try {
|
||||
return random_bytes($length);
|
||||
} catch (\Error $e) {
|
||||
// If a sufficient source of randomness is unavailable, random_bytes() will emit a warning.
|
||||
// We don't actually need to do anything here. The string() method should just continue
|
||||
// as normal. Note, however, that if we don't have a sufficient source of randomness for
|
||||
// random_bytes(), most of the other calls here will fail too, so we'll end up using
|
||||
// the PHP implementation.
|
||||
}
|
||||
}
|
||||
|
||||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
||||
// method 1. prior to PHP 5.3 this would call rand() on windows hence the function_exists('class_alias') call.
|
||||
// ie. class_alias is a function that was introduced in PHP 5.3
|
||||
|
Loading…
Reference in New Issue
Block a user