3v4l.org

run code in 300+ PHP versions simultaneously
<?php const CIPHER = MCRYPT_RIJNDAEL_128; const KEY_BYTE_SIZE = 16; const CIPHER_MODE = 'cbc'; const HASH_FUNCTION = 'sha256'; const MAC_BYTE_SIZE = 32; const ENCRYPTION_INFO = 'PIPS|KeyForEncryption'; const AUTHENTICATION_INFO = 'PIPS|KeyForAuthentication'; $key = base64_decode('wdmd3XIhnOAelom4Y8yPbw=='); $ekey = HKDF(HASH_FUNCTION, $key, KEY_BYTE_SIZE, ENCRYPTION_INFO); //print "$ekey\n"; //print urlsafe_b64encode($ekey); //$ivsize = mcrypt_get_iv_size(CIPHER, CIPHER_MODE); //if ($ivsize === FALSE || $ivsize <= 0) { // throw new CannotPerformOperationException(); //} $ivsize= 16; $iv = SecureRandom($ivsize); print urlsafe_encode(PlainEncrypt('It is a kitten', 'fish', 'freefreefreefree')); function urlsafe_b64encode($string) { $data = base64_encode($string); $data = str_replace(array('+','/','='),array('-','_',''),$data); return $data; } function urlsafe_b64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); return base64_decode($data); } function HKDF($hash, $ikm, $length, $info = '', $salt = NULL) { // Find the correct digest length as quickly as we can. $digest_length = MAC_BYTE_SIZE; if ($hash != HASH_FUNCTION) { $digest_length = strlen(hash_hmac($hash, '', '', true)); } // Sanity-check the desired output length. if (empty($length) || !is_int($length) || $length < 0 || $length > 255 * $digest_length) { return CannotPerformOperationException(); } // "if [salt] not provided, is set to a string of HashLen zeroes." if (is_null($salt)) { $salt = str_repeat("\x00", $digest_length); } // HKDF-Extract: // PRK = HMAC-Hash(salt, IKM) // The salt is the HMAC key. $prk = hash_hmac($hash, $ikm, $salt, true); // HKDF-Expand: // This check is useless, but it serves as a reminder to the spec. if (strlen($prk) < $digest_length) { throw new CannotPerformOperationException(); } // T(0) = '' $t = ''; $last_block = ''; for ($block_index = 1; strlen($t) < $length; $block_index++) { // T(i) = HMAC-Hash(PRK, T(i-1) | info | 0x??) $last_block = hash_hmac( $hash, $last_block . $info . chr($block_index), $prk, true ); // T = T(1) | T(2) | T(3) | ... | T(N) $t .= $last_block; } // ORM = first L octets of T $orm = substr($t, 0, $length); if ($orm === FALSE) { throw new CannotPerformOperationException(); } return $orm; } function SecureRandom($octets) { return urlsafe_b64decode('FSDTs5UoRN07CAeB84KVIw'); $random = mcrypt_create_iv($octets, MCRYPT_DEV_URANDOM); if ($random === FALSE) { throw new CannotPerformOperationException(); } else { return $random; } } function PlainEncrypt($plaintext, $key, $iv) { $crypt = mcrypt_module_open(CIPHER, "", CIPHER_MODE, ""); if ($crypt === FALSE) { throw new CannotPerformOperationException(); } // Pad the plaintext to a multiple of the block size. $block = mcrypt_enc_get_block_size($crypt); $pad = $block - (strlen($plaintext) % $block); $plaintext .= str_repeat(chr($pad), $pad); $ret = mcrypt_generic_init($crypt, $key, $iv); if ($ret !== 0) { throw new CannotPerformOperationException(); } $ciphertext = mcrypt_generic($crypt, $plaintext); $ret = mcrypt_generic_deinit($crypt); if ($ret !== TRUE) { throw new CannotPerformOperationException(); } $ret = mcrypt_module_close($crypt); if ($ret !== TRUE) { throw new CannotPerformOperationException(); } return $ciphertext; } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uY4k3
function name:  (null)
number of ops:  36
compiled vars:  !0 = $key, !1 = $ekey, !2 = $ivsize, !3 = $iv
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CONST                                            'CIPHER', <const ast>
    4     1        DECLARE_CONST                                            'KEY_BYTE_SIZE', 16
    5     2        DECLARE_CONST                                            'CIPHER_MODE', 'cbc'
    6     3        DECLARE_CONST                                            'HASH_FUNCTION', 'sha256'
    7     4        DECLARE_CONST                                            'MAC_BYTE_SIZE', 32
    8     5        DECLARE_CONST                                            'ENCRYPTION_INFO', 'PIPS%7CKeyForEncryption'
    9     6        DECLARE_CONST                                            'AUTHENTICATION_INFO', 'PIPS%7CKeyForAuthentication'
   11     7        INIT_FCALL                                               'base64_decode'
          8        SEND_VAL                                                 'wdmd3XIhnOAelom4Y8yPbw%3D%3D'
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !0, $4
   12    11        INIT_FCALL_BY_NAME                                       'HKDF'
         12        FETCH_CONSTANT                                   ~6      'HASH_FUNCTION'
         13        SEND_VAL_EX                                              ~6
         14        SEND_VAR_EX                                              !0
         15        FETCH_CONSTANT                                   ~7      'KEY_BYTE_SIZE'
         16        SEND_VAL_EX                                              ~7
         17        FETCH_CONSTANT                                   ~8      'ENCRYPTION_INFO'
         18        SEND_VAL_EX                                              ~8
         19        DO_FCALL                                      0  $9      
         20        ASSIGN                                                   !1, $9
   20    21        ASSIGN                                                   !2, 16
   21    22        INIT_FCALL_BY_NAME                                       'SecureRandom'
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0  $12     
         25        ASSIGN                                                   !3, $12
   22    26        INIT_FCALL_BY_NAME                                       'urlsafe_encode'
         27        INIT_FCALL_BY_NAME                                       'PlainEncrypt'
         28        SEND_VAL_EX                                              'It+is+a+kitten'
         29        SEND_VAL_EX                                              'fish'
         30        SEND_VAL_EX                                              'freefreefreefree'
         31        DO_FCALL                                      0  $14     
         32        SEND_VAR_NO_REF_EX                                       $14
         33        DO_FCALL                                      0  $15     
         34        ECHO                                                     $15
  131    35      > RETURN                                                   1

Function urlsafe_b64encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uY4k3
function name:  urlsafe_b64encode
number of ops:  13
compiled vars:  !0 = $string, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        INIT_FCALL                                               'base64_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !1, $2
   26     5        INIT_FCALL                                               'str_replace'
          6        SEND_VAL                                                 <array>
          7        SEND_VAL                                                 <array>
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !1, $4
   27    11      > RETURN                                                   !1
   28    12*     > RETURN                                                   null

End of function urlsafe_b64encode

Function urlsafe_b64decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uY4k3
function name:  urlsafe_b64decode
number of ops:  12
compiled vars:  !0 = $string, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        INIT_FCALL                                               'str_replace'
          2        SEND_VAL                                                 <array>
          3        SEND_VAL                                                 <array>
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        ASSIGN                                                   !1, $2
   32     7        INIT_FCALL                                               'base64_decode'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $4      
         10      > RETURN                                                   $4
   33    11*     > RETURN                                                   null

End of function urlsafe_b64decode

Function hkdf:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 18
Branch analysis from position: 10
2 jumps found. (Code = 47) Position 1 = 20, Position 2 = 23
Branch analysis from position: 20
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
2 jumps found. (Code = 47) Position 1 = 27, Position 2 = 30
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 51, Position 2 = 54
Branch analysis from position: 51
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 58
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 83, Position 2 = 86
Branch analysis from position: 83
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 86
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 58
Branch analysis from position: 75
Branch analysis from position: 58
Branch analysis from position: 41
Branch analysis from position: 30
Branch analysis from position: 26
Branch analysis from position: 23
Branch analysis from position: 18
filename:       /in/uY4k3
function name:  HKDF
number of ops:  88
compiled vars:  !0 = $hash, !1 = $ikm, !2 = $length, !3 = $info, !4 = $salt, !5 = $digest_length, !6 = $prk, !7 = $t, !8 = $last_block, !9 = $block_index, !10 = $orm
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV_INIT                                        !3      ''
          4        RECV_INIT                                        !4      null
   38     5        FETCH_CONSTANT                                   ~11     'MAC_BYTE_SIZE'
          6        ASSIGN                                                   !5, ~11
   39     7        FETCH_CONSTANT                                   ~13     'HASH_FUNCTION'
          8        IS_NOT_EQUAL                                             !0, ~13
          9      > JMPZ                                                     ~14, ->18
   40    10    >   INIT_FCALL                                               'hash_hmac'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 ''
         13        SEND_VAL                                                 ''
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $15     
         16        STRLEN                                           ~16     $15
         17        ASSIGN                                                   !5, ~16
   44    18    >   ISSET_ISEMPTY_CV                                 ~18     !2
         19      > JMPNZ_EX                                         ~18     ~18, ->23
         20    >   TYPE_CHECK                                   16  ~19     !2
         21        BOOL_NOT                                         ~20     ~19
         22        BOOL                                             ~18     ~20
         23    > > JMPNZ_EX                                         ~18     ~18, ->26
   45    24    >   IS_SMALLER                                       ~21     !2, 0
         25        BOOL                                             ~18     ~21
         26    > > JMPNZ_EX                                         ~18     ~18, ->30
         27    >   MUL                                              ~22     !5, 255
         28        IS_SMALLER                                       ~23     ~22, !2
         29        BOOL                                             ~18     ~23
         30    > > JMPZ                                                     ~18, ->34
   46    31    >   INIT_FCALL_BY_NAME                                       'CannotPerformOperationException'
         32        DO_FCALL                                      0  $24     
         33      > RETURN                                                   $24
   50    34    >   TYPE_CHECK                                    2          !4
         35      > JMPZ                                                     ~25, ->41
   51    36    >   INIT_FCALL                                               'str_repeat'
         37        SEND_VAL                                                 '%00'
         38        SEND_VAR                                                 !5
         39        DO_ICALL                                         $26     
         40        ASSIGN                                                   !4, $26
   57    41    >   INIT_FCALL                                               'hash_hmac'
         42        SEND_VAR                                                 !0
         43        SEND_VAR                                                 !1
         44        SEND_VAR                                                 !4
         45        SEND_VAL                                                 <true>
         46        DO_ICALL                                         $28     
         47        ASSIGN                                                   !6, $28
   62    48        STRLEN                                           ~30     !6
         49        IS_SMALLER                                               ~30, !5
         50      > JMPZ                                                     ~31, ->54
   63    51    >   NEW                                              $32     'CannotPerformOperationException'
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $32
   67    54    >   ASSIGN                                                   !7, ''
   68    55        ASSIGN                                                   !8, ''
   69    56        ASSIGN                                                   !9, 1
         57      > JMP                                                      ->72
   71    58    >   INIT_FCALL                                               'hash_hmac'
   72    59        SEND_VAR                                                 !0
   73    60        CONCAT                                           ~37     !8, !3
         61        INIT_FCALL                                               'chr'
         62        SEND_VAR                                                 !9
         63        DO_ICALL                                         $38     
         64        CONCAT                                           ~39     ~37, $38
         65        SEND_VAL                                                 ~39
   74    66        SEND_VAR                                                 !6
   75    67        SEND_VAL                                                 <true>
         68        DO_ICALL                                         $40     
   71    69        ASSIGN                                                   !8, $40
   78    70        ASSIGN_OP                                     8          !7, !8
   69    71        PRE_INC                                                  !9
         72    >   STRLEN                                           ~44     !7
         73        IS_SMALLER                                               ~44, !2
         74      > JMPNZ                                                    ~45, ->58
   82    75    >   INIT_FCALL                                               'substr'
         76        SEND_VAR                                                 !7
         77        SEND_VAL                                                 0
         78        SEND_VAR                                                 !2
         79        DO_ICALL                                         $46     
         80        ASSIGN                                                   !10, $46
   83    81        TYPE_CHECK                                    4          !10
         82      > JMPZ                                                     ~48, ->86
   84    83    >   NEW                                              $49     'CannotPerformOperationException'
         84        DO_FCALL                                      0          
         85      > THROW                                         0          $49
   86    86    > > RETURN                                                   !10
   87    87*     > RETURN                                                   null

End of function hkdf

Function securerandom:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uY4k3
function name:  SecureRandom
number of ops:  19
compiled vars:  !0 = $octets, !1 = $random
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   RECV                                             !0      
   91     1        INIT_FCALL                                               'urlsafe_b64decode'
          2        SEND_VAL                                                 'FSDTs5UoRN07CAeB84KVIw'
          3        DO_FCALL                                      0  $2      
          4      > RETURN                                                   $2
   93     5*       INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
          6*       SEND_VAR_EX                                              !0
          7*       FETCH_CONSTANT                                   ~3      'MCRYPT_DEV_URANDOM'
          8*       SEND_VAL_EX                                              ~3
          9*       DO_FCALL                                      0  $4      
         10*       ASSIGN                                                   !1, $4
   94    11*       TYPE_CHECK                                    4          !1
         12*       JMPZ                                                     ~6, ->17
   95    13*       NEW                                              $7      'CannotPerformOperationException'
         14*       DO_FCALL                                      0          
         15*       THROW                                         0          $7
         16*       JMP                                                      ->18
   97    17*       RETURN                                                   !1
   99    18*     > RETURN                                                   null

End of function securerandom

Function plainencrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 44
Branch analysis from position: 41
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 55, Position 2 = 58
Branch analysis from position: 55
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 64, Position 2 = 67
Branch analysis from position: 64
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uY4k3
function name:  PlainEncrypt
number of ops:  69
compiled vars:  !0 = $plaintext, !1 = $key, !2 = $iv, !3 = $crypt, !4 = $block, !5 = $pad, !6 = $ret, !7 = $ciphertext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  103     3        INIT_FCALL_BY_NAME                                       'mcrypt_module_open'
          4        FETCH_CONSTANT                                   ~8      'CIPHER'
          5        SEND_VAL_EX                                              ~8
          6        SEND_VAL_EX                                              ''
          7        FETCH_CONSTANT                                   ~9      'CIPHER_MODE'
          8        SEND_VAL_EX                                              ~9
          9        SEND_VAL_EX                                              ''
         10        DO_FCALL                                      0  $10     
         11        ASSIGN                                                   !3, $10
  104    12        TYPE_CHECK                                    4          !3
         13      > JMPZ                                                     ~12, ->17
  105    14    >   NEW                                              $13     'CannotPerformOperationException'
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $13
  109    17    >   INIT_FCALL_BY_NAME                                       'mcrypt_enc_get_block_size'
         18        SEND_VAR_EX                                              !3
         19        DO_FCALL                                      0  $15     
         20        ASSIGN                                                   !4, $15
  110    21        STRLEN                                           ~17     !0
         22        MOD                                              ~18     ~17, !4
         23        SUB                                              ~19     !4, ~18
         24        ASSIGN                                                   !5, ~19
  111    25        INIT_FCALL                                               'str_repeat'
         26        INIT_FCALL                                               'chr'
         27        SEND_VAR                                                 !5
         28        DO_ICALL                                         $21     
         29        SEND_VAR                                                 $21
         30        SEND_VAR                                                 !5
         31        DO_ICALL                                         $22     
         32        ASSIGN_OP                                     8          !0, $22
  113    33        INIT_FCALL_BY_NAME                                       'mcrypt_generic_init'
         34        SEND_VAR_EX                                              !3
         35        SEND_VAR_EX                                              !1
         36        SEND_VAR_EX                                              !2
         37        DO_FCALL                                      0  $24     
         38        ASSIGN                                                   !6, $24
  114    39        IS_NOT_IDENTICAL                                         !6, 0
         40      > JMPZ                                                     ~26, ->44
  115    41    >   NEW                                              $27     'CannotPerformOperationException'
         42        DO_FCALL                                      0          
         43      > THROW                                         0          $27
  118    44    >   INIT_FCALL_BY_NAME                                       'mcrypt_generic'
         45        SEND_VAR_EX                                              !3
         46        SEND_VAR_EX                                              !0
         47        DO_FCALL                                      0  $29     
         48        ASSIGN                                                   !7, $29
  119    49        INIT_FCALL_BY_NAME                                       'mcrypt_generic_deinit'
         50        SEND_VAR_EX                                              !3
         51        DO_FCALL                                      0  $31     
         52        ASSIGN                                                   !6, $31
  120    53        TYPE_CHECK                                  1014          !6
         54      > JMPZ                                                     ~33, ->58
  121    55    >   NEW                                              $34     'CannotPerformOperationException'
         56        DO_FCALL                                      0          
         57      > THROW                                         0          $34
  123    58    >   INIT_FCALL_BY_NAME                                       'mcrypt_module_close'
         59        SEND_VAR_EX                                              !3
         60        DO_FCALL                                      0  $36     
         61        ASSIGN                                                   !6, $36
  124    62        TYPE_CHECK                                  1014          !6
         63      > JMPZ                                                     ~38, ->67
  125    64    >   NEW                                              $39     'CannotPerformOperationException'
         65        DO_FCALL                                      0          
         66      > THROW                                         0          $39
  128    67    > > RETURN                                                   !7
  129    68*     > RETURN                                                   null

End of function plainencrypt

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.54 ms | 1414 KiB | 28 Q