public get_random_bytes ( integer $count ) : String | ||
$count | integer | |
return | String |
public function get_random_bytes($count)
{
$output = '';
if (@is_readable('/dev/urandom') && ($fh = @fopen('/dev/urandom', 'rb'))) {
$output = fread($fh, $count);
fclose($fh);
}
if (strlen($output) < $count) {
$output = '';
for ($i = 0; $i < $count; $i += 16) {
$this->random_state = md5(microtime() . $this->random_state);
$output .= pack('H*', md5($this->random_state));
}
$output = substr($output, 0, $count);
}
return $output;
}
/** * Generate a cryptographically secure random string * @param int $length * @return string */ public function getString($length = 12) { if (function_exists('random_bytes')) { $bytes = random_bytes($length / 2); } else { $hash = new PasswordHash(8, false); $bytes = $hash->get_random_bytes($length / 2); } return bin2hex($bytes); }