Replaced usages of mt_rand by random_int

This commit is contained in:
Alejandro Celaya 2018-10-15 19:35:41 +02:00
parent 5f85c61d6a
commit 9e49604ce2

View File

@ -3,40 +3,44 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Util;
use function random_int;
use function sprintf;
use function strlen;
trait StringUtilsTrait
{
protected function generateRandomString($length = 10)
private function generateRandomString($length = 10): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[mt_rand(0, $charactersLength - 1)];
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
protected function generateV4Uuid()
private function generateV4Uuid(): string
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
random_int(0, 0xffff),
random_int(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
random_int(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
random_int(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,
random_int(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
random_int(0, 0xffff),
random_int(0, 0xffff),
random_int(0, 0xffff)
);
}
}