3v4l.org

run code in 300+ PHP versions simultaneously
<?php $password = 'My Plaintext Password'; $plaintext = 'This is my message'; $secretBox = new Secretbox(); $ciphertext = $secretBox->encrypt($plaintext, $password); var_dump($ciphertext); $decryptedPlaintext = $secretBox->decrypt($ciphertext, $password); var_dump($decryptedPlaintext); assertThat($decryptedPlaintext === $plaintext, 'Can decrypt'); assertThat( $secretBox->encrypt($plaintext, $password) !== $secretBox->encrypt($plaintext, $password), 'Should not result in same ciphertext' ); try { $failed = false; // modify ciphertext $ciphertext[65] = 'f'; $secretBox->decrypt($ciphertext, $password); } catch (\Exception $e) { $failed = true; } finally { assertThat($failed, 'Tempered chiphertext should result in exception'); } class Secretbox { public function encrypt(string $plaintext, string $password): string { // create a random salt for key derivation $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES); $key = $this->deriveKeyFromUserPassword($password, $salt); $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key); sodium_memzero($password); sodium_memzero($key); return $salt.$nonce.$ciphertext; } public function decrypt(string $ciphertext, string $password): string { $salt = substr($ciphertext, 0, SODIUM_CRYPTO_PWHASH_SALTBYTES); $nonce = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $ciphertext = substr($ciphertext, SODIUM_CRYPTO_PWHASH_SALTBYTES + SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $key = $this->deriveKeyFromUserPassword($password, $salt); $plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key); sodium_memzero($password); sodium_memzero($key); sodium_memzero($nonce); if ($plaintext === false) { throw new \InvalidArgumentException('Bad ciphertext'); } return $plaintext; } private function deriveKeyFromUserPassword(string $password, string $salt): string { $key = sodium_crypto_pwhash( SODIUM_CRYPTO_SECRETBOX_KEYBYTES, $password, $salt, SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE, SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE ); sodium_memzero($password); return $key; } } function assertThat(bool $expressionResult, string $message) { if (!$expressionResult) { throw new \RuntimeException($message); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 162) Position 1 = 51, Position 2 = 50
Branch analysis from position: 51
1 jumps found. (Code = 163) Position 1 = -2
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 47
Branch analysis from position: 47
2 jumps found. (Code = 107) Position 1 = 48, Position 2 = -2
Branch analysis from position: 48
2 jumps found. (Code = 162) Position 1 = 51, Position 2 = 50
Branch analysis from position: 51
Branch analysis from position: 50
filename:       /in/XSCbd
function name:  (null)
number of ops:  57
compiled vars:  !0 = $password, !1 = $plaintext, !2 = $secretBox, !3 = $ciphertext, !4 = $decryptedPlaintext, !5 = $failed, !6 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 'My+Plaintext+Password'
    3     1        ASSIGN                                                   !1, 'This+is+my+message'
    5     2        NEW                                              $9      'Secretbox'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $9
    7     5        INIT_METHOD_CALL                                         !2, 'encrypt'
          6        SEND_VAR_EX                                              !1
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $12     
          9        ASSIGN                                                   !3, $12
    9    10        INIT_FCALL                                               'var_dump'
         11        SEND_VAR                                                 !3
         12        DO_ICALL                                                 
   11    13        INIT_METHOD_CALL                                         !2, 'decrypt'
         14        SEND_VAR_EX                                              !3
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0  $15     
         17        ASSIGN                                                   !4, $15
   13    18        INIT_FCALL                                               'var_dump'
         19        SEND_VAR                                                 !4
         20        DO_ICALL                                                 
   15    21        INIT_FCALL_BY_NAME                                       'assertThat'
         22        IS_IDENTICAL                                     ~18     !4, !1
         23        SEND_VAL_EX                                              ~18
         24        SEND_VAL_EX                                              'Can+decrypt'
         25        DO_FCALL                                      0          
   17    26        INIT_FCALL_BY_NAME                                       'assertThat'
   18    27        INIT_METHOD_CALL                                         !2, 'encrypt'
         28        SEND_VAR_EX                                              !1
         29        SEND_VAR_EX                                              !0
         30        DO_FCALL                                      0  $20     
         31        INIT_METHOD_CALL                                         !2, 'encrypt'
         32        SEND_VAR_EX                                              !1
         33        SEND_VAR_EX                                              !0
         34        DO_FCALL                                      0  $21     
         35        IS_NOT_IDENTICAL                                 ~22     $20, $21
         36        SEND_VAL_EX                                              ~22
   19    37        SEND_VAL_EX                                              'Should+not+result+in+same+ciphertext'
   17    38        DO_FCALL                                      0          
   23    39        ASSIGN                                                   !5, <false>
   25    40        ASSIGN_DIM                                               !3, 65
         41        OP_DATA                                                  'f'
   26    42        INIT_METHOD_CALL                                         !2, 'decrypt'
         43        SEND_VAR_EX                                              !3
         44        SEND_VAR_EX                                              !0
         45        DO_FCALL                                      0          
         46      > JMP                                                      ->49
   27    47  E > > CATCH                                       last         'Exception'
   28    48    >   ASSIGN                                                   !5, <true>
   29    49    > > FAST_CALL                                                ->51
         50    > > JMP                                                      ->56
   30    51    >   INIT_FCALL_BY_NAME                                       'assertThat'
         52        SEND_VAR_EX                                              !5
         53        SEND_VAL_EX                                              'Tempered+chiphertext+should+result+in+exception'
         54        DO_FCALL                                      0          
         55      > FAST_RET                                                 
   90    56    > > RETURN                                                   1

Function assertthat:
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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XSCbd
function name:  assertThat
number of ops:  9
compiled vars:  !0 = $expressionResult, !1 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   87     2        BOOL_NOT                                         ~2      !0
          3      > JMPZ                                                     ~2, ->8
   88     4    >   NEW                                              $3      'RuntimeException'
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $3
   90     8    > > RETURN                                                   null

End of function assertthat

Class Secretbox:
Function encrypt:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XSCbd
function name:  encrypt
number of ops:  35
compiled vars:  !0 = $plaintext, !1 = $password, !2 = $salt, !3 = $key, !4 = $nonce, !5 = $ciphertext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   41     2        INIT_FCALL                                               'random_bytes'
          3        FETCH_CONSTANT                                   ~6      'SODIUM_CRYPTO_PWHASH_SALTBYTES'
          4        SEND_VAL                                                 ~6
          5        DO_ICALL                                         $7      
          6        ASSIGN                                                   !2, $7
   42     7        INIT_METHOD_CALL                                         'deriveKeyFromUserPassword'
          8        SEND_VAR_EX                                              !1
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $9      
         11        ASSIGN                                                   !3, $9
   43    12        INIT_FCALL                                               'random_bytes'
         13        FETCH_CONSTANT                                   ~11     'SODIUM_CRYPTO_SECRETBOX_NONCEBYTES'
         14        SEND_VAL                                                 ~11
         15        DO_ICALL                                         $12     
         16        ASSIGN                                                   !4, $12
   44    17        INIT_FCALL_BY_NAME                                       'sodium_crypto_secretbox'
         18        SEND_VAR_EX                                              !0
         19        SEND_VAR_EX                                              !4
         20        SEND_VAR_EX                                              !3
         21        DO_FCALL                                      0  $14     
         22        ASSIGN                                                   !5, $14
   46    23        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0          
   47    26        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         27        SEND_VAR_EX                                              !3
         28        DO_FCALL                                      0          
   49    29        CONCAT                                           ~18     !2, !4
         30        CONCAT                                           ~19     ~18, !5
         31        VERIFY_RETURN_TYPE                                       ~19
         32      > RETURN                                                   ~19
   50    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 51
Branch analysis from position: 47
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XSCbd
function name:  decrypt
number of ops:  55
compiled vars:  !0 = $ciphertext, !1 = $password, !2 = $salt, !3 = $nonce, !4 = $key, !5 = $plaintext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   54     2        INIT_FCALL                                               'substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        FETCH_CONSTANT                                   ~6      'SODIUM_CRYPTO_PWHASH_SALTBYTES'
          6        SEND_VAL                                                 ~6
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
   55     9        INIT_FCALL                                               'substr'
         10        SEND_VAR                                                 !0
         11        FETCH_CONSTANT                                   ~9      'SODIUM_CRYPTO_PWHASH_SALTBYTES'
         12        SEND_VAL                                                 ~9
         13        FETCH_CONSTANT                                   ~10     'SODIUM_CRYPTO_SECRETBOX_NONCEBYTES'
         14        SEND_VAL                                                 ~10
         15        DO_ICALL                                         $11     
         16        ASSIGN                                                   !3, $11
   56    17        INIT_FCALL                                               'substr'
         18        SEND_VAR                                                 !0
         19        FETCH_CONSTANT                                   ~13     'SODIUM_CRYPTO_PWHASH_SALTBYTES'
         20        FETCH_CONSTANT                                   ~14     'SODIUM_CRYPTO_SECRETBOX_NONCEBYTES'
         21        ADD                                              ~15     ~13, ~14
         22        SEND_VAL                                                 ~15
         23        DO_ICALL                                         $16     
         24        ASSIGN                                                   !0, $16
   57    25        INIT_METHOD_CALL                                         'deriveKeyFromUserPassword'
         26        SEND_VAR_EX                                              !1
         27        SEND_VAR_EX                                              !2
         28        DO_FCALL                                      0  $18     
         29        ASSIGN                                                   !4, $18
   59    30        INIT_FCALL_BY_NAME                                       'sodium_crypto_secretbox_open'
         31        SEND_VAR_EX                                              !0
         32        SEND_VAR_EX                                              !3
         33        SEND_VAR_EX                                              !4
         34        DO_FCALL                                      0  $20     
         35        ASSIGN                                                   !5, $20
   60    36        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         37        SEND_VAR_EX                                              !1
         38        DO_FCALL                                      0          
   61    39        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         40        SEND_VAR_EX                                              !4
         41        DO_FCALL                                      0          
   62    42        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         43        SEND_VAR_EX                                              !3
         44        DO_FCALL                                      0          
   64    45        TYPE_CHECK                                    4          !5
         46      > JMPZ                                                     ~25, ->51
   65    47    >   NEW                                              $26     'InvalidArgumentException'
         48        SEND_VAL_EX                                              'Bad+ciphertext'
         49        DO_FCALL                                      0          
         50      > THROW                                         0          $26
   68    51    >   VERIFY_RETURN_TYPE                                       !5
         52      > RETURN                                                   !5
   69    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function decrypt

Function derivekeyfromuserpassword:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XSCbd
function name:  deriveKeyFromUserPassword
number of ops:  20
compiled vars:  !0 = $password, !1 = $salt, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   73     2        INIT_FCALL_BY_NAME                                       'sodium_crypto_pwhash'
   74     3        FETCH_CONSTANT                                   ~3      'SODIUM_CRYPTO_SECRETBOX_KEYBYTES'
          4        SEND_VAL_EX                                              ~3
          5        SEND_VAR_EX                                              !0
          6        SEND_VAR_EX                                              !1
   77     7        FETCH_CONSTANT                                   ~4      'SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE'
          8        SEND_VAL_EX                                              ~4
   78     9        FETCH_CONSTANT                                   ~5      'SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE'
         10        SEND_VAL_EX                                              ~5
   73    11        DO_FCALL                                      0  $6      
         12        ASSIGN                                                   !2, $6
   80    13        INIT_FCALL_BY_NAME                                       'sodium_memzero'
         14        SEND_VAR_EX                                              !0
         15        DO_FCALL                                      0          
   82    16        VERIFY_RETURN_TYPE                                       !2
         17      > RETURN                                                   !2
   83    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function derivekeyfromuserpassword

End of class Secretbox.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.72 ms | 1017 KiB | 16 Q