3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DeterministicRandom { protected $counter; public function __construct(string $seed = '', int $counter = 0) { $this->seed('set', $seed); $this->counter = 0; } private function seed(string $action = 'get', string $data = '') { static $seed = null; if ($action === 'set') { $seed = $data; } elseif ($action === 'get') { return $data; } else { throw new \Error( 'Unknown action' ); } } public function getBytes(int $numBytes): string { return \openssl_encrypt( \str_repeat("\0", $numBytes), 'aes-128-ctr', $this->seed('get'), OPENSSL_RAW_DATA, $this->getNonce($numBytes) ); } public function getInt(int $min, int $max): int { /** * @todo */ return 0; } protected function getNonce(int $increment = 0): string { $nonce = ''; $ctr = $this->counter; while ($ctr > 0) { $nonce = \chr($ctr & 0xFF) . $nonce; $ctr >>= 8; } $this->counter += $increment; return \str_pad($nonce, 16, "\0", STR_PAD_LEFT); } } $seed = str_repeat("\x80", 16); $rnd1 = new DeterministicRandom($seed); $rnd2 = new DeterministicRandom($seed); $out1 = $rnd1->getBytes(16); $out2 = $rnd1->getBytes(16); $out3 = $rnd2->getBytes(32); var_dump([ bin2hex($out1), bin2hex($out2), bin2hex($out3) ]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Db90F
function name:  (null)
number of ops:  41
compiled vars:  !0 = $seed, !1 = $rnd1, !2 = $rnd2, !3 = $out1, !4 = $out2, !5 = $out3
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   INIT_FCALL                                               'str_repeat'
          1        SEND_VAL                                                 '%80'
          2        SEND_VAL                                                 16
          3        DO_ICALL                                         $6      
          4        ASSIGN                                                   !0, $6
   61     5        NEW                                              $8      'DeterministicRandom'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $8
   62     9        NEW                                              $11     'DeterministicRandom'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $11
   64    13        INIT_METHOD_CALL                                         !1, 'getBytes'
         14        SEND_VAL_EX                                              16
         15        DO_FCALL                                      0  $14     
         16        ASSIGN                                                   !3, $14
   65    17        INIT_METHOD_CALL                                         !1, 'getBytes'
         18        SEND_VAL_EX                                              16
         19        DO_FCALL                                      0  $16     
         20        ASSIGN                                                   !4, $16
   66    21        INIT_METHOD_CALL                                         !2, 'getBytes'
         22        SEND_VAL_EX                                              32
         23        DO_FCALL                                      0  $18     
         24        ASSIGN                                                   !5, $18
   68    25        INIT_FCALL                                               'var_dump'
   69    26        INIT_FCALL                                               'bin2hex'
         27        SEND_VAR                                                 !3
         28        DO_ICALL                                         $20     
         29        INIT_ARRAY                                       ~21     $20
   70    30        INIT_FCALL                                               'bin2hex'
         31        SEND_VAR                                                 !4
         32        DO_ICALL                                         $22     
         33        ADD_ARRAY_ELEMENT                                ~21     $22
   71    34        INIT_FCALL                                               'bin2hex'
         35        SEND_VAR                                                 !5
         36        DO_ICALL                                         $23     
         37        ADD_ARRAY_ELEMENT                                ~21     $23
         38        SEND_VAL                                                 ~21
         39        DO_ICALL                                                 
   72    40      > RETURN                                                   1

Class DeterministicRandom:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Db90F
function name:  __construct
number of ops:  9
compiled vars:  !0 = $seed, !1 = $counter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV_INIT                                        !0      ''
          1        RECV_INIT                                        !1      0
    9     2        INIT_METHOD_CALL                                         'seed'
          3        SEND_VAL_EX                                              'set'
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0          
   10     6        ASSIGN_OBJ                                               'counter'
          7        OP_DATA                                                  0
   11     8      > RETURN                                                   null

End of function __construct

Function seed:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/Db90F
function name:  seed
number of ops:  16
compiled vars:  !0 = $action, !1 = $data, !2 = $seed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV_INIT                                        !0      'get'
          1        RECV_INIT                                        !1      ''
   15     2        BIND_STATIC                                              !2
   16     3        IS_IDENTICAL                                             !0, 'set'
          4      > JMPZ                                                     ~3, ->7
   17     5    >   ASSIGN                                                   !2, !1
          6      > JMP                                                      ->15
   18     7    >   IS_IDENTICAL                                             !0, 'get'
          8      > JMPZ                                                     ~5, ->11
   19     9    > > RETURN                                                   !1
         10*       JMP                                                      ->15
   21    11    >   NEW                                              $6      'Error'
   22    12        SEND_VAL_EX                                              'Unknown+action'
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $6
   25    15    > > RETURN                                                   null

End of function seed

Function getbytes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Db90F
function name:  getBytes
number of ops:  23
compiled vars:  !0 = $numBytes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        INIT_FCALL_BY_NAME                                       'openssl_encrypt'
   30     2        INIT_FCALL                                               'str_repeat'
          3        SEND_VAL                                                 '%00'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6        SEND_VAR_NO_REF_EX                                       $1
   31     7        SEND_VAL_EX                                              'aes-128-ctr'
   32     8        INIT_METHOD_CALL                                         'seed'
          9        SEND_VAL                                                 'get'
         10        DO_FCALL                                      0  $2      
         11        SEND_VAR_NO_REF_EX                                       $2
   33    12        FETCH_CONSTANT                                   ~3      'OPENSSL_RAW_DATA'
         13        SEND_VAL_EX                                              ~3
   34    14        INIT_METHOD_CALL                                         'getNonce'
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0  $4      
         17        SEND_VAR_NO_REF_EX                                       $4
         18        DO_FCALL                                      0  $5      
         19        VERIFY_RETURN_TYPE                                       $5
         20      > RETURN                                                   $5
   36    21*       VERIFY_RETURN_TYPE                                       
         22*     > RETURN                                                   null

End of function getbytes

Function getint:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Db90F
function name:  getInt
number of ops:  5
compiled vars:  !0 = $min, !1 = $max
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   43     2      > RETURN                                                   0
   44     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getint

Function getnonce:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 5
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 5
Branch analysis from position: 14
Branch analysis from position: 5
filename:       /in/Db90F
function name:  getNonce
number of ops:  26
compiled vars:  !0 = $increment, !1 = $nonce, !2 = $ctr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV_INIT                                        !0      0
   48     1        ASSIGN                                                   !1, ''
   49     2        FETCH_OBJ_R                                      ~4      'counter'
          3        ASSIGN                                                   !2, ~4
   50     4      > JMP                                                      ->12
   51     5    >   INIT_FCALL                                               'chr'
          6        BW_AND                                           ~6      !2, 255
          7        SEND_VAL                                                 ~6
          8        DO_ICALL                                         $7      
          9        CONCAT                                           ~8      $7, !1
         10        ASSIGN                                                   !1, ~8
   52    11        ASSIGN_OP                                     7          !2, 8
   50    12    >   IS_SMALLER                                               0, !2
         13      > JMPNZ                                                    ~11, ->5
   54    14    >   ASSIGN_OBJ_OP                                 1          'counter'
         15        OP_DATA                                                  !0
   55    16        INIT_FCALL                                               'str_pad'
         17        SEND_VAR                                                 !1
         18        SEND_VAL                                                 16
         19        SEND_VAL                                                 '%00'
         20        SEND_VAL                                                 0
         21        DO_ICALL                                         $13     
         22        VERIFY_RETURN_TYPE                                       $13
         23      > RETURN                                                   $13
   56    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function getnonce

End of class DeterministicRandom.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
197.59 ms | 1408 KiB | 23 Q