Compare commits
No commits in common. "master" and "1.0" have entirely different histories.
2 changed files with 4 additions and 89 deletions
|
@ -2,9 +2,4 @@
|
||||||
UUID generation in PHP without the Composer fuss. This is a small library for generating UUIDs without having to use a PHP package manager like Composer, it's easy to use and integrates without any fuss.
|
UUID generation in PHP without the Composer fuss. This is a small library for generating UUIDs without having to use a PHP package manager like Composer, it's easy to use and integrates without any fuss.
|
||||||
|
|
||||||
## Supported UUIDs
|
## Supported UUIDs
|
||||||
* UUIDv3
|
|
||||||
* UUIDv4
|
* UUIDv4
|
||||||
* UUIDv5
|
|
||||||
* UUIDv7
|
|
||||||
* UUIDv8 (arbitrary binary data)
|
|
||||||
* custom SHA-256 UUID (similar to UUIDv3/5)
|
|
||||||
|
|
88
uuid.php
88
uuid.php
|
@ -1,99 +1,19 @@
|
||||||
<?php
|
<?php
|
||||||
/* VanillaUUID v1.1 by abbieoverflight.
|
/* VanillaUUID v1.0 by abbieoverflight.
|
||||||
* Licenced under the EUPLv1.2.
|
* Licenced under the EUPLv1.2.
|
||||||
* https://git.colean.cc/threeoh6000/vanillauuid */
|
* https://git.colean.cc/threeoh6000/vanillauuid */
|
||||||
|
function randomLongHex() {
|
||||||
$uuidNamespaceDNS = "6ba7b8109dad11d180b400c04fd430c8";
|
$byt = random_bytes(64);
|
||||||
$uuidNamespaceURL = "6ba7b8119dad11d180b400c04fd430c8";
|
|
||||||
$uuidNamespaceOID = "6ba7b8129dad11d180b400c04fd430c8";
|
|
||||||
$uuidNamespaceX500 = "6ba7b8149dad11d180b400c04fd430c8";
|
|
||||||
|
|
||||||
function randomLongHex($bits = 128) {
|
|
||||||
$byt = random_bytes($bits/8);
|
|
||||||
return bin2hex($byt);
|
return bin2hex($byt);
|
||||||
}
|
}
|
||||||
|
|
||||||
function genUuid3($namespace, $name) {
|
|
||||||
$cleanedNamespace = preg_replace("/[^a-f0-9]/", '', strtolower($namespace));
|
|
||||||
$hash = md5(pack("H*", $cleanedNamespace) . $name);
|
|
||||||
|
|
||||||
$template = $hash;
|
|
||||||
$canonicalVariant = substr($hash, 16, 1);
|
|
||||||
$mathVariant = hexdec($canonicalVariant);
|
|
||||||
$mathVariant = $mathVariant & 0b0011;
|
|
||||||
$mathVariant = $mathVariant + 0b1000;
|
|
||||||
|
|
||||||
$buildup = substr($template, 0, 8) . "-" . substr($template, 8, 4) . "-3" . substr($template, 13, 3) . "-" . dechex($mathVariant) . substr($template, 17, 3) . "-" . substr($template, 20, 12);
|
|
||||||
|
|
||||||
return $buildup;
|
|
||||||
}
|
|
||||||
|
|
||||||
function genUuid4() {
|
function genUuid4() {
|
||||||
$template = randomLongHex();
|
$template = randomLongHex();
|
||||||
$canonicalVariant = substr($template, 16, 1);
|
$buildup = substr($template, 0, 8) . "-" . substr($template, 8, 4) . "-4" . substr($template, 13, 3) . "-1" . substr($template, 17, 3) . "-" . substr($template, 20, 12);
|
||||||
$mathVariant = hexdec($canonicalVariant);
|
|
||||||
$mathVariant = $mathVariant & 0b0011;
|
|
||||||
$mathVariant = $mathVariant + 0b1000;
|
|
||||||
|
|
||||||
$buildup = substr($template, 0, 8) . "-" . substr($template, 8, 4) . "-4" . substr($template, 13, 3) . "-" . dechex($mathVariant) . substr($template, 17, 3) . "-" . substr($template, 20, 12);
|
|
||||||
|
|
||||||
return $buildup;
|
return $buildup;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genUuid5($namespace, $name) {
|
|
||||||
$cleanedNamespace = preg_replace("/[^a-f0-9]/", '', strtolower($namespace));
|
|
||||||
$hash = sha1(pack("H*", $cleanedNamespace) . $name);
|
|
||||||
|
|
||||||
$template = $hash;
|
|
||||||
$canonicalVariant = substr($hash, 16, 1);
|
|
||||||
$mathVariant = hexdec($canonicalVariant);
|
|
||||||
$mathVariant = $mathVariant & 0b0011;
|
|
||||||
$mathVariant = $mathVariant + 0b1000;
|
|
||||||
|
|
||||||
$buildup = substr($template, 0, 8) . "-" . substr($template, 8, 4) . "-5" . substr($template, 13, 3) . "-" . dechex($mathVariant) . substr($template, 17, 3) . "-" . substr($template, 20, 12);
|
|
||||||
|
|
||||||
return $buildup;
|
|
||||||
}
|
|
||||||
|
|
||||||
function genUuid7($milliseconds = NULL) {
|
|
||||||
if($milliseconds == NULL) {
|
|
||||||
$milliseconds = round(microtime(true) * 1000);
|
|
||||||
}
|
|
||||||
$milliseconds = dechex($milliseconds);
|
|
||||||
while(strlen($milliseconds) < 12) {
|
|
||||||
$milliseconds = "0" . $milliseconds;
|
|
||||||
}
|
|
||||||
$template = randomLongHex(74);
|
|
||||||
return substr($milliseconds, -12, 8) . "-" .substr($milliseconds,-4,4) . "-7" . substr($template, 0, 3) . "-" . genVariantBit() . substr($template, 4, 3) . "-" . substr($template, 7, 12);
|
|
||||||
}
|
|
||||||
|
|
||||||
function genUuid8($data) {
|
|
||||||
$hex = bin2hex($data);
|
|
||||||
while(strlen($hex) < 32) {
|
|
||||||
$hex = $hex . "0";
|
|
||||||
}
|
|
||||||
$template = $hex;
|
|
||||||
$canonicalVariant = substr($hex, 16, 1);
|
|
||||||
$mathVariant = hexdec($canonicalVariant);
|
|
||||||
$mathVariant = $mathVariant & 0b0011;
|
|
||||||
$mathVariant = $mathVariant + 0b1000;
|
|
||||||
|
|
||||||
$buildup = substr($template, 0, 8) . "-" . substr($template, 8, 4) . "-8" . substr($template, 13, 3) . "-" . dechex($mathVariant) . substr($template, 17, 3) . "-" . substr($template, 20, 12);
|
|
||||||
|
|
||||||
return $buildup;
|
|
||||||
}
|
|
||||||
|
|
||||||
function genSha256Uuid($namespace, $name){
|
|
||||||
$cleanedNamespace = preg_replace("/[^a-f0-9]/", '', strtolower($namespace));
|
|
||||||
return genUuid8(hash("sha256", pack("H*", $cleanedNamespace) . $name, true));
|
|
||||||
}
|
|
||||||
|
|
||||||
function genVariantBit() {
|
|
||||||
$ri = random_int(0b1000, 0b1011);
|
|
||||||
$encoded_byt = dechex((int)$ri);
|
|
||||||
return $encoded_byt;
|
|
||||||
}
|
|
||||||
|
|
||||||
function nilUuid() {
|
function nilUuid() {
|
||||||
return "00000000-0000-0000-0000-000000000000";
|
return "00000000-0000-0000-0000-000000000000";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue