3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Generate a PBKDF2 key derivation of a supplied password * * This is a hash_pbkdf2() implementation for PHP versions 5.3 and 5.4. * @link http://www.php.net/manual/en/function.hash-pbkdf2.php * * @param string $algo * @param string $password * @param string $salt * @param int $iterations * @param int $length * @param bool $rawOutput * * @return string */ function compat_pbkdf2($algo, $password, $salt, $iterations, $length = 0, $rawOutput = false) { // check for hashing algorithm if (!in_array(strtolower($algo), hash_algos())) { trigger_error(sprintf( '%s(): Unknown hashing algorithm: %s', __FUNCTION__, $algo ), E_USER_WARNING); return false; } // check for type of iterations and length foreach (array(4 => $iterations, 5 => $length) as $index => $value) { if (!is_numeric($value)) { trigger_error(sprintf( '%s() expects parameter %d to be long, %s given', __FUNCTION__, $index, gettype($value) ), E_USER_WARNING); return null; } } // check iterations $iterations = (int)$iterations; if ($iterations <= 0) { trigger_error(sprintf( '%s(): Iterations must be a positive integer: %d', __FUNCTION__, $iterations ), E_USER_WARNING); return false; } // check length $length = (int)$length; if ($length < 0) { trigger_error(sprintf( '%s(): Iterations must be greater than or equal to 0: %d', __FUNCTION__, $length ), E_USER_WARNING); return false; } // check salt if (strlen($salt) > PHP_INT_MAX - 4) { trigger_error(sprintf( '%s(): Supplied salt is too long, max of INT_MAX - 4 bytes: %d supplied', __FUNCTION__, strlen($salt) ), E_USER_WARNING); return false; } // initialize $derivedKey = ''; $loops = 1; if ($length > 0) { $loops = (int)ceil($length / strlen(hash($algo, '', $rawOutput))); } // hash for each blocks for ($i = 1; $i <= $loops; $i++) { $digest = hash_hmac($algo, $salt . pack('N', $i), $password, true); $block = $digest; for ($j = 1; $j < $iterations; $j++) { $digest = hash_hmac($algo, $digest, $password, true); $block ^= $digest; } $derivedKey .= $block; } if (!$rawOutput) { $derivedKey = bin2hex($derivedKey); } if ($length > 0) { return substr($derivedKey, 0, $length); } return $derivedKey; } // test echo hash_pbkdf2("sha256", "!Password@123#", 'pCtRL7cHbJR-6Px1g5(&o&O(2Kj_pdbC', 1000, 64); echo compat_pbkdf2("sha256", "!Password@123#", 'pCtRL7cHbJR-6Px1g5(&o&O(2Kj_pdbC', 1000, 64);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bGYD6
function name:  (null)
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   INIT_FCALL                                               'hash_pbkdf2'
          1        SEND_VAL                                                 'sha256'
          2        SEND_VAL                                                 '%21Password%40123%23'
          3        SEND_VAL                                                 'pCtRL7cHbJR-6Px1g5%28%26o%26O%282Kj_pdbC'
          4        SEND_VAL                                                 1000
          5        SEND_VAL                                                 64
          6        DO_ICALL                                         $0      
          7        ECHO                                                     $0
   91     8        INIT_FCALL                                               'compat_pbkdf2'
          9        SEND_VAL                                                 'sha256'
         10        SEND_VAL                                                 '%21Password%40123%23'
         11        SEND_VAL                                                 'pCtRL7cHbJR-6Px1g5%28%26o%26O%282Kj_pdbC'
         12        SEND_VAL                                                 1000
         13        SEND_VAL                                                 64
         14        DO_FCALL                                      0  $1      
         15        ECHO                                                     $1
         16      > RETURN                                                   1

Function compat_pbkdf2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 27
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 51
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 51
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 50
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 56, Position 2 = 66
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 70, Position 2 = 80
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 80
2 jumps found. (Code = 43) Position 1 = 83, Position 2 = 94
Branch analysis from position: 83
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 94
2 jumps found. (Code = 43) Position 1 = 98, Position 2 = 110
Branch analysis from position: 98
1 jumps found. (Code = 42) Position 1 = 140
Branch analysis from position: 140
2 jumps found. (Code = 44) Position 1 = 142, Position 2 = 112
Branch analysis from position: 142
2 jumps found. (Code = 43) Position 1 = 144, Position 2 = 148
Branch analysis from position: 144
2 jumps found. (Code = 43) Position 1 = 150, Position 2 = 156
Branch analysis from position: 150
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 156
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 148
Branch analysis from position: 112
1 jumps found. (Code = 42) Position 1 = 136
Branch analysis from position: 136
2 jumps found. (Code = 44) Position 1 = 138, Position 2 = 127
Branch analysis from position: 138
2 jumps found. (Code = 44) Position 1 = 142, Position 2 = 112
Branch analysis from position: 142
Branch analysis from position: 112
Branch analysis from position: 127
2 jumps found. (Code = 44) Position 1 = 138, Position 2 = 127
Branch analysis from position: 138
Branch analysis from position: 127
Branch analysis from position: 110
Branch analysis from position: 51
filename:       /in/bGYD6
function name:  compat_pbkdf2
number of ops:  158
compiled vars:  !0 = $algo, !1 = $password, !2 = $salt, !3 = $iterations, !4 = $length, !5 = $rawOutput, !6 = $value, !7 = $index, !8 = $derivedKey, !9 = $loops, !10 = $i, !11 = $digest, !12 = $block, !13 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV_INIT                                        !4      0
          5        RECV_INIT                                        !5      <false>
   21     6        INIT_FCALL                                               'in_array'
          7        INIT_FCALL                                               'strtolower'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $14     
         10        SEND_VAR                                                 $14
         11        INIT_FCALL                                               'hash_algos'
         12        DO_ICALL                                         $15     
         13        SEND_VAR                                                 $15
         14        DO_ICALL                                         $16     
         15        BOOL_NOT                                         ~17     $16
         16      > JMPZ                                                     ~17, ->27
   22    17    >   INIT_FCALL                                               'trigger_error'
         18        INIT_FCALL                                               'sprintf'
   23    19        SEND_VAL                                                 '%25s%28%29%3A+Unknown+hashing+algorithm%3A+%25s'
   24    20        SEND_VAL                                                 'compat_pbkdf2'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $18     
         23        SEND_VAR                                                 $18
   25    24        SEND_VAL                                                 512
         25        DO_ICALL                                                 
   26    26      > RETURN                                                   <false>
   29    27    >   INIT_ARRAY                                       ~20     !3, 4
         28        ADD_ARRAY_ELEMENT                                ~20     !4, 5
         29      > FE_RESET_R                                       $21     ~20, ->51
         30    > > FE_FETCH_R                                       ~22     $21, !6, ->51
         31    >   ASSIGN                                                   !7, ~22
   30    32        INIT_FCALL                                               'is_numeric'
         33        SEND_VAR                                                 !6
         34        DO_ICALL                                         $24     
         35        BOOL_NOT                                         ~25     $24
         36      > JMPZ                                                     ~25, ->50
   31    37    >   INIT_FCALL                                               'trigger_error'
         38        INIT_FCALL                                               'sprintf'
   32    39        SEND_VAL                                                 '%25s%28%29+expects+parameter+%25d+to+be+long%2C+%25s+given'
   33    40        SEND_VAL                                                 'compat_pbkdf2'
         41        SEND_VAR                                                 !7
         42        GET_TYPE                                         ~26     !6
         43        SEND_VAL                                                 ~26
         44        DO_ICALL                                         $27     
         45        SEND_VAR                                                 $27
   34    46        SEND_VAL                                                 512
         47        DO_ICALL                                                 
   35    48        FE_FREE                                                  $21
         49      > RETURN                                                   null
   29    50    > > JMP                                                      ->30
         51    >   FE_FREE                                                  $21
   39    52        CAST                                          4  ~29     !3
         53        ASSIGN                                                   !3, ~29
   40    54        IS_SMALLER_OR_EQUAL                                      !3, 0
         55      > JMPZ                                                     ~31, ->66
   41    56    >   INIT_FCALL                                               'trigger_error'
         57        INIT_FCALL                                               'sprintf'
   42    58        SEND_VAL                                                 '%25s%28%29%3A+Iterations+must+be+a+positive+integer%3A+%25d'
   43    59        SEND_VAL                                                 'compat_pbkdf2'
         60        SEND_VAR                                                 !3
         61        DO_ICALL                                         $32     
         62        SEND_VAR                                                 $32
   44    63        SEND_VAL                                                 512
         64        DO_ICALL                                                 
   45    65      > RETURN                                                   <false>
   48    66    >   CAST                                          4  ~34     !4
         67        ASSIGN                                                   !4, ~34
   49    68        IS_SMALLER                                               !4, 0
         69      > JMPZ                                                     ~36, ->80
   50    70    >   INIT_FCALL                                               'trigger_error'
         71        INIT_FCALL                                               'sprintf'
   51    72        SEND_VAL                                                 '%25s%28%29%3A+Iterations+must+be+greater+than+or+equal+to+0%3A+%25d'
   52    73        SEND_VAL                                                 'compat_pbkdf2'
         74        SEND_VAR                                                 !4
         75        DO_ICALL                                         $37     
         76        SEND_VAR                                                 $37
   53    77        SEND_VAL                                                 512
         78        DO_ICALL                                                 
   54    79      > RETURN                                                   <false>
   57    80    >   STRLEN                                           ~39     !2
         81        IS_SMALLER                                               9223372036854775803, ~39
         82      > JMPZ                                                     ~40, ->94
   58    83    >   INIT_FCALL                                               'trigger_error'
         84        INIT_FCALL                                               'sprintf'
   59    85        SEND_VAL                                                 '%25s%28%29%3A+Supplied+salt+is+too+long%2C+max+of+INT_MAX+-+4+bytes%3A+%25d+supplied'
   60    86        SEND_VAL                                                 'compat_pbkdf2'
         87        STRLEN                                           ~41     !2
         88        SEND_VAL                                                 ~41
         89        DO_ICALL                                         $42     
         90        SEND_VAR                                                 $42
   61    91        SEND_VAL                                                 512
         92        DO_ICALL                                                 
   62    93      > RETURN                                                   <false>
   65    94    >   ASSIGN                                                   !8, ''
   66    95        ASSIGN                                                   !9, 1
   67    96        IS_SMALLER                                               0, !4
         97      > JMPZ                                                     ~46, ->110
   68    98    >   INIT_FCALL                                               'ceil'
         99        INIT_FCALL                                               'hash'
        100        SEND_VAR                                                 !0
        101        SEND_VAL                                                 ''
        102        SEND_VAR                                                 !5
        103        DO_ICALL                                         $47     
        104        STRLEN                                           ~48     $47
        105        DIV                                              ~49     !4, ~48
        106        SEND_VAL                                                 ~49
        107        DO_ICALL                                         $50     
        108        CAST                                          4  ~51     $50
        109        ASSIGN                                                   !9, ~51
   71   110    >   ASSIGN                                                   !10, 1
        111      > JMP                                                      ->140
   72   112    >   INIT_FCALL                                               'hash_hmac'
        113        SEND_VAR                                                 !0
        114        INIT_FCALL                                               'pack'
        115        SEND_VAL                                                 'N'
        116        SEND_VAR                                                 !10
        117        DO_ICALL                                         $54     
        118        CONCAT                                           ~55     !2, $54
        119        SEND_VAL                                                 ~55
        120        SEND_VAR                                                 !1
        121        SEND_VAL                                                 <true>
        122        DO_ICALL                                         $56     
        123        ASSIGN                                                   !11, $56
   73   124        ASSIGN                                                   !12, !11
   74   125        ASSIGN                                                   !13, 1
        126      > JMP                                                      ->136
   75   127    >   INIT_FCALL                                               'hash_hmac'
        128        SEND_VAR                                                 !0
        129        SEND_VAR                                                 !11
        130        SEND_VAR                                                 !1
        131        SEND_VAL                                                 <true>
        132        DO_ICALL                                         $60     
        133        ASSIGN                                                   !11, $60
   76   134        ASSIGN_OP                                    11          !12, !11
   74   135        PRE_INC                                                  !13
        136    >   IS_SMALLER                                               !13, !3
        137      > JMPNZ                                                    ~64, ->127
   78   138    >   ASSIGN_OP                                     8          !8, !12
   71   139        PRE_INC                                                  !10
        140    >   IS_SMALLER_OR_EQUAL                                      !10, !9
        141      > JMPNZ                                                    ~67, ->112
   80   142    >   BOOL_NOT                                         ~68     !5
        143      > JMPZ                                                     ~68, ->148
   81   144    >   INIT_FCALL                                               'bin2hex'
        145        SEND_VAR                                                 !8
        146        DO_ICALL                                         $69     
        147        ASSIGN                                                   !8, $69
   83   148    >   IS_SMALLER                                               0, !4
        149      > JMPZ                                                     ~71, ->156
   84   150    >   INIT_FCALL                                               'substr'
        151        SEND_VAR                                                 !8
        152        SEND_VAL                                                 0
        153        SEND_VAR                                                 !4
        154        DO_ICALL                                         $72     
        155      > RETURN                                                   $72
   86   156    > > RETURN                                                   !8
   87   157*     > RETURN                                                   null

End of function compat_pbkdf2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
272.12 ms | 1415 KiB | 41 Q