<?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>Rommel Santor&#039;s Clog &#187; uuid</title>
	<atom:link href="https://rommelsantor.com/clog/tag/uuid/feed/" rel="self" type="application/rss+xml" />
	<link>https://rommelsantor.com/clog</link>
	<description>my exploits and misadventures in programming</description>
	<lastBuildDate>Thu, 04 Feb 2016 12:56:01 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Generate UUID in PHP</title>
		<link>https://rommelsantor.com/clog/2012/02/23/generate-uuid-in-php/</link>
		<comments>https://rommelsantor.com/clog/2012/02/23/generate-uuid-in-php/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 17:47:02 +0000</pubDate>
		<dc:creator><![CDATA[rommel]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[uuid]]></category>

		<guid isPermaLink="false">http://rommelsantor.com/clog/?p=313</guid>
		<description><![CDATA[I posted this last year to PHP.net and thought I&#8217;d just share it here as well. It&#8217;s a short write-up and demonstration of a PHP class you can use to generate various versions of a Universally-Unique Identifier, or UUID. The php5-uuid functions could definitely use some documentation to clarify how they should be used, but [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I posted this last year to PHP.net and thought I&#8217;d just share it here as well. It&#8217;s a short write-up and demonstration of a PHP class you can use to generate various versions of a Universally-Unique Identifier, or UUID.</p>
<p>The php5-uuid functions could definitely use some documentation to clarify how they should be used, but here&#8217;s what I&#8217;ve gleaned by examining the OSSP source code (found here: <a href="http://ossp-uuid.sourcearchive.com/documentation/1.5.1-1ubuntu1/php_2uuid_8c-source.html" target="_blank">http://ossp-uuid.sourcearchive.com/documentation/1.5.1-1ubuntu1/php_2uuid_8c-source.html</a>).</p>
<p>The uuid_make() function takes two arguments when generating v1 or v4, but four arguments are required when generating v3 or v5. The first two arguments have been thoroughly demonstrated and are straightforward, so I&#8217;ll skip to the as-yet non-described arguments.</p>
<p><span id="more-313"></span>
<div style="float:left;margin:0 10px 10px 0">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7639656561235411";
/* Square (250x250) */
google_ad_slot = "8522038794";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
The third argument to uuid_make() is: $namespace<br />
  &#8211; this is a secondary resource created with uuid_create(); it is apparently used to generate an internal UUID, which is used as the namespace of the output UUID</p>
<p>The fourth argument to uuid_make() is: $url<br />
  &#8211; this is the value that is to be hashed (MD5 for v3, SHA-1 for v5); it may be any string or even null</p>
<p>Here&#8217;s a simple class illustrating the proper usage (note that if php5-uuid is not installed on your system, each function call will just return false):</p>
<pre>
class UUID {
  /**
   * Generates version 1: MAC address
   */
  public static function v1() {
    if (!function_exists('uuid_create'))
      return false;

    $context = $uuid = null;
    uuid_create($context);
    uuid_make($context, UUID_MAKE_V1);
    uuid_export($context, UUID_FMT_STR, $uuid);
    return trim($uuid);
  }

  /**
   * Generates version 3 UUID: MD5 hash of URL
   */
  public static function v3($i_url) {
    if (!function_exists('uuid_create'))
      return false;

    if (!strlen($i_url))
      $i_url = self::v1();

    $context = $namespace = $uuid = null;
    uuid_create($context);
    uuid_create($namespace);

    uuid_make($context, UUID_MAKE_V3, $namespace, $i_url);
    uuid_export($context, UUID_FMT_STR, $uuid);
    return trim($uuid);
  }

  /**
   * Generates version 4 UUID: random
   */
  public static function v4() {
    if (!function_exists('uuid_create'))
      return false;

    $context = $uuid = null;
    uuid_create($context);

    uuid_make($context, UUID_MAKE_V4);
    uuid_export($context, UUID_FMT_STR, $uuid);
    return trim($uuid);
  }

  /**
   * Generates version 5 UUID: SHA-1 hash of URL
   */
  public static function v5($i_url) {
    if (!function_exists('uuid_create'))
      return false;

    if (!strlen($i_url))
      $i_url = self::v1();

    $context = $namespace = $uuid = null;
    uuid_create($context);
    uuid_create($namespace);

    uuid_make($context, UUID_MAKE_V5, $namespace, $i_url);
    uuid_export($context, UUID_FMT_STR, $uuid);
    return trim($uuid);
  }
}
</pre>
<p>And here&#8217;s a demonstration:</p>
<pre>
for ($i = 1; $i <= 3; ++$i) {
  echo 'microtime = ' . microtime(true) . '<br/>';
  echo "V1 UUID: " . UUID::v1() . '<br/>';
  echo "V3 UUID of URL='abc': " . UUID::v3('abc') . '<br/>';
  echo "V4 UUID: " . UUID::v4() . '<br/>';
  echo "V5 UUID of URL=null: " . UUID::v5(null) . '<br/>';
  echo '<hr/>';
}
</pre>
<p>And the output:</p>
<p><code>microtime = 1306620716.0457<br />
V1 UUID: 7fddae8e-8977-11e0-bc11-003048c3b1f2<br />
V3 UUID of URL='abc': 522ec739-ca63-3ec5-b082-08ce08ad65e2<br />
V4 UUID: b3851ec7-4871-4527-92b5-ef5616bae1e6<br />
V5 UUID of URL=null: e129f27c-5103-5c5c-844b-cdf0a15e160d<br />
-------------------<br />
microtime = 1306620716.0465<br />
V1 UUID: 7fddb83e-8977-11e0-9e6e-003048c3b1f2<br />
V3 UUID of URL='abc': 522ec739-ca63-3ec5-b082-08ce08ad65e2<br />
V4 UUID: 7e78fe0d-59b8-4637-af7f-e88d221a7d1e<br />
V5 UUID of URL=null: e129f27c-5103-5c5c-844b-cdf0a15e160d<br />
-------------------<br />
microtime = 1306620716.0467<br />
V1 UUID: 7fddbfb4-8977-11e0-a2bc-003048c3b1f2<br />
V3 UUID of URL='abc': 522ec739-ca63-3ec5-b082-08ce08ad65e2<br />
V4 UUID: 12a940c7-0f3f-46a1-bb5f-bdd602e10654<br />
V5 UUID of URL=null: e129f27c-5103-5c5c-844b-cdf0a15e160d</code></p>
<p>As you can see, the calls to v3() always return the same UUID because the same URL parameter, &#8220;abc&#8221;, is always supplied. The same goes for the v5() function which is always supplied a null URL.</p>
<p>The v4() UUIDs are always entirely different because they are (pseudo)random. And the v1() calls are very similar but just slightly different because it&#8217;s based on the computer&#8217;s MAC address and the current time.</p>
<p><script type="text/javascript">//< ![CDATA[
$("pre").attr({name:'code'}).addClass('php:nocontrols:nogutter');
//]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>https://rommelsantor.com/clog/2012/02/23/generate-uuid-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
