3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @file * Contains \Drupal\Component\Utility\Crypt. */ namespace Drupal\Component\Utility { /** * Utility class for cryptographically-secure string handling routines. */ class Crypt { /** * Returns a string of highly randomized bytes (over the full 8-bit range). * * This function is better than simply calling mt_rand() or any other built-in * PHP function because it can return a long string of bytes (compared to < 4 * bytes normally from mt_rand()) and uses the best available pseudo-random * source. * * @param int $count * The number of characters (bytes) to return in the string. * * @return string * A randomly generated string. */ public static function randomBytes($count) { static $random_state, $bytes; // Initialize on the first call. The contents of $_SERVER includes a mix of // user-specific and system information that varies a little with each page. // Further initialize with the somewhat random PHP process ID. if (!isset($random_state)) { $random_state = print_r($_SERVER, TRUE) . getmypid(); $bytes = ''; } if (strlen($bytes) < $count) { // /dev/urandom is available on many *nix systems and is considered the // best commonly available pseudo-random source. if ($fh = @fopen('/dev/urandom', 'rb')) { // PHP only performs buffered reads, so in reality it will always read // at least 4096 bytes. Thus, it costs nothing extra to read and store // that much so as to speed any additional invocations. $bytes .= fread($fh, max(4096, $count)); fclose($fh); } // openssl_random_pseudo_bytes() will find entropy in a system-dependent // way. elseif (function_exists('openssl_random_pseudo_bytes')) { $bytes .= openssl_random_pseudo_bytes($count - strlen($bytes)); } // If /dev/urandom is not available or returns no bytes, this loop will // generate a good set of pseudo-random bytes on any system. // Note that it may be important that our $random_state is passed // through hash() prior to being rolled into $output, that the two hash() // invocations are different, and that the extra input into the first one - // the microtime() - is prepended rather than appended. This is to avoid // directly leaking $random_state via the $output stream, which could // allow for trivial prediction of further "random" numbers. while (strlen($bytes) < $count) { $random_state = hash('sha256', microtime() . mt_rand() . $random_state); $bytes .= hash('sha256', mt_rand() . $random_state, TRUE); } } $output = substr($bytes, 0, $count); $bytes = substr($bytes, $count); return $output; } /** * Calculates a base-64 encoded, URL-safe sha-256 hmac. * * @param mixed $data * Scalar value to be validated with the hmac. * @param mixed $key * A secret key, this can be any scalar value. * * @return string * A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and * any = padding characters removed. */ public static function hmacBase64($data, $key) { // $data and $key being strings here is necessary to avoid empty string // results of the hash function if they are not scalar values. As this // function is used in security-critical contexts like token validation it // is important that it never returns an empty string. if (!is_scalar($data) || !is_scalar($key)) { throw new \InvalidArgumentException('Both parameters passed to \Drupal\Component\Utility\Crypt::hmacBase64 must be scalar values.'); } $hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE)); // Modify the hmac so it's safe to use in URLs. return strtr($hmac, array('+' => '-', '/' => '_', '=' => '')); } /** * Calculates a base-64 encoded, URL-safe sha-256 hash. * * @param string $data * String to be hashed. * * @return string * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and * any = padding characters removed. */ public static function hashBase64($data) { $hash = base64_encode(hash('sha256', $data, TRUE)); // Modify the hash so it's safe to use in URLs. return strtr($hash, array('+' => '-', '/' => '_', '=' => '')); } /** * Generates a random, base-64 encoded, URL-safe, sha-256 hashed string. * * @param int $count * The number of characters (bytes) of the string to be hashed. * * @return string * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and * any = padding characters removed. * * @see \Drupal\Component\Utility\Crypt::randomBytes() * @see \Drupal\Component\Utility\Crypt::hashBase64() */ public static function randomStringHashed($count) { return static::hashBase64(static::randomBytes($count)); } } } namespace { var_dump(\Drupal\Component\Utility\Crypt::randomBytes(8)); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qDh1U
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  134     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_STATIC_METHOD_CALL                                  'Drupal%5CComponent%5CUtility%5CCrypt', 'randomBytes'
          2        SEND_VAL                                                 8
          3        DO_FCALL                                      0  $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                                 
  135     6      > RETURN                                                   1

Class Drupal\Component\Utility\Crypt:
Function randombytes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 17
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 81
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 43
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 56
Branch analysis from position: 81
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 56
Branch analysis from position: 81
Branch analysis from position: 56
Branch analysis from position: 43
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 55
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
Branch analysis from position: 55
Branch analysis from position: 81
Branch analysis from position: 17
filename:       /in/qDh1U
function name:  randomBytes
number of ops:  94
compiled vars:  !0 = $count, !1 = $random_state, !2 = $bytes, !3 = $fh, !4 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   34     3        ISSET_ISEMPTY_CV                                 ~5      !1
          4        BOOL_NOT                                         ~6      ~5
          5      > JMPZ                                                     ~6, ->17
   35     6    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cprint_r'
          7        CHECK_FUNC_ARG                                           
          8        FETCH_FUNC_ARG               global              $7      '_SERVER'
          9        SEND_FUNC_ARG                                            $7
         10        SEND_VAL_EX                                              <true>
         11        DO_FCALL                                      0  $8      
         12        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cgetmypid'
         13        DO_FCALL                                      0  $9      
         14        CONCAT                                           ~10     $8, $9
         15        ASSIGN                                                   !1, ~10
   36    16        ASSIGN                                                   !2, ''
   38    17    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cstrlen'
         18        SEND_VAR_EX                                              !2
         19        DO_FCALL                                      0  $13     
         20        IS_SMALLER                                               $13, !0
         21      > JMPZ                                                     ~14, ->81
   41    22    >   BEGIN_SILENCE                                    ~15     
         23        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cfopen'
         24        SEND_VAL_EX                                              '%2Fdev%2Furandom'
         25        SEND_VAL_EX                                              'rb'
         26        DO_FCALL                                      0  $16     
         27        END_SILENCE                                              ~15
         28        ASSIGN                                           ~17     !3, $16
         29      > JMPZ                                                     ~17, ->43
   45    30    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cfread'
         31        SEND_VAR_EX                                              !3
         32        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cmax'
         33        SEND_VAL_EX                                              4096
         34        SEND_VAR_EX                                              !0
         35        DO_FCALL                                      0  $18     
         36        SEND_VAR_NO_REF_EX                                       $18
         37        DO_FCALL                                      0  $19     
         38        ASSIGN_OP                                     8          !2, $19
   46    39        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cfclose'
         40        SEND_VAR_EX                                              !3
         41        DO_FCALL                                      0          
         42      > JMP                                                      ->55
   50    43    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cfunction_exists'
         44        SEND_VAL_EX                                              'openssl_random_pseudo_bytes'
         45        DO_FCALL                                      0  $22     
         46      > JMPZ                                                     $22, ->55
   51    47    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Copenssl_random_pseudo_bytes'
         48        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cstrlen'
         49        SEND_VAR_EX                                              !2
         50        DO_FCALL                                      0  $23     
         51        SUB                                              ~24     !0, $23
         52        SEND_VAL_EX                                              ~24
         53        DO_FCALL                                      0  $25     
         54        ASSIGN_OP                                     8          !2, $25
   61    55    > > JMP                                                      ->76
   62    56    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Chash'
         57        SEND_VAL_EX                                              'sha256'
         58        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cmicrotime'
         59        DO_FCALL                                      0  $27     
         60        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cmt_rand'
         61        DO_FCALL                                      0  $28     
         62        CONCAT                                           ~29     $27, $28
         63        CONCAT                                           ~30     ~29, !1
         64        SEND_VAL_EX                                              ~30
         65        DO_FCALL                                      0  $31     
         66        ASSIGN                                                   !1, $31
   63    67        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Chash'
         68        SEND_VAL_EX                                              'sha256'
         69        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cmt_rand'
         70        DO_FCALL                                      0  $33     
         71        CONCAT                                           ~34     $33, !1
         72        SEND_VAL_EX                                              ~34
         73        SEND_VAL_EX                                              <true>
         74        DO_FCALL                                      0  $35     
         75        ASSIGN_OP                                     8          !2, $35
   61    76    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cstrlen'
         77        SEND_VAR_EX                                              !2
         78        DO_FCALL                                      0  $37     
         79        IS_SMALLER                                               $37, !0
         80      > JMPNZ                                                    ~38, ->56
   66    81    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Csubstr'
         82        SEND_VAR_EX                                              !2
         83        SEND_VAL_EX                                              0
         84        SEND_VAR_EX                                              !0
         85        DO_FCALL                                      0  $39     
         86        ASSIGN                                                   !4, $39
   67    87        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Csubstr'
         88        SEND_VAR_EX                                              !2
         89        SEND_VAR_EX                                              !0
         90        DO_FCALL                                      0  $41     
         91        ASSIGN                                                   !2, $41
   68    92      > RETURN                                                   !4
   69    93*     > RETURN                                                   null

End of function randombytes

Function hmacbase64:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/qDh1U
function name:  hmacBase64
number of ops:  33
compiled vars:  !0 = $data, !1 = $key, !2 = $hmac
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   88     2        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cis_scalar'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $3      
          5        BOOL_NOT                                         ~4      $3
          6      > JMPNZ_EX                                         ~4      ~4, ->12
          7    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cis_scalar'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $5      
         10        BOOL_NOT                                         ~6      $5
         11        BOOL                                             ~4      ~6
         12    > > JMPZ                                                     ~4, ->17
   89    13    >   NEW                                              $7      'InvalidArgumentException'
         14        SEND_VAL_EX                                              'Both+parameters+passed+to+%5CDrupal%5CComponent%5CUtility%5CCrypt%3A%3AhmacBase64+must+be+scalar+values.'
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $7
   92    17    >   INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cbase64_encode'
         18        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Chash_hmac'
         19        SEND_VAL_EX                                              'sha256'
         20        SEND_VAR_EX                                              !0
         21        SEND_VAR_EX                                              !1
         22        SEND_VAL_EX                                              <true>
         23        DO_FCALL                                      0  $9      
         24        SEND_VAR_NO_REF_EX                                       $9
         25        DO_FCALL                                      0  $10     
         26        ASSIGN                                                   !2, $10
   94    27        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cstrtr'
         28        SEND_VAR_EX                                              !2
         29        SEND_VAL_EX                                              <array>
         30        DO_FCALL                                      0  $12     
         31      > RETURN                                                   $12
   95    32*     > RETURN                                                   null

End of function hmacbase64

Function hashbase64:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qDh1U
function name:  hashBase64
number of ops:  16
compiled vars:  !0 = $data, !1 = $hash
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
  108     1        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cbase64_encode'
          2        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Chash'
          3        SEND_VAL_EX                                              'sha256'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAL_EX                                              <true>
          6        DO_FCALL                                      0  $2      
          7        SEND_VAR_NO_REF_EX                                       $2
          8        DO_FCALL                                      0  $3      
          9        ASSIGN                                                   !1, $3
  110    10        INIT_NS_FCALL_BY_NAME                                    'Drupal%5CComponent%5CUtility%5Cstrtr'
         11        SEND_VAR_EX                                              !1
         12        SEND_VAL_EX                                              <array>
         13        DO_FCALL                                      0  $5      
         14      > RETURN                                                   $5
  111    15*     > RETURN                                                   null

End of function hashbase64

Function randomstringhashed:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qDh1U
function name:  randomStringHashed
number of ops:  9
compiled vars:  !0 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  126     0  E >   RECV                                             !0      
  127     1        INIT_STATIC_METHOD_CALL                                  'hashBase64'
          2        INIT_STATIC_METHOD_CALL                                  'randomBytes'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
  128     8*     > RETURN                                                   null

End of function randomstringhashed

End of class Drupal\Component\Utility\Crypt.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
182.79 ms | 1412 KiB | 49 Q