The UUID generation algorithms are specified in RFC4122 and I’ve created a static PHP class that implements version 1 which is time based UUID, version 4 which is truly psuedo random UUID and version 3 and 5 which are named based UUID, using either MD5 (version 3) or SHA-1 (version 5).
I know there is a PECL package around the original uuidlib but this class have no external dependencies. The full source is available at the bottom of the page.
The class has the following public functions, their return value depends on the format argument.
Code: Select all
$mixed generate($type, $fmt = self::FMT_BYTE, $node = "", $ns = "")
$mixed convert($uuid, $from, $to)
Code: Select all
UUID_TIME, UUID_NAME_MD5, UUID_RANDOM, UUID_NAME_SHA1
- FMT_BYTE is the default and returns a array of bytes that represents the 128-bit UUID.
- FMT_FIELD returns a associative array with individual fields as the format specified in RFC4122.
- FMT_STRING returns the familiar ASCII representation of a UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx).
- FMT_BINARY returns a raw 128-bit binary representation of the UUID.
UUID version 1 example
xxxxxxxxx-xxxx-1xxx-xxxx-xxxxxxxxxxxx
Version 1 UUIDs are timed based with the node constant. The name space argument is ignored and is not used in the generation. The node identifier (”abcdef” in this case) should identify the node or object, only 6 bytes (or characters) are used from the node id.
Code: Select all
require_once('class.uuid.php');
$str = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING, "abcdef");
print "$str\n";
Code: Select all
1b55e723-578b-4e34-d5cf-616263646566
UUID version 4 example
xxxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx
Version 4 is a fully pseudo random where all fields are randomly generated.
Code: Select all
require_once('class.uuid.php');
$str = UUID::generate(UUID::UUID_RANDOM, UUID::FMT_STRING);
print "$str\n";
Code: Select all
d8988842-43d5-42b3-9049-af4bbc694dbe
xxxxxxxxx-xxxx-3/5xxx-xxxx-xxxxxxxxxxxx
The main difference is that a node id is unique within a name space and a UUID generated from the same node id and name space is always the same. The generation algorithm is the same for both version 3 and 5 with the only difference being the hash method used (MD5 versus SHA1).
This is the example from the RFC.
Code: Select all
require_once('class.uuid.php');
/*
* 6ba7b810-9dad-11d1-80b4-00c04fd430c8 is the DNS name space
*/
$md5 = UUID::generate(UUID::UUID_NAME_MD5, UUID::FMT_STRING,
"www.widgets.com", '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
$sha1 = UUID::generate(UUID::UUID_NAME_SHA1, UUID::FMT_STRING,
"www.widgets.com", '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
print "MD5: $md5\n";
print "SHA1: $sha1\n";
Code: Select all
MD5: e902893a-9d22-3c7e-a7b8-d6e313b71d9f
SHA1: 13726f09-44a9-5eeb-8910-3525a23fb23b
This class have been tested with PHP 5.2.x on both little and big endian machines. While there could be bugs in the algorithm implementation at least they are consistent across platforms.
Files Links
RFC4122