Skip to content

Instantly share code, notes, and snippets.

@Steellgold
Created December 27, 2021 21:15
Show Gist options
  • Save Steellgold/6e35fa696e197f650276b43d03237235 to your computer and use it in GitHub Desktop.
Save Steellgold/6e35fa696e197f650276b43d03237235 to your computer and use it in GitHub Desktop.
<?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