<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>shapeshifter.se &#187; UUID</title>
	<atom:link href="http://www.shapeshifter.se/tag/uuid/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shapeshifter.se</link>
	<description>Mostly miscellaneous technical mumbo-jumbo.</description>
	<lastBuildDate>Sat, 12 Dec 2009 12:00:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>UUID generator for PHP</title>
		<link>http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/</link>
		<comments>http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 18:48:44 +0000</pubDate>
		<dc:creator>fli</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[UUID]]></category>

		<guid isPermaLink="false">http://www.shapeshifter.se/?p=352</guid>
		<description><![CDATA[UUID (Universally Unique Identifier) is standard (part of ISO/IEC 11578:1996) to create &#8220;universally unique&#8221; identifiers to identify objects within a system or across system boundaries. The identifiers are 128-bit in length (that&#8217;s 16 bytes) and while there really is no way to guarantee global uniqueness the probability of colissions are very small both thanks to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Uuid">UUID</a> (Universally Unique Identifier) is standard (part of ISO/IEC 11578:1996) to create &#8220;universally unique&#8221; identifiers to identify objects within a system or across system boundaries. The identifiers are 128-bit in length (that&#8217;s 16 bytes) and while there really is no way to guarantee global uniqueness the probability of colissions are very small both thanks to the number of bits and the way the identifiers are created.</p>
<p>The UUID generation algorithms are specified in <a href="http://tools.ietf.org/html/rfc4122">RFC4122</a> and I&#8217;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).<br />
<span id="more-352"></span><br />
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.</p>
<p><!-- WSA: rules for context 'adsense-post-top' did not apply --></p>
<p>The class has the following public functions, their return value depends on the format argument.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mixed</span> generate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$type</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">FMT_BYTE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ns</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$mixed</span> convert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$uuid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>UUID version constants where each constant represents a specific UUID version</p>
<blockquote>
<pre>UUID_TIME, UUID_NAME_MD5, UUID_RANDOM, UUID_NAME_SHA1</pre>
</blockquote>
<p>UUID format constants</p>
<ul>
<li><em>FMT_BYTE</em> is the default and returns a array of bytes that represents the 128-bit UUID.</li>
<li><em>FMT_FIELD</em> returns a associative array with individual fields as the format specified in RFC4122.</li>
<li><em>FMT_STRING</em> returns the familiar ASCII representation of a UUID (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx).</li>
<li><em>FMT_BINARY</em> returns a raw 128-bit binary representation of the UUID.</li>
</ul>
<p>The <em>convert</em> method can be used to convert between these representations.</p>
<h3>UUID version 1 example</h3>
<p>xxxxxxxxx-xxxx-1xxx-xxxx-xxxxxxxxxxxx<br />
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 (&#8221;abcdef&#8221; in this case) should identify the node or object, only 6 bytes (or characters) are used from the node id.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.uuid.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>UUID<span style="color: #339933;">::</span><span style="color: #004000;">UUID_TIME</span><span style="color: #339933;">,</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">FMT_STRING</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;abcdef&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$str</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Example output, the last part is the node id and will remain constant (as long as the node id is the same), the first part will change according to time while the middle part is pseudo random.</p>
<blockquote>
<pre>1b55e723-578b-4e34-d5cf-616263646566</pre>
</blockquote>
<p>The class does not fully implement version 1 as described in the RFC. The clock sequence is generated from a psuedo random generator each time a new UUID is generated and not written and read from a stable storage as described in the RFC.</p>
<h3>UUID version 4 example</h3>
<p>xxxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx<br />
Version 4 is a fully pseudo random where all fields are randomly generated.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.uuid.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>UUID<span style="color: #339933;">::</span><span style="color: #004000;">UUID_RANDOM</span><span style="color: #339933;">,</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">FMT_STRING</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$str</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Kind of useless example output <img src='http://www.shapeshifter.se/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<blockquote>
<pre>d8988842-43d5-42b3-9049-af4bbc694dbe</pre>
</blockquote>
<h3>UUID version 3/5 example</h3>
<p>xxxxxxxxx-xxxx-3/5xxx-xxxx-xxxxxxxxxxxx<br />
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).<br />
This is the example from the RFC.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class.uuid.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * 6ba7b810-9dad-11d1-80b4-00c04fd430c8 is the DNS name space
 */</span>
<span style="color: #000088;">$md5</span> <span style="color: #339933;">=</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>UUID<span style="color: #339933;">::</span><span style="color: #004000;">UUID_NAME_MD5</span><span style="color: #339933;">,</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">FMT_STRING</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;www.widgets.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'6ba7b810-9dad-11d1-80b4-00c04fd430c8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sha1</span>  <span style="color: #339933;">=</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">generate</span><span style="color: #009900;">&#40;</span>UUID<span style="color: #339933;">::</span><span style="color: #004000;">UUID_NAME_SHA1</span><span style="color: #339933;">,</span> UUID<span style="color: #339933;">::</span><span style="color: #004000;">FMT_STRING</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;www.widgets.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'6ba7b810-9dad-11d1-80b4-00c04fd430c8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;MD5: <span style="color: #006699; font-weight: bold;">$md5</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;SHA1: <span style="color: #006699; font-weight: bold;">$sha1</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Example output</p>
<blockquote>
<pre>MD5: e902893a-9d22-3c7e-a7b8-d6e313b71d9f
SHA1: 13726f09-44a9-5eeb-8910-3525a23fb23b</pre>
</blockquote>
<h3>Final notes</h3>
<p>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 <img src='http://www.shapeshifter.se/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Files</h3>
<ul>
<li><a href="http://www.shapeshifter.se/wp-content/uploads/2008/09/classuuid.phps">class.uuid.php</a></li>
</ul>
<h3>Links</h3>
<ul>
<li><a href="http://tools.ietf.org/html/rfc4122">RFC4122</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.shapeshifter.se/2008/09/29/uuid-generator-for-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
