Last active
February 4, 2022 04:14
-
-
Save anzz1/29a787fac8ef6d693b41779911287505 to your computer and use it in GitHub Desktop.
compat_random.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bytelen($str) { | |
if (defined('MB_OVERLOAD_STRING') && (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING) && extension_loaded('mbstring')) { | |
return mb_strlen($str, '8bit'); | |
} else { | |
return strlen($str); | |
} | |
} | |
if (!is_callable('random_bytes')) { | |
if (is_callable('openssl_random_pseudo_bytes')) { | |
function random_bytes($b) { | |
$rand = openssl_random_pseudo_bytes($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else if (is_callable('mcrypt_create_iv')) { | |
if (DIRECTORY_SEPARATOR === '/') { | |
function random_bytes($b) { | |
$rand = mcrypt_create_iv($b, MCRYPT_DEV_URANDOM); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else { | |
function random_bytes($b) { | |
$rand = mcrypt_create_iv($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
} else if (DIRECTORY_SEPARATOR === '/') { | |
$stat = @stat('/dev/urandom'); | |
if ($stat !== false && ($stat['mode'] & 0170000) === 020000) { | |
function random_bytes($b) { | |
$rand = @file_get_contents('/dev/urandom', false, null, 0, $b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
} else if (class_exists('\\COM')) { | |
try { | |
$util = new COM('CAPICOM.Utilities.1'); | |
$method = array($util, 'GetRandom'); | |
if (is_callable($method)) { | |
function random_bytes($b) { | |
$util = new \COM('CAPICOM.Utilities.1'); | |
$rand = base64_decode($util->GetRandom($b,0)); | |
$rand = ($rand !== false) ? str_pad($rand, $b, chr(0)) : false; | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
} catch (Exception $e) { } | |
} | |
if (!is_callable('random_bytes')) { | |
if (is_callable('gmp_random_bits')) { | |
function random_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(gmp_intval(gmp_random_bits(8))); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else if (is_callable('mt_rand')) { | |
function random_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(mt_rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else { | |
function random_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
} | |
} | |
if (!is_callable('random_int')) { | |
if (is_callable('gmp_random_range')) { | |
function random_int($min, $max) { | |
return gmp_intval(gmp_random_range($min, $max)); | |
} | |
} elseif (is_callable('mt_rand')) { | |
function random_int($min, $max) { | |
mt_srand(random_bytes(3)); | |
return mt_rand($min, $max); | |
} | |
} elseif (is_callable('rand')) { | |
function random_int($min, $max) { | |
srand(random_bytes(3)); | |
return rand($min, $max); | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-Type: text/plain"); | |
function bytelen($str) { | |
if (defined('MB_OVERLOAD_STRING') && (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING) && extension_loaded('mbstring')) { | |
return mb_strlen($str, '8bit'); | |
} else { | |
return strlen($str); | |
} | |
} | |
if (is_callable('random_bytes')) { | |
echo "random_bytes: ".random_bytes(32)."\n\n"; | |
} else { | |
echo "random_bytes: not supported\n\n"; | |
} | |
if (is_callable('gmp_random_bits')) { | |
function gmp_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(gmp_intval(gmp_random_bits(8))); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
echo "gmp_random_bits: ".gmp_bytes(32)."\n\n"; | |
} else { | |
echo "gmp_random_bits: not supported\n\n"; | |
} | |
if (is_callable('openssl_random_pseudo_bytes')) { | |
function openssl_bytes($b) { | |
$rand = openssl_random_pseudo_bytes($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
echo "openssl_random_pseudo_bytes: ".openssl_bytes(32)."\n\n"; | |
} else { | |
echo "openssl_random_pseudo_bytes: not supported\n\n"; | |
} | |
if (is_callable('mcrypt_create_iv')) { | |
if (DIRECTORY_SEPARATOR === '/') { | |
function mcrypt_bytes($b) { | |
$rand = mcrypt_create_iv($b, MCRYPT_DEV_URANDOM); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else { | |
function mcrypt_bytes($b) { | |
$rand = mcrypt_create_iv($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
echo "mcrypt_create_iv: ".mcrypt_bytes(32)."\n\n"; | |
} else { | |
echo "mcrypt_create_iv: not supported\n\n"; | |
} | |
if (DIRECTORY_SEPARATOR === '/') { | |
$stat = @stat('/dev/urandom'); | |
if ($stat !== false && ($stat['mode'] & 0170000) === 020000) { | |
function urandom_bytes($b) { | |
$rand = @file_get_contents('/dev/urandom', false, null, 0, $b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
echo "/dev/urandom: ".urandom_bytes(32)."\n\n"; | |
} else { | |
echo "/dev/urandom: not supported\n\n"; | |
} | |
if (class_exists('\\COM')) { | |
try { | |
$util = new COM('CAPICOM.Utilities.1'); | |
$method = array($util, 'GetRandom'); | |
if (is_callable($method)) { | |
function com_bytes($b) { | |
$util = new \COM('CAPICOM.Utilities.1'); | |
$rand = base64_decode($util->GetRandom($b,0)); | |
$rand = ($rand !== false) ? str_pad($rand, $b, chr(0)) : false; | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
echo "CAPICOM: ".com_bytes(32)."\n\n"; | |
} else { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
} catch (Exception $e) { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
} else { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
if (is_callable('mt_rand')) { | |
function mt_rand_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(mt_rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
echo "mt_rand(): ".mt_rand_bytes(32)."\n\n"; | |
} else { | |
echo "mt_rand(): not supported\n\n"; | |
} | |
function rand_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
echo "rand(): ".rand_bytes(32)."\n\n"; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-Type: text/plain"); | |
function bytelen($str) { | |
if (defined('MB_OVERLOAD_STRING') && (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING) && extension_loaded('mbstring')) { | |
return mb_strlen($str, '8bit'); | |
} else { | |
return strlen($str); | |
} | |
} | |
function exectime($name, $func) { | |
$bytes = 512; | |
$start_time = microtime(true); | |
$ret = $func($bytes); | |
$end_time = microtime(true); | |
$execution_time = ($end_time - $start_time); | |
echo "$name (".$execution_time."s): ".bin2hex($ret)."\n\n"; | |
} | |
if (is_callable('random_bytes')) { | |
exectime('random_bytes', 'random_bytes'); | |
} else { | |
echo "random_bytes: not supported\n\n"; | |
} | |
if (is_callable('gmp_random_bits')) { | |
function gmp_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(gmp_intval(gmp_random_bits(8))); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
exectime('gmp_random_bits', 'gmp_bytes'); | |
} else { | |
echo "gmp_random_bits: not supported\n\n"; | |
} | |
if (is_callable('openssl_random_pseudo_bytes')) { | |
function openssl_bytes($b) { | |
$rand = openssl_random_pseudo_bytes($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
exectime('openssl_random_pseudo_bytes', 'openssl_bytes'); | |
} else { | |
echo "openssl_random_pseudo_bytes: not supported\n\n"; | |
} | |
if (is_callable('mcrypt_create_iv')) { | |
if (DIRECTORY_SEPARATOR === '/') { | |
function mcrypt_bytes($b) { | |
$rand = mcrypt_create_iv($b, MCRYPT_DEV_URANDOM); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} else { | |
function mcrypt_bytes($b) { | |
$rand = mcrypt_create_iv($b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
exectime('mcrypt_create_iv', 'mcrypt_bytes'); | |
} else { | |
echo "mcrypt_create_iv: not supported\n\n"; | |
} | |
if (DIRECTORY_SEPARATOR === '/') { | |
$stat = @stat('/dev/urandom'); | |
if ($stat !== false && ($stat['mode'] & 0170000) === 020000) { | |
function urandom_bytes($b) { | |
$rand = @file_get_contents('/dev/urandom', false, null, 0, $b); | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
} | |
exectime('/dev/urandom', 'urandom_bytes'); | |
} else { | |
echo "/dev/urandom: not supported\n\n"; | |
} | |
if (class_exists('\\COM')) { | |
try { | |
$util = new COM('CAPICOM.Utilities.1'); | |
$method = array($util, 'GetRandom'); | |
if (is_callable($method)) { | |
function com_bytes($b) { | |
$util = new \COM('CAPICOM.Utilities.1'); | |
$rand = base64_decode($util->GetRandom($b,0)); | |
$rand = ($rand !== false) ? str_pad($rand, $b, chr(0)) : false; | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
exectime('CAPICOM', 'com_bytes'); | |
} else { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
} catch (Exception $e) { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
} else { | |
echo "CAPICOM: not supported\n\n"; | |
} | |
if (is_callable('mt_rand')) { | |
function mt_rand_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(mt_rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
exectime('mt_rand()', 'mt_rand_bytes'); | |
} else { | |
echo "mt_rand(): not supported\n\n"; | |
} | |
function rand_bytes($b) { | |
$rand = ''; | |
for ($i = 1; $i <= $b; $i++) { | |
$rand .= chr(rand(0,255)); | |
} | |
return ($rand !== false && bytelen($rand) == $b) ? $rand : null; | |
} | |
exectime('rand()', 'rand_bytes'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment