3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Crypt { private static $range = 62; private static $goldenPrimes36 = array( /* * Key..: next prime greater than 36 ^ n / 1.618033988749894848 * Value: modular multiplicative inverse */ '1' => '1', '23' => '11', '809' => '809', '28837' => '29485', '1038073' => '179017', '37370153' => '47534873', '1345325473' => '264202849', '48431716939' => '19727015779', '1743541808839' => '1532265214711', '62767505117101' => '67935388019749', '3656158440062987' => '3323780400057251', '131621703842267239' => '14056686818106199' ); private static $goldenPrimes62 = array( /* * Key..: next prime greater than 62 ^ n / 1.618033988749894848 * Value: modular multiplicative inverse */ '1' => '1', '41' => '59', '2377' => '1677', '147299' => '187507', '9132313' => '5952585', '566201239' => '643566407', '35104476161' => '22071637057', '2176477521929' => '294289236153', '134941606358731' => '88879354792675', '8366379594239857' => '7275288500431249', '518715534842869223' => '280042546585394647' ); private static $chars62 = array( // Ascii : 0-9 0 => 48, 1 => 49, 2 => 50, 3 => 51, 4 => 52, 5 => 53, 6 => 54, 7 => 55, 8 => 56, 9 => 57, // Ascii : A-Z 10 => 65, 11 => 66, 12 => 67, 13 => 68, 14 => 69, 15 => 70, 16 => 71, 17 => 72, 18 => 73, 19 => 74, 20 => 75, 21 => 76, 22 => 77, 23 => 78, 24 => 79, 25 => 80, 26 => 81, 27 => 82, 28 => 83, 29 => 84, 30 => 85, 31 => 86, 32 => 87, 33 => 88, 34 => 89, 35 => 90, // Ascii : a-z 36 => 97, 37 => 98, 38 => 99, 39 => 100, 40 => 101, 41 => 102, 42 => 103, 43 => 104, 44 => 105, 45 => 106, 46 => 107, 47 => 108, 48 => 109, 49 => 110, 50 => 111, 51 => 112, 52 => 113, 53 => 114, 54 => 115, 55 => 116, 56 => 117, 57 => 118, 58 => 119, 59 => 120, 60 => 121, 61 => 122 ); /** * Calculates the hash 36 or 62 based range. * @param integer $int * @return string */ private static function baseX($int) { $key = ''; while (bccomp($int, 0) > 0) { $mod = bcmod($int, self::$range); $key .= chr(self::$chars62[$mod]); $int = bcdiv($int, self::$range, 0); } return strrev($key); } /** * Inverse calculation for hashes 36 or 62 based range. * @param string $key * @return integer */ private static function unbaseX($key) { $int = 0; foreach (str_split(strrev($key)) as $i => $char) { $dec = array_search(ord($char), self::$chars62); $int = bcadd(bcmul($dec, bcpow(self::$range, $i)), $int); } return $int; } /** * Generates the hash 62 based chars(a-z, A-Z, 0-9) to a given int number. * Important: * – the biggest cryptable integer with hash length = 5 is 916 132 831. * – the biggest cryptable integer with hash length = 6 is 56 800 235 583. It seems to be enough, no? * * @param integer $num * @param integer $len * @return string */ public static function hash($num, $len = 5) { $ceil = bcpow(self::$range, $len); $primes = array_keys(self::$range === 36 ? self::$goldenPrimes36 : self::$goldenPrimes62); $prime = $primes[$len]; $dec = bcmod(bcmul($num, $prime, 0),$ceil); $hash = self::baseX($dec); return str_pad($hash, $len, '0', STR_PAD_LEFT); } /** * Generates the hash 36 based chars(A-Z, 0-9) to a given int number. * * Important: * * – the biggest cryptable integer with hash length = 5 is 60 466 175. * * – the biggest cryptable integer with hash length = 6 is 2 176 782 335. */ public static function hash36($num, $len = 5) { self::$range = 36; return self::hash($num, $len); } /** * Converts a previous generated hash to its original integer. * @param string $hash * @return integer */ public static function unhash($hash) { $len = strlen($hash); $ceil = bcpow(self::$range, $len); $mmiprimes = array_values(self::$range === 36 ? self::$goldenPrimes36 : self::$goldenPrimes62); $mmi = $mmiprimes[$len]; $num = self::unbaseX($hash); $dec = bcmod(bcmul($num, $mmi), $ceil); return $dec; } } $hashParams = array( 'plopi' => 'gato', 'token' => 'lol' ); var_dump($hashParams); $strEncoded = Crypt::hash(json_encode($hashParams)); echo "\n encoded\t:" . $strEncoded. "\n"; echo "\n decode encoded\t:" . Crypt::unhash($strEncoded)."\n"; var_dump(json_decode(Crypt::unhash($strEncoded), true));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AfQkq
function name:  (null)
number of ops:  31
compiled vars:  !0 = $hashParams, !1 = $strEncoded
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   ASSIGN                                                   !0, <array>
  142     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                                 
  145     4        INIT_STATIC_METHOD_CALL                                  'Crypt', 'hash'
          5        INIT_FCALL                                               'json_encode'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $4      
          8        SEND_VAR                                                 $4
          9        DO_FCALL                                      0  $5      
         10        ASSIGN                                                   !1, $5
  147    11        CONCAT                                           ~7      '%0A+encoded%09%3A', !1
         12        CONCAT                                           ~8      ~7, '%0A'
         13        ECHO                                                     ~8
  148    14        INIT_STATIC_METHOD_CALL                                  'Crypt', 'unhash'
         15        SEND_VAR                                                 !1
         16        DO_FCALL                                      0  $9      
         17        CONCAT                                           ~10     '%0A+decode+encoded%09%3A', $9
         18        CONCAT                                           ~11     ~10, '%0A'
         19        ECHO                                                     ~11
  150    20        INIT_FCALL                                               'var_dump'
         21        INIT_FCALL                                               'json_decode'
         22        INIT_STATIC_METHOD_CALL                                  'Crypt', 'unhash'
         23        SEND_VAR                                                 !1
         24        DO_FCALL                                      0  $12     
         25        SEND_VAR                                                 $12
         26        SEND_VAL                                                 <true>
         27        DO_ICALL                                         $13     
         28        SEND_VAR                                                 $13
         29        DO_ICALL                                                 
         30      > RETURN                                                   1

Class Crypt:
Function basex:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 3
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 3
Branch analysis from position: 30
Branch analysis from position: 3
filename:       /in/AfQkq
function name:  baseX
number of ops:  35
compiled vars:  !0 = $int, !1 = $key, !2 = $mod
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        ASSIGN                                                   !1, ''
   67     2      > JMP                                                      ->24
   68     3    >   INIT_FCALL_BY_NAME                                       'bcmod'
          4        SEND_VAR_EX                                              !0
          5        CHECK_FUNC_ARG                                           
          6        FETCH_STATIC_PROP_FUNC_ARG   unknown             $4      'range'
          7        SEND_FUNC_ARG                                            $4
          8        DO_FCALL                                      0  $5      
          9        ASSIGN                                                   !2, $5
   69    10        INIT_FCALL                                               'chr'
         11        FETCH_STATIC_PROP_R          unknown             ~7      'chars62'
         12        FETCH_DIM_R                                      ~8      ~7, !2
         13        SEND_VAL                                                 ~8
         14        DO_ICALL                                         $9      
         15        ASSIGN_OP                                     8          !1, $9
   70    16        INIT_FCALL_BY_NAME                                       'bcdiv'
         17        SEND_VAR_EX                                              !0
         18        CHECK_FUNC_ARG                                           
         19        FETCH_STATIC_PROP_FUNC_ARG   unknown             $11     'range'
         20        SEND_FUNC_ARG                                            $11
         21        SEND_VAL_EX                                              0
         22        DO_FCALL                                      0  $12     
         23        ASSIGN                                                   !0, $12
   67    24    >   INIT_FCALL_BY_NAME                                       'bccomp'
         25        SEND_VAR_EX                                              !0
         26        SEND_VAL_EX                                              0
         27        DO_FCALL                                      0  $14     
         28        IS_SMALLER                                               0, $14
         29      > JMPNZ                                                    ~15, ->3
   72    30    >   INIT_FCALL                                               'strrev'
         31        SEND_VAR                                                 !1
         32        DO_ICALL                                         $16     
         33      > RETURN                                                   $16
   73    34*     > RETURN                                                   null

End of function basex

Function unbasex:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 36
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 36
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/AfQkq
function name:  unbaseX
number of ops:  39
compiled vars:  !0 = $key, !1 = $int, !2 = $char, !3 = $i, !4 = $dec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   81     1        ASSIGN                                                   !1, 0
   82     2        INIT_FCALL                                               'str_split'
          3        INIT_FCALL                                               'strrev'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $6      
          6        SEND_VAR                                                 $6
          7        DO_ICALL                                         $7      
          8      > FE_RESET_R                                       $8      $7, ->36
          9    > > FE_FETCH_R                                       ~9      $8, !2, ->36
         10    >   ASSIGN                                                   !3, ~9
   83    11        INIT_FCALL                                               'array_search'
         12        INIT_FCALL                                               'ord'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $11     
         15        SEND_VAR                                                 $11
         16        FETCH_STATIC_PROP_R          unknown             ~12     'chars62'
         17        SEND_VAL                                                 ~12
         18        DO_ICALL                                         $13     
         19        ASSIGN                                                   !4, $13
   84    20        INIT_FCALL_BY_NAME                                       'bcadd'
         21        INIT_FCALL_BY_NAME                                       'bcmul'
         22        SEND_VAR_EX                                              !4
         23        INIT_FCALL_BY_NAME                                       'bcpow'
         24        CHECK_FUNC_ARG                                           
         25        FETCH_STATIC_PROP_FUNC_ARG   unknown             $15     'range'
         26        SEND_FUNC_ARG                                            $15
         27        SEND_VAR_EX                                              !3
         28        DO_FCALL                                      0  $16     
         29        SEND_VAR_NO_REF_EX                                       $16
         30        DO_FCALL                                      0  $17     
         31        SEND_VAR_NO_REF_EX                                       $17
         32        SEND_VAR_EX                                              !1
         33        DO_FCALL                                      0  $18     
         34        ASSIGN                                                   !1, $18
   82    35      > JMP                                                      ->9
         36    >   FE_FREE                                                  $8
   86    37      > RETURN                                                   !1
   87    38*     > RETURN                                                   null

End of function unbasex

Function hash:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AfQkq
function name:  hash
number of ops:  45
compiled vars:  !0 = $num, !1 = $len, !2 = $ceil, !3 = $primes, !4 = $prime, !5 = $dec, !6 = $hash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      5
   99     2        INIT_FCALL_BY_NAME                                       'bcpow'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_STATIC_PROP_FUNC_ARG   unknown             $7      'range'
          5        SEND_FUNC_ARG                                            $7
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0  $8      
          8        ASSIGN                                                   !2, $8
  100     9        INIT_FCALL                                               'array_keys'
         10        FETCH_STATIC_PROP_R          unknown             ~10     'range'
         11        IS_IDENTICAL                                             ~10, 36
         12      > JMPZ                                                     ~11, ->16
         13    >   FETCH_STATIC_PROP_R          unknown             ~12     'goldenPrimes36'
         14        QM_ASSIGN                                        ~13     ~12
         15      > JMP                                                      ->18
         16    >   FETCH_STATIC_PROP_R          unknown             ~14     'goldenPrimes62'
         17        QM_ASSIGN                                        ~13     ~14
         18    >   SEND_VAL                                                 ~13
         19        DO_ICALL                                         $15     
         20        ASSIGN                                                   !3, $15
  101    21        FETCH_DIM_R                                      ~17     !3, !1
         22        ASSIGN                                                   !4, ~17
  102    23        INIT_FCALL_BY_NAME                                       'bcmod'
         24        INIT_FCALL_BY_NAME                                       'bcmul'
         25        SEND_VAR_EX                                              !0
         26        SEND_VAR_EX                                              !4
         27        SEND_VAL_EX                                              0
         28        DO_FCALL                                      0  $19     
         29        SEND_VAR_NO_REF_EX                                       $19
         30        SEND_VAR_EX                                              !2
         31        DO_FCALL                                      0  $20     
         32        ASSIGN                                                   !5, $20
  103    33        INIT_STATIC_METHOD_CALL                                  'baseX'
         34        SEND_VAR                                                 !5
         35        DO_FCALL                                      0  $22     
         36        ASSIGN                                                   !6, $22
  104    37        INIT_FCALL                                               'str_pad'
         38        SEND_VAR                                                 !6
         39        SEND_VAR                                                 !1
         40        SEND_VAL                                                 '0'
         41        SEND_VAL                                                 0
         42        DO_ICALL                                         $24     
         43      > RETURN                                                   $24
  105    44*     > RETURN                                                   null

End of function hash

Function hash36:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AfQkq
function name:  hash36
number of ops:  10
compiled vars:  !0 = $num, !1 = $len
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      5
  116     2        ASSIGN_STATIC_PROP                                       'range'
          3        OP_DATA                                                  36
  117     4        INIT_STATIC_METHOD_CALL                                  'hash'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $3      
          8      > RETURN                                                   $3
  118     9*     > RETURN                                                   null

End of function hash36

Function unhash:
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 = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AfQkq
function name:  unhash
number of ops:  39
compiled vars:  !0 = $hash, !1 = $len, !2 = $ceil, !3 = $mmiprimes, !4 = $mmi, !5 = $num, !6 = $dec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   RECV                                             !0      
  126     1        STRLEN                                           ~7      !0
          2        ASSIGN                                                   !1, ~7
  127     3        INIT_FCALL_BY_NAME                                       'bcpow'
          4        CHECK_FUNC_ARG                                           
          5        FETCH_STATIC_PROP_FUNC_ARG   unknown             $9      'range'
          6        SEND_FUNC_ARG                                            $9
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $10     
          9        ASSIGN                                                   !2, $10
  128    10        INIT_FCALL                                               'array_values'
         11        FETCH_STATIC_PROP_R          unknown             ~12     'range'
         12        IS_IDENTICAL                                             ~12, 36
         13      > JMPZ                                                     ~13, ->17
         14    >   FETCH_STATIC_PROP_R          unknown             ~14     'goldenPrimes36'
         15        QM_ASSIGN                                        ~15     ~14
         16      > JMP                                                      ->19
         17    >   FETCH_STATIC_PROP_R          unknown             ~16     'goldenPrimes62'
         18        QM_ASSIGN                                        ~15     ~16
         19    >   SEND_VAL                                                 ~15
         20        DO_ICALL                                         $17     
         21        ASSIGN                                                   !3, $17
  129    22        FETCH_DIM_R                                      ~19     !3, !1
         23        ASSIGN                                                   !4, ~19
  130    24        INIT_STATIC_METHOD_CALL                                  'unbaseX'
         25        SEND_VAR                                                 !0
         26        DO_FCALL                                      0  $21     
         27        ASSIGN                                                   !5, $21
  131    28        INIT_FCALL_BY_NAME                                       'bcmod'
         29        INIT_FCALL_BY_NAME                                       'bcmul'
         30        SEND_VAR_EX                                              !5
         31        SEND_VAR_EX                                              !4
         32        DO_FCALL                                      0  $23     
         33        SEND_VAR_NO_REF_EX                                       $23
         34        SEND_VAR_EX                                              !2
         35        DO_FCALL                                      0  $24     
         36        ASSIGN                                                   !6, $24
  132    37      > RETURN                                                   !6
  133    38*     > RETURN                                                   null

End of function unhash

End of class Crypt.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.59 ms | 1412 KiB | 35 Q