3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('random_bytes')) { /** * PHP 5.2.0 - 5.6.x way to implement random_bytes() * * In order of preference: * 1. mcrypt_create_iv($bytes, MCRYPT_CREATE_IV) * 2. fread() /dev/arandom if available * 3. fread() /dev/urandom if available * 4. COM('CAPICOM.Utilities.1')->GetRandom() * 5. openssl_random_pseudo_bytes() */ if (function_exists('mcrypt_create_iv') && version_compare(PHP_VERSION, '5.3.7') >= 0) { /** * Powered by ext/mcrypt * * @param int $bytes * @return string */ function random_bytes($bytes) { if (!is_int($bytes)) { throw new Exception( 'Length must be an integer' ); } if ($bytes < 1) { throw new Exception( 'Length must be greater than 0' ); } // See PHP bug #55169 for why 5.3.7 is required $buf = mcrypt_create_iv($bytes, MCRYPT_DEV_URANDOM); if ($buf !== false) { if (RandomCompat_strlen($buf) === $bytes) { return $buf; } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (!ini_get('open_basedir') && (is_readable('/dev/arandom') || is_readable('/dev/urandom'))) { /** * Use /dev/arandom or /dev/urandom for random numbers * * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers * * @param int $bytes * @return string */ function random_bytes($bytes) { static $fp = null; if ($fp === null) { if (is_readable('/dev/arandom')) { $fp = fopen('/dev/arandom', 'rb'); } else { $fp = fopen('/dev/urandom', 'rb'); } } if ($fp !== false) { $streamset = stream_set_read_buffer($fp, 0); if ($streamset === 0) { $remaining = $bytes; $buf = ''; do { $read = fread($fp, $remaining); if ($read === false) { // We cannot safely read from urandom. $buf = false; break; } // Decrease the number of bytes returned from remaining $remaining -= RandomCompat_strlen($read); $buf .= $read; } while ($remaining > 0); if ($buf !== false) { if (RandomCompat_strlen($buf) === $bytes) { /** * Return our random entropy buffer here: */ return $buf; } } } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (extension_loaded('com_dotnet')) { /** * Windows with PHP < 5.3.0 will not have the function * openssl_random_pseudo_bytes() available, so let's use * CAPICOM to work around this deficiency. * * @param int $bytes * @return string */ function random_bytes($bytes) { $buf = ''; $util = new COM('CAPICOM.Utilities.1'); $execCount = 0; /** * Let's not let it loop forever. If we run N times and fail to * get N bytes of random data, then CAPICOM has failed us. */ do { $buf .= base64_decode($util->GetRandom($bytes, 0)); if (RandomCompat_strlen($buf) >= $bytes) { return RandomCompat_substr($buf, 0, $bytes); } ++$execCount; } while ($execCount < $bytes); /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } elseif (function_exists('openssl_random_pseudo_bytes')) { /** * Since openssl_random_pseudo_bytes() uses openssl's * RAND_pseudo_bytes() API, which has been marked as deprecated by the * OpenSSL team, this is our last resort before failure. * * @ref https://www.openssl.org/docs/crypto/RAND_bytes.html * * @param int $bytes * @return string */ function random_bytes($bytes) { $secure = true; $buf = openssl_random_pseudo_bytes($bytes, $secure); if ($buf !== false && $secure) { if (RandomCompat_strlen($buf) === $bytes) { return $buf; } } /** * If we reach here, PHP has failed us. */ throw new Exception( 'PHP failed to generate random data.' ); } } else { /** * We don't have any more options, so let's throw an exception right now */ throw new Exception( 'There is no suitable CSPRNG installed on your system' ); } } var_dump( implode('-', [ bin2hex(random_bytes(4)), bin2hex(random_bytes(2)), bin2hex((random_bytes(1) & 0x0F) | 0x40) . bin2hex(random_bytes(1)), bin2hex((random_bytes(1) & 0x3F) | 0x80) . bin2hex(random_bytes(1)), bin2hex(random_bytes(12)) ]) );
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 51
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 23, Position 2 = 32
Branch analysis from position: 23
2 jumps found. (Code = 47) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 35
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 47
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
Branch analysis from position: 47
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 32
Branch analysis from position: 15
Branch analysis from position: 51
filename:       /in/i0k74
function name:  (null)
number of ops:  112
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'random_bytes'
          2        DO_ICALL                                         $0      
          3        BOOL_NOT                                         ~1      $0
          4      > JMPZ                                                     ~1, ->51
   14     5    >   INIT_FCALL                                               'function_exists'
          6        SEND_VAL                                                 'mcrypt_create_iv'
          7        DO_ICALL                                         $2      
          8      > JMPZ_EX                                          ~3      $2, ->15
          9    >   INIT_FCALL                                               'version_compare'
         10        SEND_VAL                                                 '8.0.0'
         11        SEND_VAL                                                 '5.3.7'
         12        DO_ICALL                                         $4      
         13        IS_SMALLER_OR_EQUAL                              ~5      0, $4
         14        BOOL                                             ~3      ~5
         15    > > JMPZ                                                     ~3, ->18
   21    16    >   DECLARE_FUNCTION                                         'random_bytes'
   46    17      > JMP                                                      ->51
   47    18    >   INIT_FCALL                                               'ini_get'
         19        SEND_VAL                                                 'open_basedir'
         20        DO_ICALL                                         $6      
         21        BOOL_NOT                                         ~7      $6
         22      > JMPZ_EX                                          ~7      ~7, ->32
         23    >   INIT_FCALL                                               'is_readable'
         24        SEND_VAL                                                 '%2Fdev%2Farandom'
         25        DO_ICALL                                         $8      
         26      > JMPNZ_EX                                         ~9      $8, ->31
         27    >   INIT_FCALL                                               'is_readable'
         28        SEND_VAL                                                 '%2Fdev%2Furandom'
         29        DO_ICALL                                         $10     
         30        BOOL                                             ~9      $10
         31    >   BOOL                                             ~7      ~9
         32    > > JMPZ                                                     ~7, ->35
   56    33    >   DECLARE_FUNCTION                                         'random_bytes'
   98    34      > JMP                                                      ->51
   99    35    >   INIT_FCALL                                               'extension_loaded'
         36        SEND_VAL                                                 'com_dotnet'
         37        DO_ICALL                                         $11     
         38      > JMPZ                                                     $11, ->41
  108    39    >   DECLARE_FUNCTION                                         'random_bytes'
  130    40      > JMP                                                      ->51
  131    41    >   INIT_FCALL                                               'function_exists'
         42        SEND_VAL                                                 'openssl_random_pseudo_bytes'
         43        DO_ICALL                                         $12     
         44      > JMPZ                                                     $12, ->47
  142    45    >   DECLARE_FUNCTION                                         'random_bytes'
  157    46      > JMP                                                      ->51
  162    47    >   NEW                                              $13     'Exception'
  163    48        SEND_VAL_EX                                              'There+is+no+suitable+CSPRNG+installed+on+your+system'
         49        DO_FCALL                                      0          
         50      > THROW                                         0          $13
  168    51    >   INIT_FCALL                                               'var_dump'
  169    52        INIT_FCALL                                               'implode'
         53        SEND_VAL                                                 '-'
  170    54        INIT_FCALL                                               'bin2hex'
         55        INIT_FCALL                                               'random_bytes'
         56        SEND_VAL                                                 4
         57        DO_ICALL                                         $15     
         58        SEND_VAR                                                 $15
         59        DO_ICALL                                         $16     
         60        INIT_ARRAY                                       ~17     $16
  171    61        INIT_FCALL                                               'bin2hex'
         62        INIT_FCALL                                               'random_bytes'
         63        SEND_VAL                                                 2
         64        DO_ICALL                                         $18     
         65        SEND_VAR                                                 $18
         66        DO_ICALL                                         $19     
         67        ADD_ARRAY_ELEMENT                                ~17     $19
  172    68        INIT_FCALL                                               'bin2hex'
         69        INIT_FCALL                                               'random_bytes'
         70        SEND_VAL                                                 1
         71        DO_ICALL                                         $20     
         72        BW_AND                                           ~21     $20, 15
         73        BW_OR                                            ~22     ~21, 64
         74        SEND_VAL                                                 ~22
         75        DO_ICALL                                         $23     
         76        INIT_FCALL                                               'bin2hex'
         77        INIT_FCALL                                               'random_bytes'
         78        SEND_VAL                                                 1
         79        DO_ICALL                                         $24     
         80        SEND_VAR                                                 $24
         81        DO_ICALL                                         $25     
         82        CONCAT                                           ~26     $23, $25
         83        ADD_ARRAY_ELEMENT                                ~17     ~26
  173    84        INIT_FCALL                                               'bin2hex'
         85        INIT_FCALL                                               'random_bytes'
         86        SEND_VAL                                                 1
         87        DO_ICALL                                         $27     
         88        BW_AND                                           ~28     $27, 63
         89        BW_OR                                            ~29     ~28, 128
         90        SEND_VAL                                                 ~29
         91        DO_ICALL                                         $30     
         92        INIT_FCALL                                               'bin2hex'
         93        INIT_FCALL                                               'random_bytes'
         94        SEND_VAL                                                 1
         95        DO_ICALL                                         $31     
         96        SEND_VAR                                                 $31
         97        DO_ICALL                                         $32     
         98        CONCAT                                           ~33     $30, $32
         99        ADD_ARRAY_ELEMENT                                ~17     ~33
  174   100        INIT_FCALL                                               'bin2hex'
        101        INIT_FCALL                                               'random_bytes'
        102        SEND_VAL                                                 12
        103        DO_ICALL                                         $34     
        104        SEND_VAR                                                 $34
        105        DO_ICALL                                         $35     
        106        ADD_ARRAY_ELEMENT                                ~17     $35
        107        SEND_VAL                                                 ~17
        108        DO_ICALL                                         $36     
        109        SEND_VAR                                                 $36
        110        DO_ICALL                                                 
  176   111      > RETURN                                                   1

Function %00random_bytes%2Fin%2Fi0k74%3A21%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 28
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
filename:       /in/i0k74
function name:  random_bytes
number of ops:  33
compiled vars:  !0 = $bytes, !1 = $buf
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        TYPE_CHECK                                   16  ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->8
   24     4    >   NEW                                              $4      'Exception'
   25     5        SEND_VAL_EX                                              'Length+must+be+an+integer'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $4
   28     8    >   IS_SMALLER                                               !0, 1
          9      > JMPZ                                                     ~6, ->14
   29    10    >   NEW                                              $7      'Exception'
   30    11        SEND_VAL_EX                                              'Length+must+be+greater+than+0'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $7
   34    14    >   INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
         15        SEND_VAR_EX                                              !0
         16        FETCH_CONSTANT                                   ~9      'MCRYPT_DEV_URANDOM'
         17        SEND_VAL_EX                                              ~9
         18        DO_FCALL                                      0  $10     
         19        ASSIGN                                                   !1, $10
   35    20        TYPE_CHECK                                  1018          !1
         21      > JMPZ                                                     ~12, ->28
   36    22    >   INIT_FCALL_BY_NAME                                       'RandomCompat_strlen'
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0  $13     
         25        IS_IDENTICAL                                             !0, $13
         26      > JMPZ                                                     ~14, ->28
   37    27    > > RETURN                                                   !1
   43    28    >   NEW                                              $15     'Exception'
   44    29        SEND_VAL_EX                                              'PHP+failed+to+generate+random+data.'
         30        DO_FCALL                                      0          
         31      > THROW                                         0          $15
   46    32*     > RETURN                                                   null

End of function %00random_bytes%2Fin%2Fi0k74%3A21%240

Function %00random_bytes%2Fin%2Fi0k74%3A56%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 54
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 54
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 39
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 54
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 54
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 54
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 46, Position 2 = 30
Branch analysis from position: 46
Branch analysis from position: 30
Branch analysis from position: 54
Branch analysis from position: 54
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 54
Branch analysis from position: 21
Branch analysis from position: 54
Branch analysis from position: 19
filename:       /in/i0k74
function name:  random_bytes
number of ops:  59
compiled vars:  !0 = $bytes, !1 = $fp, !2 = $streamset, !3 = $remaining, !4 = $buf, !5 = $read
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   58     1        BIND_STATIC                                              !1
   59     2        TYPE_CHECK                                    2          !1
          3      > JMPZ                                                     ~6, ->19
   60     4    >   INIT_FCALL                                               'is_readable'
          5        SEND_VAL                                                 '%2Fdev%2Farandom'
          6        DO_ICALL                                         $7      
          7      > JMPZ                                                     $7, ->14
   61     8    >   INIT_FCALL                                               'fopen'
          9        SEND_VAL                                                 '%2Fdev%2Farandom'
         10        SEND_VAL                                                 'rb'
         11        DO_ICALL                                         $8      
         12        ASSIGN                                                   !1, $8
         13      > JMP                                                      ->19
   63    14    >   INIT_FCALL                                               'fopen'
         15        SEND_VAL                                                 '%2Fdev%2Furandom'
         16        SEND_VAL                                                 'rb'
         17        DO_ICALL                                         $10     
         18        ASSIGN                                                   !1, $10
   66    19    >   TYPE_CHECK                                  1018          !1
         20      > JMPZ                                                     ~12, ->54
   67    21    >   INIT_FCALL                                               'stream_set_read_buffer'
         22        SEND_VAR                                                 !1
         23        SEND_VAL                                                 0
         24        DO_ICALL                                         $13     
         25        ASSIGN                                                   !2, $13
   68    26        IS_IDENTICAL                                             !2, 0
         27      > JMPZ                                                     ~15, ->54
   69    28    >   ASSIGN                                                   !3, !0
   70    29        ASSIGN                                                   !4, ''
   72    30    >   INIT_FCALL                                               'fread'
         31        SEND_VAR                                                 !1
         32        SEND_VAR                                                 !3
         33        DO_ICALL                                         $18     
         34        ASSIGN                                                   !5, $18
   73    35        TYPE_CHECK                                    4          !5
         36      > JMPZ                                                     ~20, ->39
   75    37    >   ASSIGN                                                   !4, <false>
   76    38      > JMP                                                      ->46
   79    39    >   INIT_FCALL_BY_NAME                                       'RandomCompat_strlen'
         40        SEND_VAR_EX                                              !5
         41        DO_FCALL                                      0  $22     
         42        ASSIGN_OP                                     2          !3, $22
   80    43        ASSIGN_OP                                     8          !4, !5
   81    44        IS_SMALLER                                               0, !3
         45      > JMPNZ                                                    ~25, ->30
   82    46    >   TYPE_CHECK                                  1018          !4
         47      > JMPZ                                                     ~26, ->54
   83    48    >   INIT_FCALL_BY_NAME                                       'RandomCompat_strlen'
         49        SEND_VAR_EX                                              !4
         50        DO_FCALL                                      0  $27     
         51        IS_IDENTICAL                                             !0, $27
         52      > JMPZ                                                     ~28, ->54
   87    53    > > RETURN                                                   !4
   95    54    >   NEW                                              $29     'Exception'
   96    55        SEND_VAL_EX                                              'PHP+failed+to+generate+random+data.'
         56        DO_FCALL                                      0          
         57      > THROW                                         0          $29
   98    58*     > RETURN                                                   null

End of function %00random_bytes%2Fin%2Fi0k74%3A56%241

Function %00random_bytes%2Fin%2Fi0k74%3A108%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 7
Branch analysis from position: 29
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 7
filename:       /in/i0k74
function name:  random_bytes
number of ops:  34
compiled vars:  !0 = $bytes, !1 = $buf, !2 = $util, !3 = $execCount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   RECV                                             !0      
  110     1        ASSIGN                                                   !1, ''
  111     2        NEW                                              $5      'COM'
          3        SEND_VAL_EX                                              'CAPICOM.Utilities.1'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $5
  112     6        ASSIGN                                                   !3, 0
  118     7    >   INIT_FCALL                                               'base64_decode'
          8        INIT_METHOD_CALL                                         !2, 'GetRandom'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAL_EX                                              0
         11        DO_FCALL                                      0  $9      
         12        SEND_VAR                                                 $9
         13        DO_ICALL                                         $10     
         14        ASSIGN_OP                                     8          !1, $10
  119    15        INIT_FCALL_BY_NAME                                       'RandomCompat_strlen'
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $12     
         18        IS_SMALLER_OR_EQUAL                                      !0, $12
         19      > JMPZ                                                     ~13, ->26
  120    20    >   INIT_FCALL_BY_NAME                                       'RandomCompat_substr'
         21        SEND_VAR_EX                                              !1
         22        SEND_VAL_EX                                              0
         23        SEND_VAR_EX                                              !0
         24        DO_FCALL                                      0  $14     
         25      > RETURN                                                   $14
  122    26    >   PRE_INC                                                  !3
  123    27        IS_SMALLER                                               !3, !0
         28      > JMPNZ                                                    ~16, ->7
  127    29    >   NEW                                              $17     'Exception'
  128    30        SEND_VAL_EX                                              'PHP+failed+to+generate+random+data.'
         31        DO_FCALL                                      0          
         32      > THROW                                         0          $17
  130    33*     > RETURN                                                   null

End of function %00random_bytes%2Fin%2Fi0k74%3A108%242

Function %00random_bytes%2Fin%2Fi0k74%3A142%243:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 17
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 10
filename:       /in/i0k74
function name:  random_bytes
number of ops:  22
compiled vars:  !0 = $bytes, !1 = $secure, !2 = $buf
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  142     0  E >   RECV                                             !0      
  144     1        ASSIGN                                                   !1, <true>
  145     2        INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !2, $4
  146     7        TYPE_CHECK                                  1018  ~6      !2
          8      > JMPZ_EX                                          ~6      ~6, ->10
          9    >   BOOL                                             ~6      !1
         10    > > JMPZ                                                     ~6, ->17
  147    11    >   INIT_FCALL_BY_NAME                                       'RandomCompat_strlen'
         12        SEND_VAR_EX                                              !2
         13        DO_FCALL                                      0  $7      
         14        IS_IDENTICAL                                             !0, $7
         15      > JMPZ                                                     ~8, ->17
  148    16    > > RETURN                                                   !2
  154    17    >   NEW                                              $9      'Exception'
  155    18        SEND_VAL_EX                                              'PHP+failed+to+generate+random+data.'
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $9
  157    21*     > RETURN                                                   null

End of function %00random_bytes%2Fin%2Fi0k74%3A142%243

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.58 ms | 1420 KiB | 39 Q