Location: PHPKode > scripts > EPub > Grandt-PHPePub-877312c/lib.uuid.manual.html
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="generator" content="HTML Tidy for Windows (vers 14 February 2006), see www.w3.org" />
<meta />
<title>DrUUID RFC 4122 library manual</title>
<meta name="author" content="J. King" />
<link rel="stylesheet" type="text/css" href="documentation.css" />
<link rel="author" href="http://jkingweb.ca/" />
<link rel="up" href="http://jkingweb.ca/code/" />
</head>
<body>
<h1>DrUUID RFC 4122 library manual</h1>
<div id="preface">
<h2>Preface</h2>
<p>&quot;DrUUID&quot; (<i>&#712;d&#633;&#720;.&#618;d</i>) is an implementation for PHP5 of---and associated API for---the data format described in <a href="http://tools.ietf.org/html/rfc4122">RFC 4122</a>, <i>A Universally Unique IDentifier (UUID) URN Namespace</i>. It is able to mint new UUIDs, import existing UUIDs, extract information from UUIDs, and compare two UUIDs for bit-exact equality.</p>
<p>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.</p>
<p>Questions and comments are very welcome, and should be directed to the author via <a href="http://jkingweb.ca/">his Web site</a>.</p>
<p><a href="http://jkingweb.ca/code/php/lib.uuid/download">Download current version</a> (<a href="http://jkingweb.ca/code/php/lib.uuid/archives">archives</a>)</p>
</div>
<div id="contents">
<h2>Table of contents</h2>
<h3>Prefaces</h3>
<ol>
<li><a href="#preface">Preface</a></li>
<li><a href="#contents">Table of contents</a></li>
<li><a href="#features">Features</a></li>
<li><a href="#requirements">Requirements</a></li>
<li><a href="#conformance">Conformance exceptions</a></li>
</ol>
<h3>Body</h3>
<ol>
<li><a href="#note">Documentation note</a></li>
<li><a href="#api">The API</a>
<ol>
<li><a href="#api-mint">UUID::mint()</a>
<ol>
<li><a href="#api-mint-v1">Version 1</a></li>
<li><a href="#api-mint-v3">Version 3</a></li>
<li><a href="#api-mint-v4">Version 4</a></li>
<li><a href="#api-mint-v5">Version 5</a></li>
</ol>
</li>
<li><a href="#api-import">UUID::import()</a></li>
<li><a href="#api-compare">UUID::compare()</a></li>
</ol>
</li>
<li><a href="#the-object">The UUID object</a>
<ol>
<li><a href="#the-object-properties">Public properties</a></li>
</ol>
</li>
<li><a href="#randomness">Sources for random numbers</a>
<ol>
<li><a href="#randomness-initrandom">UUID::initRandom()</a></li>
</ol>
</li>
</ol>
<h3>Appendices</h3>
<ol>
<li><a href="#append-namespaces">Predefined namespaces</a></li>
<li><a href="#append-credits">Credits and licensing</a></li>
<li><a href="#append-history">Revision history</a></li>
</ol>
</div>
<div id="features">
<h2>Features</h2>
<ul>
<li>Conforms to <a href="http://tools.ietf.org/html/rfc4122">RFC 4122</a> (with <a href="#conformance">exceptions</a>)</li>
<li>Simple API</li>
<li>Able to generate new UUIDs, import existing UUIDs, extract data from UUIDs and compare two UUIDs for equality</li>
<li>Optionally draws random numbers from a system-dependent randomness source</li>
</ul>
</div>
<div id="requirements">
<h2>Requirements</h2>
<ul>
<li>PHP 5.0 or later</li>
</ul>
<p>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.</p>
</div>
<div id="conformance">
<h2>Conformance exceptions</h2>
<ul>
<li>No stable storage is implemented. Consequently clock sequence is random for each Version 1 UUID generated</li>
<li>Random numbers are not guaranteed to be generated from a cryptographically secure source</li>
<li>Conversion of large-integer timestamp on 32-bit systems to and from floating-point may introduce minor rounding errors</li>
</ul>
</div>
<div id="note">
<h2>Documentation note</h2>
<p>This manual often makes references to <dfn>binary and hexdecimal strings</dfn> for input and output. For the sake of simplicity please assume that such strings are always in network order (big-endian).</p>
<p id="note-uuid">For methods accepting <dfn>a UUID</dfn> as a parameter, the UUID may be:</p>
<ul>
<li>in canonical form, with or without curly braces</li>
<li>a 16-byte binary string (fastest)</li>
<li>a 32-character hexadecimal string</li>
<li>an RFC 4122 URN</li>
<li>a <a href="#the-object"><code>UUID</code> object</a></li>
</ul>
<p>This manual often makes reference to <dfn>invalid UUIDs</dfn>. For simplicity this is merely any string more or less than 16 bytes long. DrUUID performs no other validation on UUIDs.</p>
</div>
<div id="api">
<h2>The API</h2>
<p>The core DrUUID API consists of three static methods: <code>UUID::mint()</code>, <code>UUID::import()</code> and <code>UUID::compare()</code>. Of these <code>UUID::mint()</code> and <code>UUID::import()</code> return an instance of the <code>UUID</code> class.</p>
<h3 id="api-mint">UUID::mint()</h3>
<div>
<p class="samp"><samp><var>UUID</var> UUID::mint( [<var>int version</var> [, <var>string node</var> [, <var>mixed namespace</var>]]] )</samp></p>
<p>The <code>UUID::mint()</code> method generates (&quot;mints&quot;, 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.</p>
<h4 id="api-mint-v1">Version 1</h4>
<p class="samp"><samp><var>UUID</var> UUID::mint( <var>void</var> )</samp><br />
<samp><var>UUID</var> UUID::mint( 1 [, <var>string node</var>] )</samp></p>
<p>Version 1 UUIDs (the default type) are generated based on the current time and a MAC address (called a <i>node</i>).</p>
<p>If specified, <var>node</var> 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.</p>
<p>Due to the difficulties inherent in the process from PHP, DrUUID does not attempt to detect the host&#39;s MAC address.</p>
<h4 id="api-mint-v3">Version 3</h4>
<p class="samp"><samp><var>UUID</var> UUID::mint( 3, <var>string name</var>, <var>mixed namespace</var> )</samp></p>
<p>Version 3 UUIDs are generated based upon a an MD5 hash of an arbitrary name and its associated name-space. For example the name &quot;www.example.com&quot; is within the DNS namespace, much as &quot;Canada&quot; is within a name-space of the world&#39;s countries. A name/namespace pair will predictably generate the same UUID.</p>
<p>The <var>name</var> argument is an arbitrary name and should be in a binary form appropriate for <var>namespace</var>. 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.</p>
<p>The <var>namespace</var> argument is itself <a href="#note-uuid">a UUID</a>; invalid UUIDs will throw an exception. A list of provided namespaces is available in <a href="#append-namespaces">appendix A</a>.</p>
<p>Note that the continued use of Version 3 UUIDs is discouraged: Version 5 UUIDs should be used instead whenever possible.</p>
<h4 id="api-mint-v4">Version 4</h4>
<p class="samp"><samp><var>UUID</var> UUID::mint( 4 )</samp></p>
<p>Version 4 UUIDs are generated from random numbers. Save for embedded version information they are completely random.</p>
<h4 id="api-mint-v5">Version 5</h4>
<p class="samp"><samp><var>UUID</var> UUID::mint( 5, <var>string name</var>, <var>mixed namespace</var> )</samp></p>
<p>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 &quot;www.example.com&quot; is within the DNS namespace, much as &quot;Canada&quot; is within a name-space of the world&#39;s countries. A name/namespace pair will predictably generate the same UUID.</p>
<p>The <var>name</var> argument is an arbitrary name and should be in a binary form appropriate for <var>namespace</var>. 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.</p>
<p>The <var>namespace</var> argument is itself <a href="#note-uuid">a UUID</a>; invalid UUIDs will throw an exception. A list of provided namespaces is available in <a href="#append-namespaces">appendix A</a>.</p>
<p>Version 5 UUIDs are preferred over Version 3 UUIDs.</p>
</div>
<h3 id="api-import">UUID::import()</h3>
<div>
<p class="samp"><samp><var>UUID</var> UUID::import( <var>string uuid</var> )</samp></p>
<p>The <code>UUID::import()</code> method imports <a href="#note-uuid">a UUID</a> string as a <code>UUID</code> object. Invalid UUIDs will throw an exception. Note that while it will take a <code>UUID</code> object as input, this is <em>not</em> a no-op.</p>
</div>
<h3 id="api-compare">UUID::compare()</h3>
<div>
<p class="samp"><samp><var>bool</var> UUID::compare( <var>mixed uuid1</var>, <var>mixed uuid2</var> )</samp></p>
<p>The <code>UUID::compare()</code> method compares <a href="#note-uuid">two UUIDs</a> for equivalency. If both UUIDs, as binary numbers, are equal, the method returns <samp>TRUE</samp>. The method will also return <samp>TRUE</samp> if neither arguments is a valid UUID.</p>
<p>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.</p>
</div>
</div>
<div id="the-object">
<h2>The UUID object</h2>
<p><code>UUID</code> objects cannot be instantiated manually; they must be created via <code>UUID::mint()</code> or <code>UUID::import()</code>. When cast to a string a <code>UUID</code> object will be rendered in the canonical string form (eg. <samp>550e8400-e29b-41d4-a716-446655440000</samp>). They have no public methods, but do have a number of public properties:</p>
<h3 id="the-object-properties">Public properties</h3>
<div>
<dl>
<dt>bytes</dt>
<dd>A 16-byte binary string representation of the UUID.</dd>
<dt>hex</dt>
<dd>A 32-character hexadecimal representation of the UUID. Neither octets nor fields are ever padded and high digits are always lowercased.</dd>
<dt>string</dt>
<dd>The canonical string representation of the UUID, with high hexadecimal digits always lowercased.</dd>
<dt>urn</dt>
<dd>The UUID formatted as an URN.</dd>
<dt>version</dt>
<dd>The UUID&#39;s version (eg. 1, 3, 4, 5).</dd>
<dt>variant</dt>
<dd>The UUID&#39;s variant. For RFC 4122 UUIDs this is always <samp>1</samp></dd>
<dt>node</dt>
<dd>The MAC address associated with the UUID. Only applicable to Version 1 identifiers.</dd>
<dt>time</dt>
<dd>The time at which the UUID was generated, as a floating-point Unix timestamp with microseconds. Only applicable to Version 1 identifiers.</dd>
</dl>
</div>
</div>
<div id="randomness">
<h2>Sources for random numbers</h2>
<p>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 <code>mt_rand()</code> 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 <code>/dev/urandom</code> on Unix systems or <a href="http://msdn.microsoft.com/en-us/library/aa388182(VS.85).aspx">CAPICOM&#39;s GetRandom method</a> on Windows; other sources may be added in future. In order to use an alternative source the <code>UUID::initRandom()</code> static method must be invoked.</p>
<h3 id="randomness-initrandom">UUID::initRandom()</h3>
<div>
<p class="samp"><samp><var>string</var> UUID::initRandom( <var>void</var> )</samp></p>
<p>The <code>UUID::initRandom()</code> method need only be called once; subsequent calls are harmless other than incurring the same performance penalty each time.</p>
<p>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.</p>
</div>
<p>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 <code>UUID::initRandom()</code> and calls thereafter to <code>UUID::randomBytes()</code> warrant an alternative source&#39;s use.</p>
</div>
<div id="append-namespaces">
<h2>Predefined namespaces</h2>
<p><a href="http://tools.ietf.org/html/rfc4122#appendix-C">Appendix C of RFC 4122</a> lists a number of &quot;potentially interesting&quot; namespaces. For convenience DrUUID includes these namespaces as class constants:</p>
<table>
<caption>Namespace constants</caption>
<thead>
<tr>
<th>Constant</th>
<th>Namespace description</th>
<th>UUID</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>UUID::nsDNS</code></td>
<td>DNS hostnames (eg. &quot;www.example.com&quot;)</td>
<td><samp>6ba7b810-9dad-11d1-80b4-00c04fd430c8</samp></td>
</tr>
<tr>
<td><code>UUID::nsURL</code></td>
<td>Any valid URL (eg. &quot;http://www.example.com/example.html&quot;)</td>
<td><samp>6ba7b811-9dad-11d1-80b4-00c04fd430c8</samp></td>
</tr>
<tr>
<td><code>UUID::nsOID</code></td>
<td>An ISO Object Identifier</td>
<td><samp>6ba7b812-9dad-11d1-80b4-00c04fd430c8</samp></td>
</tr>
<tr>
<td><code>UUID::nsX500</code></td>
<td>An X.500 Distinguished Name</td>
<td><samp>6ba7b814-9dad-11d1-80b4-00c04fd430c8</samp></td>
</tr>
</tbody>
</table>
</div>
<div id="append-credits">
<h2>Credits and licensing</h2>
<p>DrUUID and its manual (ie. this document) were written by <a href="http://jkingweb.ca/">J. King</a>. They are both covered by the following license:</p>
<pre>
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 &quot;Software&quot;), 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 &quot;AS IS&quot;, 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.
</pre>
<p>This manual&#39;s stylesheet was written by <a href="http://dustinwilson.com/">Dustin Wilson</a>. It is licensed under the <a href="http://creativecommons.org/licenses/by/2.5/">Creative Commons Attribution</a> license (v2.5).</p>
<p>This software is dedicated to <a href="http://www.insani.org/">Seung Park</a>. HLN <em>forever</em>!</p>
</div>
<h2 id="append-history">Revision history</h2>
<dl>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20100215">2010-02-15</a></dt>
<dd>Fixed bug in <code>UUID::import</code> as reported by Sander van Lambalgen.</dd>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20091126">2009-11-26</a></dt>
<dd>Fixed previously non-functional <code>UUID::compare()</code> method. Also allowed input UUIDs to be RFC 4122 URNs.</dd>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20091111">2009-11-11</a></dt>
<dd>Various changes:
<ul>
<li>Implemented the <code>UUID::initRandom()</code> method. See <a href="#randomness">Section 4</a> for details. As a consequence generating random numbers is now much faster. Acknowledgement to Rubén Marrero for the impulse to implement this.</li>
<li>Implemented system-level randomness source for Windows.</li>
<li>Fixed a minor unnecessary processing branch when attempting to retrieve node value from non-version 1 UUIDs.</li>
<li>Various documentation corrections and clarifications; numerous fixed typos.</li>
</ul>
</dd>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20090928">2009-09-28</a></dt>
<dd>Fixed a minor bug preventing <code>/dev/urandom</code> from being used. Reported by Rubén Marrero.</dd>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20090413">2009-04-13</a></dt>
<dd>Fixed two serious bugs in Version 5 generation and string casting.</dd>
<dt><a href="http://jkingweb.ca/code/php/lib.uuid/archives?rev=20090411">2009-04-11</a></dt>
<dd>First release.</dd>
</dl>
</body>
</html>
Return current item: EPub