3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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)); }
Output for 7.1.0RC5
string(8) "R(,��./"
Output for 7.1.0RC4
string(8) "%U�v�l�"
Output for 7.1.0RC3
string(8) "�T�D�)W"
Output for 7.1.0RC1
string(8) "�N���T"
Output for 7.1.0beta3
string(8) "�7�Yv�."
Output for 7.1.0beta2
string(8) ") ®*�j�"
Output for 7.1.0beta1
string(8) "i��� >y"
Output for 7.1.0alpha3
string(8) "�c'o���"
Output for 7.1.0alpha2
string(8) "6¶�� �]"
Output for 7.1.0alpha1
string(8) "�gO[^��"
Output for 7.0.10
string(8) "פ�AS�6"
Output for 7.0.9
string(8) "-��Ii�B"
Output for 7.0.8
string(8) "���V;7"
Output for 7.0.7
string(8) "sV!>�;Z"
Output for 7.0.6
string(8) "M���ɳ@"
Output for 7.0.5
string(8) "67kL+"
Output for 7.0.4
string(8) ")�rjւ�"
Output for 7.0.3
string(8) "�c��*j�"
Output for 7.0.2
string(8) "6\Gڎ��0"
Output for 7.0.1
string(8) " ��򯿌�"
Output for 7.0.0
string(8) "��FB"
Output for hhvm-3.12.0
string(8) "��I*�U�"
Output for hhvm-3.10.0
string(8) "���䱗�"
Output for 5.6.25
string(8) "2����C&"
Output for 5.6.24
string(8) "��g 7�"
Output for 5.6.23
string(8) "X� �M��"
Output for 5.6.22
string(8) "�0.䠭�"
Output for 5.6.21
string(8) "��(B�"
Output for 5.6.20
string(8) ""-�Y��9,"
Output for 5.6.19
string(8) "; �(CŠ�"
Output for 5.6.18
string(8) "�/U�����"
Output for 5.6.17
string(8) "�+p�44{�"
Output for 5.6.16
string(8) "�B" "
Output for 5.6.15
string(8) "@,K� ��"
Output for 5.6.14
string(8) "��m漠�"
Output for 5.6.13
string(8) "�s5���"
Output for 5.6.12
string(8) "~�9;�8"
Output for 5.6.11
string(8) "�p���"
Output for 5.6.10
string(8) "��|�fv�z"
Output for 5.6.9
string(8) "7�me<���"
Output for 5.6.8
string(8) "�Ш�̾�."
Output for 5.6.7
string(8) "����@O "
Output for 5.6.6
string(8) "�� �"5�"
Output for 5.6.5
string(8) "�dݾm̀�"
Output for 5.6.4
string(8) "P�f�<]��"
Output for 5.6.3
string(8) "Ϸb�%�9M"
Output for 5.6.2
string(8) "��?����6"
Output for 5.6.1
string(8) "�O?I� E�"
Output for 5.6.0
string(8) "��q^׈<�"
Output for 5.5.38
string(8) "`أ�c( "
Output for 5.5.37
string(8) "bƜ0� "
Output for 5.5.36
string(8) "%�"� �"
Output for 5.5.35
string(8) "���M"
Output for 5.5.34
string(8) "����c���"
Output for 5.5.33
string(8) "�����>�"
Output for 5.5.32
string(8) "ڣt���"
Output for 5.5.31
string(8) "�P@B^�|#"
Output for 5.5.30
string(8) "� T.L�"
Output for 5.5.29
string(8) "�`H���"
Output for 5.5.28
string(8) "�r��� "
Output for 5.5.27
string(8) "��o�Z"
Output for 5.5.26
string(8) "���5�O"
Output for 5.5.25
string(8) "�3�vR4{"
Output for 5.5.24
string(8) "�J���Z�"
Output for 5.5.23
string(8) "���8d�|"
Output for 5.5.22
string(8) "��Ӯ�P"
Output for 5.5.21
string(8) "O���{g"
Output for 5.5.20
string(8) "l���.r"
Output for 5.5.19
string(8) " �e�q��,"
Output for 5.5.18
string(8) "#ND٨� '"
Output for 5.5.16
string(8) "nd�G=�"
Output for 5.5.15
string(8) "<Ȓ�솸"
Output for 5.5.14
string(8) "�F̈́���["
Output for 5.5.13
string(8) "�(Wc"C�"
Output for 5.5.12
string(8) "�E��RK�"
Output for 5.5.11
string(8) "�^�}n"
Output for 5.5.10
string(8) "A`�Ŵ.�"
Output for 5.5.9
string(8) "4U�k��q�"
Output for 5.5.8
string(8) "-D��"
Output for 5.5.7
string(8) "��G8���"
Output for 5.5.6
string(8) "�y����"
Output for 5.5.5
string(8) "i�8ͨ_"
Output for 5.5.4
string(8) "NQ���4J�"
Output for 5.5.3
string(8) "R�~]���2"
Output for 5.5.2
string(8) "�%R+V;"
Output for 5.5.1
string(8) "H��fܱAE"
Output for 5.5.0
string(8) "$D��p�{�"
Output for 5.4.45
string(8) "��TNw�E"
Output for 5.4.44
string(8) "��.�ۙ�j"
Output for 5.4.43
string(8) "1š0}"�"
Output for 5.4.42
string(8) "�ҭӔa"
Output for 5.4.41
string(8) "O� �6�N�"
Output for 5.4.40
string(8) "�WRud^�"
Output for 5.4.39
string(8) "��_|� Ku"
Output for 5.4.38
string(8) "�Ҡ}�1"
Output for 5.4.37
string(8) "�5o��;��"
Output for 5.4.36
string(8) "{�R�ۭ�f"
Output for 5.4.35
string(8) "�����n�m"
Output for 5.4.34
string(8) "�� �S�e"
Output for 5.4.32
string(8) "��Ec�V�s"
Output for 5.4.31
string(8) "��L��$�"
Output for 5.4.30
string(8) ""��A�F�"
Output for 5.4.29
string(8) "�Ou�X릖"
Output for 5.4.28
string(8) ",MHX��"
Output for 5.4.27
string(8) "%��]��"
Output for 5.4.26
string(8) "z$!(�]]3"
Output for 5.4.25
string(8) "��Ɏm(�"
Output for 5.4.24
string(8) "ѿ�@�oN�"
Output for 5.4.23
string(8) "��PjZ�"
Output for 5.4.22
string(8) "��Bt���"
Output for 5.4.21
string(8) "/f��T�/"
Output for 5.4.20
string(8) "<��;y���"
Output for 5.4.19
string(8) "����:u"
Output for 5.4.18
string(8) "Ou}� �~P"
Output for 5.4.17
string(8) "�-���kK�"
Output for 5.4.16
string(8) "�)_;�� "
Output for 5.4.15
string(8) "�|�Mkg�C"
Output for 5.4.14
string(8) "��ܧ��("
Output for 5.4.13
string(8) "�9W�iP$�"
Output for 5.4.12
string(8) "�gL{��*"
Output for 5.4.11
string(8) "��^�%�e"
Output for 5.4.10
string(8) "�7`}rC�@"
Output for 5.4.9
string(8) "ݖ�vu� "
Output for 5.4.8
string(8) "ﶶ�7W<"
Output for 5.4.7
string(8) "����'("
Output for 5.4.6
string(8) "�O�Ś��/"
Output for 5.4.5
string(8) "���UĖ�"
Output for 5.4.4
string(8) "unrW���"
Output for 5.4.3
string(8) "z +�D�X"
Output for 5.4.2
string(8) "�[s�? �E"
Output for 5.4.1
string(8) "���sg�#"
Output for 5.4.0
string(8) "��u���o�"
Output for 5.3.29
string(8) "&��_ʭ "
Output for 5.3.28
string(8) "7���;�T�"
Output for 5.3.27
string(8) "o�ͭ0X�"
Output for 5.3.26
string(8) "/f,�uE�"
Output for 5.3.25
string(8) "���� ��"
Output for 5.3.24
string(8) "{�w�d"
Output for 5.3.23
string(8) "C ��-/�"
Output for 5.3.22
string(8) "1�n���M"
Output for 5.3.21
string(8) "ɨ9E��E�"
Output for 5.3.20
string(8) "L�w�s�"
Output for 5.3.19
string(8) "S�Q�v"
Output for 5.3.18
string(8) "�) ��9U�"
Output for 5.3.17
string(8) "��S󕉻"
Output for 5.3.16
string(8) ",��;+}"
Output for 5.3.15
string(8) "�)�]b"
Output for 5.3.14
string(8) "-� ����"
Output for 5.3.13
string(8) " �#��d�*"
Output for 5.3.12
string(8) "���1�i �"
Output for 5.3.11
string(8) "��޳�T"
Output for 5.3.10
string(8) "��:�7�k�"
Output for 5.3.9
string(8) "m�,G��6"
Output for 5.3.8
string(8) "/��ld"
Output for 5.3.7
string(8) "�c�d"
Output for 5.3.6
string(8) "�M���s0"
Output for 5.3.5
string(8) "��Z�"
Output for 5.3.4
string(8) "�I��S���"
Output for 5.3.3
string(8) "9�3B�g�^"
Output for 5.3.2
string(8) "�ד�]�9"
Output for 5.3.1
string(8) "���J;��"
Output for 5.3.0
string(8) " AY��W"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/qDh1U on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/qDh1U on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/qDh1U on line 8
Process exited with code 255.