<?PHP



// Number of times to loop

$loop = 8000 ;

// Functions to benchmark

$funcs = array( 'gen_md5' , 'gen_sha1' , 'gen_sha256' , 'gen_sha512' , 'gen_uniqid' , 'gen_uniqid2' , 'gen_uuid' , 'openssl_random' , 'openssl_random_sha256' , 'get_nonce' , 'get_nonce_ssl' );



// Loop through each function and benchmark it

foreach ( $funcs as $i ) {

$time = bench ( $i , $loop );

$out = call_user_func ( $i );

$len = strlen ( $out );



$data [ $i ] = array( 'time' => $time , 'sample' => $out );

}



$min = 9999999 ;

$max = 0 ;



foreach ( $data as $hash => $i ) {

$time = $i [ 'time' ];



if ( $time < $min ) {

$min = $time ;

$min_hash = $hash ;

} elseif ( $time > $max ) {

$max = $time ;

$max_hash = $hash ;

}

}



foreach ( $data as $hash => $i ) {

$time = $i [ 'time' ];

$sample = $i [ 'sample' ];

$len = strlen ( $sample );

$per = sprintf ( "%0.1f" ,( $time / $min ) * 100 );



if ( $hash === $max_hash ) {

$color = '#FFDBDD' ;

} elseif ( $hash === $min_hash ) {

$color = '#DBFFD1' ;

} else {

$color = 'white' ;

}



print "<div style=\"background-color: $color ; margin-bottom: 0.7em;\">

" ;

print "<div><b> $hash ()</b> x $loop = $time seconds - $per % of fastest</div>

" ;

print "<div> Sample output: <code> $sample </code> ( $len chars)</div>

" ;

print "</div>



" ;

}



print "<br />

" ;



/////////////////////////////////////////////////////////////////////////////



function gen_md5 () {

return md5 ( microtime ( 1 ));

}



function gen_sha1 () {

return sha1 ( microtime ( 1 ));

}



function gen_sha256 () {

#return hash('sha256',openssl_random_pseudo_bytes(16));



return hash ( 'sha256' , microtime ( 1 ));

}



function gen_sha512 () {

return hash ( 'sha512' , microtime ( 1 ));

}



function gen_uniqid () {

return uniqid ( '' );

}



function gen_uniqid2 () {

return uniqid ( '' , true );

}



// From PHP.net in the uniqid() comments

function gen_uuid () {

return sprintf ( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x' ,

// 32 bits for "time_low"

mt_rand ( 0 , 0xffff ), mt_rand ( 0 , 0xffff ),



// 16 bits for "time_mid"

mt_rand ( 0 , 0xffff ),



// 16 bits for "time_hi_and_version",

// four most significant bits holds version number 4

mt_rand ( 0 , 0x0fff ) | 0x4000 ,



// 16 bits, 8 bits for "clk_seq_hi_res",

// 8 bits for "clk_seq_low",

// two most significant bits holds zero and one for variant DCE1.1

mt_rand ( 0 , 0x3fff ) | 0x8000 ,



// 48 bits for "node"

mt_rand ( 0 , 0xffff ), mt_rand ( 0 , 0xffff ), mt_rand ( 0 , 0xffff )

);

}



function openssl_random ( $bytes = 32 ) {

$ret = openssl_random_pseudo_bytes ( $bytes );

$ret = bin2hex ( $ret );



return $ret ;

}



function openssl_random_sha256 ( $bytes = 16 ) {

$ret = openssl_random_pseudo_bytes ( $bytes );

$ret = hash ( 'sha256' , $ret );



return $ret ;

}



// Get a cryptographic nonce

function get_nonce ( $len = 16 ) {

// Some random data

$str = mt_rand () . microtime ();



// Hash and return only X chars

$ret = hash ( 'sha256' , $str );

$ret = substr ( $ret , 0 , $len );



return $ret ;

}



// Get a cryptographic nonce

function get_nonce_ssl ( $len = 8 , $raw = false ) {

$bytes = openssl_random_pseudo_bytes ( $len );



if ( $raw ) {

$ret = $bytes ;

} else {

$ret = bin2hex ( $bytes );

}



return $ret ;

}



////////////////////////////////////////////



function bench ( $func , $times ) {

$start = microtime ( 1 );



for ( $i = 0 ; $i < $times ; $i ++) {

call_user_func ( $func );

}



$end = microtime ( 1 );

$ret = sprintf ( "%0.4f" , $end - $start );



return $ret ;

}



highlight_file ( __FILE__ );



?>

