Created
December 27, 2021 21:15
-
-
Save Steellgold/6e35fa696e197f650276b43d03237235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Utils; | |
use Exception; | |
class UUID { | |
/** | |
* @throws Exception | |
*/ | |
public function generate($format = "%s%s-%s-%s-%s-%s%s%s", $length = 4, $random_bytes_length = 16, $data = null): string { | |
$data = $data ?? random_bytes($random_bytes_length); | |
assert(strlen($data) == $random_bytes_length); | |
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); | |
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); | |
return vsprintf($format, str_split(bin2hex($data), $length)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment