/** * 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); }
/** * Generate a cryptographically secure random string * @param int $length * @return string */ public function getString($length = 12) { $size = ceil($length / 2); try { if (function_exists('random_bytes')) { $bytes = random_bytes($size); } else { $hash = new PasswordHash(8, false); $bytes = $hash->get_random_bytes($size); } } catch (\Exception $e) { die('Could not generate a random string.'); } return substr(bin2hex($bytes), 0, $length); }