PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/classes/uuid.php

https://github.com/mmarinero/ProjectManagementExample
PHP | 159 lines | 52 code | 38 blank | 69 comment | 4 complexity | e679f908ec5b8045b15dd0815c854c33 MD5 | raw file
  1<?php
  2
  3/**
  4 * UUID generator class
  5 *
  6 * Generates valid RFC 4211 compliant Universally Unique IDentifiers (UUID) version 3, 4 and 5. 
  7 * UUIDs generated validate using the OSSP UUID Tool, and the output for named-based UUIDs are 
  8 * exactly the same. This is a pure PHP implementation.
  9 *
 10 * Usage:
 11 * 
 12 *   Name-based UUID:
 13 *
 14 *     $v3uuid = UUID::v3('1546058f-5a25-4334-85ae-e68f2a44bbaf', 'SomeRandomString');
 15 *     $v5uuid = UUID::v5(UUID::NS_URL, 'http://www.google.com/');
 16 *
 17 *   Pseudo-random UUID:
 18 *
 19 *     $v4uuid = UUID::v4();
 20 *
 21 *
 22 * Originally found at: http://www.php.net/manual/en/function.uniqid.php#94959
 23 *
 24 * @author Andrew Moore 
 25 *
 26 *
 27 * Modifications made by Henry Merriam <php@henrymerriam.com> on 2009-12-20:
 28 *
 29 *   + Added constants for predefined namespaces as defined in RFC 4211 Appendix C.
 30 *     + NS_DNS
 31 *     + NS_URL
 32 *     + NS_ISO_UID
 33 *     + NS_X500_DN
 34 *
 35 *   + Wrote this documentation comment.
 36 *
 37 */
 38class UUID {
 39
 40    const NS_DNS     = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // FQDN
 41    const NS_URL     = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; // URL
 42    const NS_ISO_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; // ISO OID
 43    const NS_X500_DN = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; // X.500 DN (in DER or a text output format)
 44
 45    public static function v3($namespace, $name) {
 46
 47        if(!self::is_valid($namespace)) return false;
 48
 49        // Get hexadecimal components of namespace
 50        $nhex = str_replace(array('-','{','}'), '', $namespace);
 51
 52        // Binary Value
 53        $nstr = '';
 54
 55        // Convert Namespace UUID to bits
 56        for($i = 0; $i < strlen($nhex); $i+=2) {
 57            $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
 58        }
 59
 60        // Calculate hash value
 61        $hash = md5($nstr . $name);
 62
 63        // Format and return UUID
 64        return sprintf('%08s-%04s-%04x-%04x-%12s',
 65
 66            // 32 bits for "time_low"
 67            substr($hash, 0, 8),
 68
 69            // 16 bits for "time_mid"
 70            substr($hash, 8, 4),
 71
 72            // 16 bits for "time_hi_and_version",
 73            // four most significant bits holds version number 3
 74            (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
 75
 76            // 16 bits, 8 bits for "clk_seq_hi_res",
 77            // 8 bits for "clk_seq_low",
 78            // two most significant bits holds zero and one for variant DCE1.1
 79            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
 80
 81            // 48 bits for "node"
 82            substr($hash, 20, 12)
 83        );
 84
 85    }
 86
 87    public static function v4() {
 88
 89        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
 90
 91            // 32 bits for "time_low"
 92            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
 93
 94            // 16 bits for "time_mid"
 95            mt_rand(0, 0xffff),
 96
 97            // 16 bits for "time_hi_and_version",
 98            // four most significant bits holds version number 4
 99            mt_rand(0, 0x0fff) | 0x4000,
100
101            // 16 bits, 8 bits for "clk_seq_hi_res",
102            // 8 bits for "clk_seq_low",
103            // two most significant bits holds zero and one for variant DCE1.1
104            mt_rand(0, 0x3fff) | 0x8000,
105
106            // 48 bits for "node"
107            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
108        );
109
110    }
111
112    public static function v5($namespace, $name) {
113
114        if(!self::is_valid($namespace)) return false;
115
116        // Get hexadecimal components of namespace
117        $nhex = str_replace(array('-','{','}'), '', $namespace);
118
119        // Binary Value
120        $nstr = '';
121
122        // Convert Namespace UUID to bits
123        for($i = 0; $i < strlen($nhex); $i+=2) {
124            $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
125        }
126
127        // Calculate hash value
128        $hash = sha1($nstr . $name);
129
130        // Format and return UUID
131        return sprintf('%08s-%04s-%04x-%04x-%12s',
132
133            // 32 bits for "time_low"
134            substr($hash, 0, 8),
135
136            // 16 bits for "time_mid"
137            substr($hash, 8, 4),
138
139            // 16 bits for "time_hi_and_version",
140            // four most significant bits holds version number 5
141            (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
142
143            // 16 bits, 8 bits for "clk_seq_hi_res",
144            // 8 bits for "clk_seq_low",
145            // two most significant bits holds zero and one for variant DCE1.1
146            (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
147
148            // 48 bits for "node"
149            substr($hash, 20, 12)
150        );
151
152    }
153
154    public static function is_valid($uuid) {
155        return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
156            '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
157    }
158
159}