3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TokenGenerator { const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; public function generateToken($length = 16) { $alphabetSize = strlen(self::ALPHABET); $token = ''; while (strlen($token) < $length) { $randomBytes = $this->generateRandomBytes(32); $offset = abs(hexdec(bin2hex(substr($randomBytes, 0, 4)))) % $alphabetSize; $token .= substr(self::ALPHABET, $offset, 1); } return $token; } private function generateRandomBytes($length) { $bytes = ''; // pre-fill with weak bytes while (strlen($bytes) < $length) { $bytes .= chr(mt_rand(0, 255)); } // merge with mcrypt iv if (function_exists('mcrypt_create_iv')) { $bytes = hash_hmac('sha256', $bytes, mcrypt_create_iv($length, MCRYPT_DEV_URANDOM), true); } // merge with openssl bytes if (function_exists('openssl_random_pseudo_bytes')) { $bytes = hash_hmac('sha256', $bytes, openssl_random_pseudo_bytes($length), true); } // merge with urandom if (file_exists('/dev/urandom') && is_readable('/dev/urandom')) { $bytes = hash_hmac('sha256', $bytes, file_get_contents('/dev/urandom', null, null, null, $length), true); } return substr($bytes, 0, $bytes); } } $gen = new TokenGenerator(); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(10)); var_dump($gen->generateToken(20)); var_dump($gen->generateToken(20)); var_dump($gen->generateToken(20));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/QZYpQ
function name:  (null)
number of ops:  40
compiled vars:  !0 = $gen
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   NEW                                              $1      'TokenGenerator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   51     3        INIT_FCALL                                               'var_dump'
          4        INIT_METHOD_CALL                                         !0, 'generateToken'
          5        SEND_VAL_EX                                              10
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   52     9        INIT_FCALL                                               'var_dump'
         10        INIT_METHOD_CALL                                         !0, 'generateToken'
         11        SEND_VAL_EX                                              10
         12        DO_FCALL                                      0  $6      
         13        SEND_VAR                                                 $6
         14        DO_ICALL                                                 
   53    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         !0, 'generateToken'
         17        SEND_VAL_EX                                              10
         18        DO_FCALL                                      0  $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                                 
   54    21        INIT_FCALL                                               'var_dump'
         22        INIT_METHOD_CALL                                         !0, 'generateToken'
         23        SEND_VAL_EX                                              20
         24        DO_FCALL                                      0  $10     
         25        SEND_VAR                                                 $10
         26        DO_ICALL                                                 
   55    27        INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !0, 'generateToken'
         29        SEND_VAL_EX                                              20
         30        DO_FCALL                                      0  $12     
         31        SEND_VAR                                                 $12
         32        DO_ICALL                                                 
   56    33        INIT_FCALL                                               'var_dump'
         34        INIT_METHOD_CALL                                         !0, 'generateToken'
         35        SEND_VAL_EX                                              20
         36        DO_FCALL                                      0  $14     
         37        SEND_VAR                                                 $14
         38        DO_ICALL                                                 
         39      > RETURN                                                   1

Class TokenGenerator:
Function generatetoken:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 4
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 4
Branch analysis from position: 33
Branch analysis from position: 4
filename:       /in/QZYpQ
function name:  generateToken
number of ops:  35
compiled vars:  !0 = $length, !1 = $alphabetSize, !2 = $token, !3 = $randomBytes, !4 = $offset
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV_INIT                                        !0      16
    9     1        ASSIGN                                                   !1, 62
   10     2        ASSIGN                                                   !2, ''
   12     3      > JMP                                                      ->30
   13     4    >   INIT_METHOD_CALL                                         'generateRandomBytes'
          5        SEND_VAL_EX                                              32
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !3, $7
   14     8        INIT_FCALL                                               'abs'
          9        INIT_FCALL                                               'hexdec'
         10        INIT_FCALL                                               'bin2hex'
         11        INIT_FCALL                                               'substr'
         12        SEND_VAR                                                 !3
         13        SEND_VAL                                                 0
         14        SEND_VAL                                                 4
         15        DO_ICALL                                         $9      
         16        SEND_VAR                                                 $9
         17        DO_ICALL                                         $10     
         18        SEND_VAR                                                 $10
         19        DO_ICALL                                         $11     
         20        SEND_VAR                                                 $11
         21        DO_ICALL                                         $12     
         22        MOD                                              ~13     $12, !1
         23        ASSIGN                                                   !4, ~13
   15    24        INIT_FCALL                                               'substr'
         25        SEND_VAL                                                 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
         26        SEND_VAR                                                 !4
         27        SEND_VAL                                                 1
         28        DO_ICALL                                         $15     
         29        ASSIGN_OP                                     8          !2, $15
   12    30    >   STRLEN                                           ~17     !2
         31        IS_SMALLER                                               ~17, !0
         32      > JMPNZ                                                    ~18, ->4
   18    33    > > RETURN                                                   !2
   19    34*     > RETURN                                                   null

End of function generatetoken

Function generaterandombytes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 3
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 30
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 44
Branch analysis from position: 34
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 52
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 67
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
Branch analysis from position: 52
Branch analysis from position: 44
Branch analysis from position: 30
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 3
Branch analysis from position: 14
Branch analysis from position: 3
filename:       /in/QZYpQ
function name:  generateRandomBytes
number of ops:  74
compiled vars:  !0 = $length, !1 = $bytes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        ASSIGN                                                   !1, ''
   26     2      > JMP                                                      ->11
   27     3    >   INIT_FCALL                                               'chr'
          4        INIT_FCALL                                               'mt_rand'
          5        SEND_VAL                                                 0
          6        SEND_VAL                                                 255
          7        DO_ICALL                                         $3      
          8        SEND_VAR                                                 $3
          9        DO_ICALL                                         $4      
         10        ASSIGN_OP                                     8          !1, $4
   26    11    >   STRLEN                                           ~6      !1
         12        IS_SMALLER                                               ~6, !0
         13      > JMPNZ                                                    ~7, ->3
   31    14    >   INIT_FCALL                                               'function_exists'
         15        SEND_VAL                                                 'mcrypt_create_iv'
         16        DO_ICALL                                         $8      
         17      > JMPZ                                                     $8, ->30
   32    18    >   INIT_FCALL                                               'hash_hmac'
         19        SEND_VAL                                                 'sha256'
         20        SEND_VAR                                                 !1
         21        INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
         22        SEND_VAR_EX                                              !0
         23        FETCH_CONSTANT                                   ~9      'MCRYPT_DEV_URANDOM'
         24        SEND_VAL_EX                                              ~9
         25        DO_FCALL                                      0  $10     
         26        SEND_VAR                                                 $10
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                         $11     
         29        ASSIGN                                                   !1, $11
   36    30    >   INIT_FCALL                                               'function_exists'
         31        SEND_VAL                                                 'openssl_random_pseudo_bytes'
         32        DO_ICALL                                         $13     
         33      > JMPZ                                                     $13, ->44
   37    34    >   INIT_FCALL                                               'hash_hmac'
         35        SEND_VAL                                                 'sha256'
         36        SEND_VAR                                                 !1
         37        INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
         38        SEND_VAR_EX                                              !0
         39        DO_FCALL                                      0  $14     
         40        SEND_VAR                                                 $14
         41        SEND_VAL                                                 <true>
         42        DO_ICALL                                         $15     
         43        ASSIGN                                                   !1, $15
   41    44    >   INIT_FCALL                                               'file_exists'
         45        SEND_VAL                                                 '%2Fdev%2Furandom'
         46        DO_ICALL                                         $17     
         47      > JMPZ_EX                                          ~18     $17, ->52
         48    >   INIT_FCALL                                               'is_readable'
         49        SEND_VAL                                                 '%2Fdev%2Furandom'
         50        DO_ICALL                                         $19     
         51        BOOL                                             ~18     $19
         52    > > JMPZ                                                     ~18, ->67
   42    53    >   INIT_FCALL                                               'hash_hmac'
         54        SEND_VAL                                                 'sha256'
         55        SEND_VAR                                                 !1
         56        INIT_FCALL                                               'file_get_contents'
         57        SEND_VAL                                                 '%2Fdev%2Furandom'
         58        SEND_VAL                                                 null
         59        SEND_VAL                                                 null
         60        SEND_VAL                                                 null
         61        SEND_VAR                                                 !0
         62        DO_ICALL                                         $20     
         63        SEND_VAR                                                 $20
         64        SEND_VAL                                                 <true>
         65        DO_ICALL                                         $21     
         66        ASSIGN                                                   !1, $21
   45    67    >   INIT_FCALL                                               'substr'
         68        SEND_VAR                                                 !1
         69        SEND_VAL                                                 0
         70        SEND_VAR                                                 !1
         71        DO_ICALL                                         $23     
         72      > RETURN                                                   $23
   46    73*     > RETURN                                                   null

End of function generaterandombytes

End of class TokenGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.43 ms | 1408 KiB | 37 Q