Merge "HTMLForm: Show more options when incorrect displayFormat is set"
[lhc/web/wiklou.git] / includes / libs / CryptRand.php
index 10088f2..859d58b 100644 (file)
@@ -234,7 +234,6 @@ class CryptRand {
         * @return string Raw binary random data
         */
        public function generate( $bytes, $forceStrong = false ) {
-
                $bytes = floor( $bytes );
                static $buffer = '';
                if ( is_null( $this->strong ) ) {
@@ -242,6 +241,24 @@ class CryptRand {
                        $this->strong = true;
                }
 
+               if ( strlen( $buffer ) < $bytes ) {
+                       // If available make use of PHP 7's random_bytes
+                       // On Linux, getrandom syscall will be used if available.
+                       // On Windows CryptGenRandom will always be used
+                       // On other platforms, /dev/urandom will be used.
+                       // Avoids polyfills from before php 7.0
+                       // All error situations will throw Exceptions and or Errors
+                       if ( PHP_VERSION_ID >= 70000
+                               || ( defined( 'HHVM_VERSION_ID' ) && HHVM_VERSION_ID >= 31101 )
+                       ) {
+                               $rem = $bytes - strlen( $buffer );
+                               $buffer .= random_bytes( $rem );
+                       }
+                       if ( strlen( $buffer ) >= $bytes ) {
+                               $this->strong = true;
+                       }
+               }
+
                if ( strlen( $buffer ) < $bytes ) {
                        // If available make use of mcrypt_create_iv URANDOM source to generate randomness
                        // On unix-like systems this reads from /dev/urandom but does it without any buffering