CryptRand: only use random_bytes on php 7 and HHVM
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Wed, 10 May 2017 09:47:49 +0000 (11:47 +0200)
committerDerk-Jan Hartman <hartman.wiki@gmail.com>
Fri, 26 May 2017 12:31:55 +0000 (14:31 +0200)
This avoids random_bytes polyfills used on older versions of PHP or
HHVM. HHVM has had random_bytes longer than PHP so treat it separately.

Bug: T143788
Change-Id: Ic9e511ccc043d50e0a9051cdfe069c27e4f4c339

includes/libs/CryptRand.php

index 0d3613a..4b4a913 100644 (file)
@@ -247,8 +247,11 @@ class CryptRand {
                        // On Linux, getrandom syscall will be used if available.
                        // On Windows CryptGenRandom will always be used
                        // On other platforms, /dev/urandom will be used.
                        // 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
                        // All error situations will throw Exceptions and or Errors
-                       if ( function_exists( 'random_bytes' ) ) {
+                       if ( PHP_VERSION_ID >= 70000
+                               || ( defined( 'HHVM_VERSION_ID' ) && HHVM_VERSION_ID >= 31101 )
+                       ) {
                                $rem = $bytes - strlen( $buffer );
                                $buffer .= random_bytes( $rem );
                        }
                                $rem = $bytes - strlen( $buffer );
                                $buffer .= random_bytes( $rem );
                        }