3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Crypt { /* Key: Next prime greater than 62 ^ n / 1.618033988749894848 */ /* Value: modular multiplicative inverse */ private static $golden_primes = array( '1' => '1', '41' => '59', '2377' => '1677', '147299' => '187507', '9132313' => '5952585', '566201239' => '643566407', '35104476161' => '22071637057', '2176477521929' => '294289236153', '134941606358731' => '88879354792675', '8366379594239857' => '7275288500431249', '518715534842869223' => '280042546585394647' ); /* Ascii : 0 9, A Z, a z */ /* $chars = array_merge(range(48,57), range(65,90), range(97,122)) */ private static $chars62 = array( 0=>48,1=>49,2=>50,3=>51,4=>52,5=>53,6=>54,7=>55,8=>56,9=>57,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,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 ); public static function base62($int) { $key = ""; while(bccomp($int, 0) > 0) { $mod = bcmod($int, 62); $key .= chr(self::$chars62[$mod]); $int = bcdiv($int, 62); } return strrev($key); } public static function hash($num, $len = 5) { $ceil = bcpow(62, $len); $primes = array_keys(self::$golden_primes); $prime = $primes[$len]; $dec = bcmod(bcmul($num, $prime), $ceil); $hash = self::base62($dec); return str_pad($hash, $len, "0", STR_PAD_LEFT); } public static function unbase62($key) { $int = 0; foreach(str_split(strrev($key)) as $i => $char) { $dec = array_search(ord($char), self::$chars62); $int = bcadd(bcmul($dec, bcpow(62, $i)), $int); } return $int; } public static function unhash($hash) { $len = strlen($hash); $ceil = bcpow(62, $len); $mmiprimes = array_values(self::$golden_primes); $mmi = $mmiprimes[$len]; $num = self::unbase62($hash); $dec = bcmod(bcmul($num, $mmi), $ceil); return $dec; } } $hashParams = array( 'plopi' => 'gato', 'token' => 'lol' ); var_dump($hashParams); $hashParams = 46521; $strEncoded = Crypt::hash(json_encode($hashParams)); echo "\n encoded\t:" . json_encode($hashParams). "\n"; 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/3Bat9
function name:  (null)
number of ops:  38
compiled vars:  !0 = $hashParams, !1 = $strEncoded
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   ASSIGN                                                   !0, <array>
   79     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                                 
   81     4        ASSIGN                                                   !0, 46521
   84     5        INIT_STATIC_METHOD_CALL                                  'Crypt', 'hash'
          6        INIT_FCALL                                               'json_encode'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $5      
          9        SEND_VAR                                                 $5
         10        DO_FCALL                                      0  $6      
         11        ASSIGN                                                   !1, $6
   86    12        INIT_FCALL                                               'json_encode'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $8      
         15        CONCAT                                           ~9      '%0A+encoded%09%3A', $8
         16        CONCAT                                           ~10     ~9, '%0A'
         17        ECHO                                                     ~10
   87    18        CONCAT                                           ~11     '%0A+encoded%09%3A', !1
         19        CONCAT                                           ~12     ~11, '%0A'
         20        ECHO                                                     ~12
   88    21        INIT_STATIC_METHOD_CALL                                  'Crypt', 'unhash'
         22        SEND_VAR                                                 !1
         23        DO_FCALL                                      0  $13     
         24        CONCAT                                           ~14     '%0A+decode+encoded%09%3A', $13
         25        CONCAT                                           ~15     ~14, '%0A'
         26        ECHO                                                     ~15
   90    27        INIT_FCALL                                               'var_dump'
         28        INIT_FCALL                                               'json_decode'
         29        INIT_STATIC_METHOD_CALL                                  'Crypt', 'unhash'
         30        SEND_VAR                                                 !1
         31        DO_FCALL                                      0  $16     
         32        SEND_VAR                                                 $16
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $17     
         35        SEND_VAR                                                 $17
         36        DO_ICALL                                                 
         37      > RETURN                                                   1

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

End of function base62

Function hash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Bat9
function name:  hash
number of ops:  35
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
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      5
   46     2        INIT_FCALL_BY_NAME                                       'bcpow'
          3        SEND_VAL_EX                                              62
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $7      
          6        ASSIGN                                                   !2, $7
   47     7        INIT_FCALL                                               'array_keys'
          8        FETCH_STATIC_PROP_R          unknown             ~9      'golden_primes'
          9        SEND_VAL                                                 ~9
         10        DO_ICALL                                         $10     
         11        ASSIGN                                                   !3, $10
   48    12        FETCH_DIM_R                                      ~12     !3, !1
         13        ASSIGN                                                   !4, ~12
   49    14        INIT_FCALL_BY_NAME                                       'bcmod'
         15        INIT_FCALL_BY_NAME                                       'bcmul'
         16        SEND_VAR_EX                                              !0
         17        SEND_VAR_EX                                              !4
         18        DO_FCALL                                      0  $14     
         19        SEND_VAR_NO_REF_EX                                       $14
         20        SEND_VAR_EX                                              !2
         21        DO_FCALL                                      0  $15     
         22        ASSIGN                                                   !5, $15
   50    23        INIT_STATIC_METHOD_CALL                                  'base62'
         24        SEND_VAR                                                 !5
         25        DO_FCALL                                      0  $17     
         26        ASSIGN                                                   !6, $17
   51    27        INIT_FCALL                                               'str_pad'
         28        SEND_VAR                                                 !6
         29        SEND_VAR                                                 !1
         30        SEND_VAL                                                 '0'
         31        SEND_VAL                                                 0
         32        DO_ICALL                                         $19     
         33      > RETURN                                                   $19
   52    34*     > RETURN                                                   null

End of function hash

Function unbase62:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 34
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 34
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/3Bat9
function name:  unbase62
number of ops:  37
compiled vars:  !0 = $key, !1 = $int, !2 = $char, !3 = $i, !4 = $dec
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        ASSIGN                                                   !1, 0
   56     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, ->34
          9    > > FE_FETCH_R                                       ~9      $8, !2, ->34
         10    >   ASSIGN                                                   !3, ~9
   57    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
   58    20        INIT_FCALL_BY_NAME                                       'bcadd'
         21        INIT_FCALL_BY_NAME                                       'bcmul'
         22        SEND_VAR_EX                                              !4
         23        INIT_FCALL_BY_NAME                                       'bcpow'
         24        SEND_VAL_EX                                              62
         25        SEND_VAR_EX                                              !3
         26        DO_FCALL                                      0  $15     
         27        SEND_VAR_NO_REF_EX                                       $15
         28        DO_FCALL                                      0  $16     
         29        SEND_VAR_NO_REF_EX                                       $16
         30        SEND_VAR_EX                                              !1
         31        DO_FCALL                                      0  $17     
         32        ASSIGN                                                   !1, $17
   56    33      > JMP                                                      ->9
         34    >   FE_FREE                                                  $8
   60    35      > RETURN                                                   !1
   61    36*     > RETURN                                                   null

End of function unbase62

Function unhash:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3Bat9
function name:  unhash
number of ops:  30
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
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   64     1        STRLEN                                           ~7      !0
          2        ASSIGN                                                   !1, ~7
   65     3        INIT_FCALL_BY_NAME                                       'bcpow'
          4        SEND_VAL_EX                                              62
          5        SEND_VAR_EX                                              !1
          6        DO_FCALL                                      0  $9      
          7        ASSIGN                                                   !2, $9
   66     8        INIT_FCALL                                               'array_values'
          9        FETCH_STATIC_PROP_R          unknown             ~11     'golden_primes'
         10        SEND_VAL                                                 ~11
         11        DO_ICALL                                         $12     
         12        ASSIGN                                                   !3, $12
   67    13        FETCH_DIM_R                                      ~14     !3, !1
         14        ASSIGN                                                   !4, ~14
   68    15        INIT_STATIC_METHOD_CALL                                  'unbase62'
         16        SEND_VAR                                                 !0
         17        DO_FCALL                                      0  $16     
         18        ASSIGN                                                   !5, $16
   69    19        INIT_FCALL_BY_NAME                                       'bcmod'
         20        INIT_FCALL_BY_NAME                                       'bcmul'
         21        SEND_VAR_EX                                              !5
         22        SEND_VAR_EX                                              !4
         23        DO_FCALL                                      0  $18     
         24        SEND_VAR_NO_REF_EX                                       $18
         25        SEND_VAR_EX                                              !2
         26        DO_FCALL                                      0  $19     
         27        ASSIGN                                                   !6, $19
   70    28      > RETURN                                                   !6
   71    29*     > RETURN                                                   null

End of function unhash

End of class Crypt.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.5 ms | 1412 KiB | 35 Q