DrUUID RFC 4122 library manual

Preface

"DrUUID" (ˈdɹː.ɪd) is an implementation for PHP5 of---and associated API for---the data format described in RFC 4122, A Universally Unique IDentifier (UUID) URN Namespace. It is able to mint new UUIDs, import existing UUIDs, extract information from UUIDs, and compare two UUIDs for bit-exact equality.

The API is designed to be as simple as possible, with an implementation as accurate as practical given the limits of PHP. All other concerns, including PHP compatibility, efficiency and extensibility are secondary.

Questions and comments are very welcome, and should be directed to the author via his Web site.

Download current version (archives)

Table of contents

Prefaces

  1. Preface
  2. Table of contents
  3. Features
  4. Requirements
  5. Conformance exceptions

Body

  1. Documentation note
  2. The API
    1. UUID::mint()
      1. Version 1
      2. Version 3
      3. Version 4
      4. Version 5
    2. UUID::import()
    3. UUID::compare()
  3. The UUID object
    1. Public properties
  4. Sources for random numbers
    1. UUID::initRandom()

Appendices

  1. Predefined namespaces
  2. Credits and licensing
  3. Revision history

Features

Requirements

No other requirements are known to me. Behaviour may be erratic with PHP versions earlier than 5.1 due to bugs in string casting for objects.

Conformance exceptions

Documentation note

This manual often makes references to binary and hexdecimal strings for input and output. For the sake of simplicity please assume that such strings are always in network order (big-endian).

For methods accepting a UUID as a parameter, the UUID may be:

This manual often makes reference to invalid UUIDs. For simplicity this is merely any string more or less than 16 bytes long. DrUUID performs no other validation on UUIDs.

The API

The core DrUUID API consists of three static methods: UUID::mint(), UUID::import() and UUID::compare(). Of these UUID::mint() and UUID::import() return an instance of the UUID class.

UUID::mint()

UUID UUID::mint( [int version [, string node [, mixed namespace]]] )

The UUID::mint() method generates ("mints", like coinage) a new UUID. It is capable of producing Version 1 (time-based), Version 3 (MD5 hash-based), Version 4 (random) and Version 5 (SHA-1 hash-based) UUIDs. Its argument list is generic: required and optional argument depend upon the specified version to produce.

Version 1

UUID UUID::mint( void )
UUID UUID::mint( 1 [, string node] )

Version 1 UUIDs (the default type) are generated based on the current time and a MAC address (called a node).

If specified, node should be either an 8-byte binary string or a 16-character hexadecimal string (with or without separators) representing a MAC address. Any other value will generate a random node.

Due to the difficulties inherent in the process from PHP, DrUUID does not attempt to detect the host's MAC address.

Version 3

UUID UUID::mint( 3, string name, mixed namespace )

Version 3 UUIDs are generated based upon a an MD5 hash of an arbitrary name and its associated name-space. For example the name "www.example.com" is within the DNS namespace, much as "Canada" is within a name-space of the world's countries. A name/namespace pair will predictably generate the same UUID.

The name argument is an arbitrary name and should be in a binary form appropriate for namespace. It is the responsibility of the user to assure the proper conversion to binary form. For many namespaces (like the DNS) the appropriate representation is plain text and therefore no conversion is required.

The namespace argument is itself a UUID; invalid UUIDs will throw an exception. A list of provided namespaces is available in appendix A.

Note that the continued use of Version 3 UUIDs is discouraged: Version 5 UUIDs should be used instead whenever possible.

Version 4

UUID UUID::mint( 4 )

Version 4 UUIDs are generated from random numbers. Save for embedded version information they are completely random.

Version 5

UUID UUID::mint( 5, string name, mixed namespace )

Version 5 UUIDs are generated based upon a an SHA-1 hash of an arbitrary name and its associated name-space. For example the name "www.example.com" is within the DNS namespace, much as "Canada" is within a name-space of the world's countries. A name/namespace pair will predictably generate the same UUID.

The name argument is an arbitrary name and should be in a binary form appropriate for namespace. It is the responsibility of the user to assure the proper conversion to binary form. For many namespaces (like the DNS) the appropriate representation is plain text and therefore no conversion is required.

The namespace argument is itself a UUID; invalid UUIDs will throw an exception. A list of provided namespaces is available in appendix A.

Version 5 UUIDs are preferred over Version 3 UUIDs.

UUID::import()

UUID UUID::import( string uuid )

The UUID::import() method imports a UUID string as a UUID object. Invalid UUIDs will throw an exception. Note that while it will take a UUID object as input, this is not a no-op.

UUID::compare()

bool UUID::compare( mixed uuid1, mixed uuid2 )

The UUID::compare() method compares two UUIDs for equivalency. If both UUIDs, as binary numbers, are equal, the method returns TRUE. The method will also return TRUE if neither arguments is a valid UUID.

This method is useful for determining if two different UUID representations (eg. canonical string, lowercase hex string, uppercase hex string, binary, URN) are in fact the same UUID.

The UUID object

UUID objects cannot be instantiated manually; they must be created via UUID::mint() or UUID::import(). When cast to a string a UUID object will be rendered in the canonical string form (eg. 550e8400-e29b-41d4-a716-446655440000). They have no public methods, but do have a number of public properties:

Public properties

bytes
A 16-byte binary string representation of the UUID.
hex
A 32-character hexadecimal representation of the UUID. Neither octets nor fields are ever padded and high digits are always lowercased.
string
The canonical string representation of the UUID, with high hexadecimal digits always lowercased.
urn
The UUID formatted as an URN.
version
The UUID's version (eg. 1, 3, 4, 5).
variant
The UUID's variant. For RFC 4122 UUIDs this is always 1
node
The MAC address associated with the UUID. Only applicable to Version 1 identifiers.
time
The time at which the UUID was generated, as a floating-point Unix timestamp with microseconds. Only applicable to Version 1 identifiers.

Sources for random numbers

PHP has a number of good sources for random numbers available to it, but most are either system-dependent, incur considerable overhead, or both. Consequently DrUUID uses the mt_rand() function to generate random numbers unless instructed to seek an alternative, usually cryptographically secure source. As of this writing random bytes may be drawn from /dev/urandom on Unix systems or CAPICOM's GetRandom method on Windows; other sources may be added in future. In order to use an alternative source the UUID::initRandom() static method must be invoked.

UUID::initRandom()

string UUID::initRandom( void )

The UUID::initRandom() method need only be called once; subsequent calls are harmless other than incurring the same performance penalty each time.

The return value is a string containing the name of the private method which will be used to gather random numbers. It is purely advisory, and return values may change in future versions.

Note that since the characteristics of any given system can be unpredictably different from those of another, users are encouraged to run their own benchmarks to ascertain whether the performance of both UUID::initRandom() and calls thereafter to UUID::randomBytes() warrant an alternative source's use.

Predefined namespaces

Appendix C of RFC 4122 lists a number of "potentially interesting" namespaces. For convenience DrUUID includes these namespaces as class constants:

Namespace constants
Constant Namespace description UUID
UUID::nsDNS DNS hostnames (eg. "www.example.com") 6ba7b810-9dad-11d1-80b4-00c04fd430c8
UUID::nsURL Any valid URL (eg. "http://www.example.com/example.html") 6ba7b811-9dad-11d1-80b4-00c04fd430c8
UUID::nsOID An ISO Object Identifier 6ba7b812-9dad-11d1-80b4-00c04fd430c8
UUID::nsX500 An X.500 Distinguished Name 6ba7b814-9dad-11d1-80b4-00c04fd430c8

Credits and licensing

DrUUID and its manual (ie. this document) were written by J. King. They are both covered by the following license:

Copyright (c) 2009 J. King

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

This manual's stylesheet was written by Dustin Wilson. It is licensed under the Creative Commons Attribution license (v2.5).

This software is dedicated to Seung Park. HLN forever!

Revision history

2010-02-15
Fixed bug in UUID::import as reported by Sander van Lambalgen.
2009-11-26
Fixed previously non-functional UUID::compare() method. Also allowed input UUIDs to be RFC 4122 URNs.
2009-11-11
Various changes:
2009-09-28
Fixed a minor bug preventing /dev/urandom from being used. Reported by Rubén Marrero.
2009-04-13
Fixed two serious bugs in Version 5 generation and string casting.
2009-04-11
First release.