Use random_int with PHP 7

This commit is contained in:
othercorey 2020-05-23 11:12:46 -05:00 committed by GitHub
parent 59e12788fc
commit 01d5d039f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -30,23 +30,24 @@ class CakeText {
* @return string RFC 4122 UUID * @return string RFC 4122 UUID
*/ */
public static function uuid() { public static function uuid() {
$random = function_exists('random_int') ? 'random_int' : 'mt_rand';
return sprintf( return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low" // 32 bits for "time_low"
mt_rand(0, 65535), $random(0, 65535),
mt_rand(0, 65535), $random(0, 65535),
// 16 bits for "time_mid" // 16 bits for "time_mid"
mt_rand(0, 65535), $random(0, 65535),
// 12 bits before the 0100 of (version) 4 for "time_hi_and_version" // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
mt_rand(0, 4095) | 0x4000, $random(0, 4095) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res", // 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low", // 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1 // two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000, $random(0, 0x3fff) | 0x8000,
// 48 bits for "node" // 48 bits for "node"
mt_rand(0, 65535), $random(0, 65535),
mt_rand(0, 65535), $random(0, 65535),
mt_rand(0, 65535) $random(0, 65535)
); );
} }