3v4l.org

run code in 300+ PHP versions simultaneously
<?php class QR { const METHOD = "aes-256-ctr"; /** * * Encrypts (without authenticating) * * @param string $input - json encoded array * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return base64-encoded * @return string (raw binary) */ public static function encrypt($input, $key, $encode = false) { $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = openssl_random_pseudo_bytes($nonceSize); $ciphertext = openssl_encrypt( $input, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); //Pack the IV and ciphertext together - concatenate return ($encode ? base64_encode($nonce.$ciphertext) : $nonce.$ciphertext); } /** * * Decrypts (but does not verify) * * @param string $input - ciphertext message * @param string $key - encryption key * @param boolean $encoded - are we expecting an encoded string? * @return string */ public static function decrypt($input, $key, $encoded = false) { if($encoded){ $input = base64_decode($message, true); if($input===false){ throw new Exception("Encryption failure"); } } $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = mb_substr($input, 0, $nonceSize, "8bit"); $ciphertext = mb_substr($input, $nonceSize, null, "8bit"); $plaintext = openssl_decrypt( $ciphertext, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); return $plaintext; } } class QRCrypt extends QR { const HASH_ALGO = "sha256"; /** * Encrypts then MACs an input * * @param string $input - plaintext input * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return a base64-encoded string * @return string (raw binary) */ public static function encrypt($input, $key, $encode = false) { list($encKey, $authKey) = self::splitKeys($key); //Pass to QR::encrypt $ciphertext = parent::encrypt($input, $encKey); //Calculate a MAC of hte IV and ciphertext $mac = hash_hmac(self::HASH_ALGO, $ciphertext, $authKey, true); return ($encode ? base64_encode($mac.$ciphertext) : $mac.$ciphertext); } /** * Decrypts an input (after verifying integrity) * * @param string $input - ciphertext input * @param string $key - encryption key (raw binary expected) * @param boolean $encoded - are we expecting an encoded string? * @return string (raw binary) */ public static function decrypt($input, $key, $encoded = false) { list($encKey, $authKey) = self::splitKeys($key); if($encoded){ $input = base64_decode($input, true); if($input===false){ throw new Exception("Encryption failure"); } } //Hash size in case HASH_ALGO is changed $hs = mb_strlen(hash(self::HASH_ALGO, "", true), "8bit"); $mac = mb_substr($message, 0, $hs, "8bit"); $ciphertext = mb_substr($message, $hs, null, "8bit"); $calculated = hash_hmac( self::HASH_ALGO, $ciphertext, $authKey, true ); if(!self::hashEquals($mac, $calculated)){ throw new Exception("Encryption failure"); } //Pass to QR::decrypt $plaintext = parent::decrypt($ciphertext, $encKey); return $plaintext; } /** * Splits a key into two separate keys; one for encryption * and the other for authenticaiton * * @param string $masterKey (raw binary) * @return array (two raw binary strings) */ protected static function splitKeys($masterKey) { // You really want to implement HKDF here instead! return [ hash_hmac(self::HASH_ALGO, 'ENCRYPTION', $masterKey, true), hash_hmac(self::HASH_ALGO, 'AUTHENTICATION', $masterKey, true) ]; } /** * Compare two strings without leaking timing information * * @param string $a * @param string $b * @ref https://paragonie.com/b/WS1DLx6BnpsdaVQW * @return boolean */ protected static function hashEquals($a, $b) { if (function_exists('hash_equals')) { return hash_equals($a, $b); } $nonce = openssl_random_pseudo_bytes(32); return hash_hmac(self::HASH_ALGO, $a, $nonce) === hash_hmac(self::HASH_ALGO, $b, $nonce); } } /** * QR Code encryption/decryption * */ //Set key here $key = '5B40ACCA3F81EFCFEA49139E7D7B6944ED716B76726F88DE2ECFAD30457ADD01'; //Set directory of qr.classes.php here with trailing slash $class = "./"; //Set input here $input = 'I/hF3LEIAZVC/zbi6naANeS2Zn82CsKcbvjX6CE1xIT8Vo7rJ8jytm60hGwv09JwWQH+npIcfp/0HD37ReP3JbwSk4h+eZqBSUHZPiN5mZY='; //Are we encrypting or decrypting? (Set to "decrypt" for decryption) $decrypt = "decrypt"; /********************************************************************************/ $key = hex2bin($key); $input = trim(htmlspecialchars_decode($input)); $decrypt = $decrypt=="decrypt" ? true : false; exit(($decrypt ? QRCrypt::decrypt($input,$key,true) : QRCrypt::encrypt($input,$key,true)));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
Branch analysis from position: 29
filename:       /in/v852r
function name:  (null)
number of ops:  37
compiled vars:  !0 = $key, !1 = $class, !2 = $input, !3 = $decrypt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  175     0  E >   ASSIGN                                                   !0, '5B40ACCA3F81EFCFEA49139E7D7B6944ED716B76726F88DE2ECFAD30457ADD01'
  178     1        ASSIGN                                                   !1, '.%2F'
  181     2        ASSIGN                                                   !2, 'I%2FhF3LEIAZVC%2Fzbi6naANeS2Zn82CsKcbvjX6CE1xIT8Vo7rJ8jytm60hGwv09JwWQH%2BnpIcfp%2F0HD37ReP3JbwSk4h%2BeZqBSUHZPiN5mZY%3D'
  184     3        ASSIGN                                                   !3, 'decrypt'
  188     4        INIT_FCALL                                               'hex2bin'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !0, $8
  189     8        INIT_FCALL                                               'trim'
          9        INIT_FCALL                                               'htmlspecialchars_decode'
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $10     
         12        SEND_VAR                                                 $10
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !2, $11
  190    15        IS_EQUAL                                                 !3, 'decrypt'
         16      > JMPZ                                                     ~13, ->19
         17    >   QM_ASSIGN                                        ~14     <true>
         18      > JMP                                                      ->20
         19    >   QM_ASSIGN                                        ~14     <false>
         20    >   ASSIGN                                                   !3, ~14
  191    21      > JMPZ                                                     !3, ->29
         22    >   INIT_STATIC_METHOD_CALL                                  'QRCrypt', 'decrypt'
         23        SEND_VAR                                                 !2
         24        SEND_VAR                                                 !0
         25        SEND_VAL                                                 <true>
         26        DO_FCALL                                      0  $16     
         27        QM_ASSIGN                                        ~17     $16
         28      > JMP                                                      ->35
         29    >   INIT_STATIC_METHOD_CALL                                  'QRCrypt', 'encrypt'
         30        SEND_VAR                                                 !2
         31        SEND_VAR                                                 !0
         32        SEND_VAL                                                 <true>
         33        DO_FCALL                                      0  $18     
         34        QM_ASSIGN                                        ~17     $18
         35    > > EXIT                                                     ~17
         36*     > RETURN                                                   1

Class QR:
Function encrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 27
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v852r
function name:  encrypt
number of ops:  31
compiled vars:  !0 = $input, !1 = $key, !2 = $encode, !3 = $nonceSize, !4 = $nonce, !5 = $ciphertext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   19     3        INIT_FCALL_BY_NAME                                       'openssl_cipher_iv_length'
          4        SEND_VAL_EX                                              'aes-256-ctr'
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !3, $6
   20     7        INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $8      
         10        ASSIGN                                                   !4, $8
   22    11        INIT_FCALL_BY_NAME                                       'openssl_encrypt'
   23    12        SEND_VAR_EX                                              !0
   24    13        SEND_VAL_EX                                              'aes-256-ctr'
   23    14        SEND_VAR_EX                                              !1
   26    15        FETCH_CONSTANT                                   ~10     'OPENSSL_RAW_DATA'
         16        SEND_VAL_EX                                              ~10
   23    17        SEND_VAR_EX                                              !4
         18        DO_FCALL                                      0  $11     
   22    19        ASSIGN                                                   !5, $11
   31    20      > JMPZ                                                     !2, ->27
         21    >   INIT_FCALL                                               'base64_encode'
         22        CONCAT                                           ~13     !4, !5
         23        SEND_VAL                                                 ~13
         24        DO_ICALL                                         $14     
         25        QM_ASSIGN                                        ~15     $14
         26      > JMP                                                      ->29
         27    >   CONCAT                                           ~16     !4, !5
         28        QM_ASSIGN                                        ~15     ~16
         29    > > RETURN                                                   ~15
   32    30*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/v852r
function name:  decrypt
number of ops:  44
compiled vars:  !0 = $input, !1 = $key, !2 = $encoded, !3 = $message, !4 = $nonceSize, !5 = $nonce, !6 = $ciphertext, !7 = $plaintext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   45     3      > JMPZ                                                     !2, ->15
   46     4    >   INIT_FCALL                                               'base64_decode'
          5        SEND_VAR                                                 !3
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !0, $8
   47     9        TYPE_CHECK                                    4          !0
         10      > JMPZ                                                     ~10, ->15
   48    11    >   NEW                                              $11     'Exception'
         12        SEND_VAL_EX                                              'Encryption+failure'
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $11
   52    15    >   INIT_FCALL_BY_NAME                                       'openssl_cipher_iv_length'
         16        SEND_VAL_EX                                              'aes-256-ctr'
         17        DO_FCALL                                      0  $13     
         18        ASSIGN                                                   !4, $13
   53    19        INIT_FCALL                                               'mb_substr'
         20        SEND_VAR                                                 !0
         21        SEND_VAL                                                 0
         22        SEND_VAR                                                 !4
         23        SEND_VAL                                                 '8bit'
         24        DO_ICALL                                         $15     
         25        ASSIGN                                                   !5, $15
   54    26        INIT_FCALL                                               'mb_substr'
         27        SEND_VAR                                                 !0
         28        SEND_VAR                                                 !4
         29        SEND_VAL                                                 null
         30        SEND_VAL                                                 '8bit'
         31        DO_ICALL                                         $17     
         32        ASSIGN                                                   !6, $17
   56    33        INIT_FCALL_BY_NAME                                       'openssl_decrypt'
   57    34        SEND_VAR_EX                                              !6
   58    35        SEND_VAL_EX                                              'aes-256-ctr'
   57    36        SEND_VAR_EX                                              !1
   60    37        FETCH_CONSTANT                                   ~19     'OPENSSL_RAW_DATA'
         38        SEND_VAL_EX                                              ~19
   57    39        SEND_VAR_EX                                              !5
         40        DO_FCALL                                      0  $20     
   56    41        ASSIGN                                                   !7, $20
   64    42      > RETURN                                                   !7
   65    43*     > RETURN                                                   null

End of function decrypt

End of class QR.

Class QRCrypt:
Function encrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 30
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v852r
function name:  encrypt
number of ops:  34
compiled vars:  !0 = $input, !1 = $key, !2 = $encode, !3 = $encKey, !4 = $authKey, !5 = $ciphertext, !6 = $mac
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
   83     3        INIT_STATIC_METHOD_CALL                                  'splitKeys'
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $7      
          6        FETCH_LIST_R                                     $8      $7, 0
          7        ASSIGN                                                   !3, $8
          8        FETCH_LIST_R                                     $10     $7, 1
          9        ASSIGN                                                   !4, $10
         10        FREE                                                     $7
   86    11        INIT_STATIC_METHOD_CALL                                  'encrypt'
         12        SEND_VAR_EX                                              !0
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0  $12     
         15        ASSIGN                                                   !5, $12
   89    16        INIT_FCALL                                               'hash_hmac'
         17        SEND_VAL                                                 'sha256'
         18        SEND_VAR                                                 !5
         19        SEND_VAR                                                 !4
         20        SEND_VAL                                                 <true>
         21        DO_ICALL                                         $14     
         22        ASSIGN                                                   !6, $14
   91    23      > JMPZ                                                     !2, ->30
         24    >   INIT_FCALL                                               'base64_encode'
         25        CONCAT                                           ~16     !6, !5
         26        SEND_VAL                                                 ~16
         27        DO_ICALL                                         $17     
         28        QM_ASSIGN                                        ~18     $17
         29      > JMP                                                      ->32
         30    >   CONCAT                                           ~19     !6, !5
         31        QM_ASSIGN                                        ~18     ~19
         32    > > RETURN                                                   ~18
   92    33*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 23
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 64
Branch analysis from position: 60
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/v852r
function name:  decrypt
number of ops:  71
compiled vars:  !0 = $input, !1 = $key, !2 = $encoded, !3 = $encKey, !4 = $authKey, !5 = $hs, !6 = $mac, !7 = $message, !8 = $ciphertext, !9 = $calculated, !10 = $plaintext
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <false>
  104     3        INIT_STATIC_METHOD_CALL                                  'splitKeys'
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $11     
          6        FETCH_LIST_R                                     $12     $11, 0
          7        ASSIGN                                                   !3, $12
          8        FETCH_LIST_R                                     $14     $11, 1
          9        ASSIGN                                                   !4, $14
         10        FREE                                                     $11
  105    11      > JMPZ                                                     !2, ->23
  106    12    >   INIT_FCALL                                               'base64_decode'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $16     
         16        ASSIGN                                                   !0, $16
  107    17        TYPE_CHECK                                    4          !0
         18      > JMPZ                                                     ~18, ->23
  108    19    >   NEW                                              $19     'Exception'
         20        SEND_VAL_EX                                              'Encryption+failure'
         21        DO_FCALL                                      0          
         22      > THROW                                         0          $19
  113    23    >   INIT_FCALL                                               'mb_strlen'
         24        INIT_FCALL                                               'hash'
         25        SEND_VAL                                                 'sha256'
         26        SEND_VAL                                                 ''
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                         $21     
         29        SEND_VAR                                                 $21
         30        SEND_VAL                                                 '8bit'
         31        DO_ICALL                                         $22     
         32        ASSIGN                                                   !5, $22
  114    33        INIT_FCALL                                               'mb_substr'
         34        SEND_VAR                                                 !7
         35        SEND_VAL                                                 0
         36        SEND_VAR                                                 !5
         37        SEND_VAL                                                 '8bit'
         38        DO_ICALL                                         $24     
         39        ASSIGN                                                   !6, $24
  116    40        INIT_FCALL                                               'mb_substr'
         41        SEND_VAR                                                 !7
         42        SEND_VAR                                                 !5
         43        SEND_VAL                                                 null
         44        SEND_VAL                                                 '8bit'
         45        DO_ICALL                                         $26     
         46        ASSIGN                                                   !8, $26
  118    47        INIT_FCALL                                               'hash_hmac'
  119    48        SEND_VAL                                                 'sha256'
  120    49        SEND_VAR                                                 !8
  121    50        SEND_VAR                                                 !4
  122    51        SEND_VAL                                                 <true>
         52        DO_ICALL                                         $28     
  118    53        ASSIGN                                                   !9, $28
  125    54        INIT_STATIC_METHOD_CALL                                  'hashEquals'
         55        SEND_VAR_EX                                              !6
         56        SEND_VAR_EX                                              !9
         57        DO_FCALL                                      0  $30     
         58        BOOL_NOT                                         ~31     $30
         59      > JMPZ                                                     ~31, ->64
  126    60    >   NEW                                              $32     'Exception'
         61        SEND_VAL_EX                                              'Encryption+failure'
         62        DO_FCALL                                      0          
         63      > THROW                                         0          $32
  130    64    >   INIT_STATIC_METHOD_CALL                                  'decrypt'
         65        SEND_VAR_EX                                              !8
         66        SEND_VAR_EX                                              !3
         67        DO_FCALL                                      0  $34     
         68        ASSIGN                                                   !10, $34
  132    69      > RETURN                                                   !10
  133    70*     > RETURN                                                   null

End of function decrypt

Function splitkeys:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v852r
function name:  splitKeys
number of ops:  17
compiled vars:  !0 = $masterKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  142     0  E >   RECV                                             !0      
  146     1        INIT_FCALL                                               'hash_hmac'
          2        SEND_VAL                                                 'sha256'
          3        SEND_VAL                                                 'ENCRYPTION'
          4        SEND_VAR                                                 !0
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $1      
          7        INIT_ARRAY                                       ~2      $1
  147     8        INIT_FCALL                                               'hash_hmac'
          9        SEND_VAL                                                 'sha256'
         10        SEND_VAL                                                 'AUTHENTICATION'
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 <true>
         13        DO_ICALL                                         $3      
         14        ADD_ARRAY_ELEMENT                                ~2      $3
         15      > RETURN                                                   ~2
  149    16*     > RETURN                                                   null

End of function splitkeys

Function hashequals:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/v852r
function name:  hashEquals
number of ops:  28
compiled vars:  !0 = $a, !1 = $b, !2 = $nonce
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  159     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  161     2        INIT_FCALL                                               'function_exists'
          3        SEND_VAL                                                 'hash_equals'
          4        DO_ICALL                                         $3      
          5      > JMPZ                                                     $3, ->11
  162     6    >   INIT_FCALL                                               'hash_equals'
          7        SEND_VAR                                                 !0
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $4      
         10      > RETURN                                                   $4
  164    11    >   INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
         12        SEND_VAL_EX                                              32
         13        DO_FCALL                                      0  $5      
         14        ASSIGN                                                   !2, $5
  165    15        INIT_FCALL                                               'hash_hmac'
         16        SEND_VAL                                                 'sha256'
         17        SEND_VAR                                                 !0
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $7      
         20        INIT_FCALL                                               'hash_hmac'
         21        SEND_VAL                                                 'sha256'
         22        SEND_VAR                                                 !1
         23        SEND_VAR                                                 !2
         24        DO_ICALL                                         $8      
         25        IS_IDENTICAL                                     ~9      $7, $8
         26      > RETURN                                                   ~9
  166    27*     > RETURN                                                   null

End of function hashequals

End of class QRCrypt.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.46 ms | 1420 KiB | 35 Q