PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Services/Stormpath/Util/UUID.php

https://github.com/Ocramius/stormpath-sdk-php
PHP | 347 lines | 209 code | 43 blank | 95 comment | 14 complexity | f4f899a1045400bb1146e1c9ef683b71 MD5 | raw file
  1. <?php
  2. /*-
  3. * Copyright (c) 2011 Fredrik Lindberg - http://www.shapeshifter.se
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * Alternative this software might be licensed under the following license
  27. *
  28. * Copyright 2011 Fredrik Lindberg
  29. *
  30. * Licensed under the Apache License, Version 2.0 (the "License");
  31. * you may not use this file except in compliance with the License.
  32. * You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing, software
  37. * distributed under the License is distributed on an "AS IS" BASIS,
  38. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  39. * See the License for the specific language governing permissions and
  40. * limitations under the License.
  41. *
  42. */
  43. /*
  44. * UUID (RFC4122) Generator
  45. * http://tools.ietf.org/html/rfc4122
  46. *
  47. * Implements version 1, 3, 4 and 5
  48. */
  49. /*
  50. * UUID (RFC4122) Generator
  51. * http://tools.ietf.org/html/rfc4122
  52. *
  53. * Implements version 1, 3, 4 and 5
  54. */
  55. class Services_Stormpath_Util_UUID
  56. {
  57. /* UUID versions */
  58. const UUID_TIME = 1; /* Time based UUID */
  59. const UUID_NAME_MD5 = 3; /* Name based (MD5) UUID */
  60. const UUID_RANDOM = 4; /* Random UUID */
  61. const UUID_NAME_SHA1 = 5; /* Name based (SHA1) UUID */
  62. /* UUID formats */
  63. const FMT_FIELD = 100;
  64. const FMT_STRING = 101;
  65. const FMT_BINARY = 102;
  66. const FMT_QWORD = 1; /* Quad-word, 128-bit (not impl.) */
  67. const FMT_DWORD = 2; /* Double-word, 64-bit (not impl.) */
  68. const FMT_WORD = 4; /* Word, 32-bit (not impl.) */
  69. const FMT_SHORT = 8; /* Short (not impl.) */
  70. const FMT_BYTE = 16; /* Byte */
  71. const FMT_DEFAULT = 16;
  72. /* Field UUID representation */
  73. static private $m_uuid_field = array(
  74. 'time_low' => 0, /* 32-bit */
  75. 'time_mid' => 0, /* 16-bit */
  76. 'time_hi' => 0, /* 16-bit */
  77. 'clock_seq_hi' => 0, /* 8-bit */
  78. 'clock_seq_low' => 0, /* 8-bit */
  79. 'node' => array() /* 48-bit */
  80. );
  81. static private $m_generate = array(
  82. self::UUID_TIME => "generateTime",
  83. self::UUID_RANDOM => "generateRandom",
  84. self::UUID_NAME_MD5 => "generateNameMD5",
  85. self::UUID_NAME_SHA1 => "generateNameSHA1"
  86. );
  87. static private $m_convert = array(
  88. self::FMT_FIELD => array(
  89. self::FMT_BYTE => "conv_field2byte",
  90. self::FMT_STRING => "conv_field2string",
  91. self::FMT_BINARY => "conv_field2binary"
  92. ),
  93. self::FMT_BYTE => array(
  94. self::FMT_FIELD => "conv_byte2field",
  95. self::FMT_STRING => "conv_byte2string",
  96. self::FMT_BINARY => "conv_byte2binary"
  97. ),
  98. self::FMT_STRING => array(
  99. self::FMT_BYTE => "conv_string2byte",
  100. self::FMT_FIELD => "conv_string2field",
  101. self::FMT_BINARY => "conv_string2binary"
  102. ),
  103. );
  104. /* Swap byte order of a 32-bit number */
  105. static private function swap32($x) {
  106. return (($x & 0x000000ff) << 24) | (($x & 0x0000ff00) << 8) |
  107. (($x & 0x00ff0000) >> 8) | (($x & 0xff000000) >> 24);
  108. }
  109. /* Swap byte order of a 16-bit number */
  110. static private function swap16($x) {
  111. return (($x & 0x00ff) << 8) | (($x & 0xff00) >> 8);
  112. }
  113. /* Auto-detect UUID format */
  114. static private function detectFormat($src) {
  115. if (is_string($src))
  116. return self::FMT_STRING;
  117. else if (is_array($src)) {
  118. $len = count($src);
  119. if ($len == 1 || ($len % 2) == 0)
  120. return $len;
  121. else
  122. return (-1);
  123. }
  124. else
  125. return self::FMT_BINARY;
  126. }
  127. /*
  128. * Public API, generate a UUID of 'type' in format 'fmt' for
  129. * the given namespace 'ns' and node 'node'
  130. */
  131. static public function generate($type, $fmt = self::FMT_BYTE,
  132. $node = "", $ns = "") {
  133. $func = self::$m_generate[$type];
  134. if (!isset($func))
  135. return null;
  136. $conv = self::$m_convert[self::FMT_FIELD][$fmt];
  137. $uuid = self::$func($ns, $node);
  138. return self::$conv($uuid);
  139. }
  140. /*
  141. * Public API, convert a UUID from one format to another
  142. */
  143. static public function convert($uuid, $from, $to) {
  144. $conv = self::$m_convert[$from][$to];
  145. if (!isset($conv))
  146. return ($uuid);
  147. return (self::$conv($uuid));
  148. }
  149. /*
  150. * Generate an UUID version 4 (pseudo random)
  151. */
  152. static private function generateRandom($ns, $node) {
  153. $uuid = self::$m_uuid_field;
  154. $uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000));
  155. $uuid['clock_seq_hi'] = (1 << 7) | mt_rand(0, 128);
  156. $uuid['time_low'] = mt_rand(0, 0xffff) + (mt_rand(0, 0xffff) << 16);
  157. $uuid['time_mid'] = mt_rand(0, 0xffff);
  158. $uuid['clock_seq_low'] = mt_rand(0, 255);
  159. for ($i = 0; $i < 6; $i++)
  160. $uuid['node'][$i] = mt_rand(0, 255);
  161. return ($uuid);
  162. }
  163. /*
  164. * Generate UUID version 3 and 5 (name based)
  165. */
  166. static private function generateName($ns, $node, $hash, $version) {
  167. $ns_fmt = self::detectFormat($ns);
  168. $field = self::convert($ns, $ns_fmt, self::FMT_FIELD);
  169. /* Swap byte order to keep it in big endian on all platforms */
  170. $field['time_low'] = self::swap32($field['time_low']);
  171. $field['time_mid'] = self::swap16($field['time_mid']);
  172. $field['time_hi'] = self::swap16($field['time_hi']);
  173. /* Convert the namespace to binary and concatenate node */
  174. $raw = self::convert($field, self::FMT_FIELD, self::FMT_BINARY);
  175. $raw .= $node;
  176. /* Hash the namespace and node and convert to a byte array */
  177. $val = $hash($raw, true);
  178. $tmp = unpack('C16', $val);
  179. foreach (array_keys($tmp) as $key)
  180. $byte[$key - 1] = $tmp[$key];
  181. /* Convert byte array to a field array */
  182. $field = self::conv_byte2field($byte);
  183. $field['time_low'] = self::swap32($field['time_low']);
  184. $field['time_mid'] = self::swap16($field['time_mid']);
  185. $field['time_hi'] = self::swap16($field['time_hi']);
  186. /* Apply version and constants */
  187. $field['clock_seq_hi'] &= 0x3f;
  188. $field['clock_seq_hi'] |= (1 << 7);
  189. $field['time_hi'] &= 0x0fff;
  190. $field['time_hi'] |= ($version << 12);
  191. return ($field);
  192. }
  193. static private function generateNameMD5($ns, $node) {
  194. return self::generateName($ns, $node, "md5",
  195. self::UUID_NAME_MD5);
  196. }
  197. static private function generateNameSHA1($ns, $node) {
  198. return self::generateName($ns, $node, "sha1",
  199. self::UUID_NAME_SHA1);
  200. }
  201. /*
  202. * Generate UUID version 1 (time based)
  203. */
  204. static private function generateTime($ns, $node) {
  205. $uuid = self::$m_uuid_field;
  206. /*
  207. * Get current time in 100 ns intervals. The magic value
  208. * is the offset between UNIX epoch and the UUID UTC
  209. * time base October 15, 1582.
  210. */
  211. $tp = gettimeofday();
  212. $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) +
  213. 0x01B21DD213814000;
  214. $uuid['time_low'] = $time & 0xffffffff;
  215. /* Work around PHP 32-bit bit-operation limits */
  216. $high = intval($time / 0xffffffff);
  217. $uuid['time_mid'] = $high & 0xffff;
  218. $uuid['time_hi'] = (($high >> 16) & 0xfff) | (self::UUID_TIME << 12);
  219. /*
  220. * We don't support saved state information and generate
  221. * a random clock sequence each time.
  222. */
  223. $uuid['clock_seq_hi'] = 0x80 | mt_rand(0, 64);
  224. $uuid['clock_seq_low'] = mt_rand(0, 255);
  225. /*
  226. * Node should be set to the 48-bit IEEE node identifier, but
  227. * we leave it for the user to supply the node.
  228. */
  229. for ($i = 0; $i < 6; $i++)
  230. $uuid['node'][$i] = ord(substr($node, $i, 1));
  231. return ($uuid);
  232. }
  233. /* Assumes correct byte order */
  234. static private function conv_field2byte($src) {
  235. $uuid[0] = ($src['time_low'] & 0xff000000) >> 24;
  236. $uuid[1] = ($src['time_low'] & 0x00ff0000) >> 16;
  237. $uuid[2] = ($src['time_low'] & 0x0000ff00) >> 8;
  238. $uuid[3] = ($src['time_low'] & 0x000000ff);
  239. $uuid[4] = ($src['time_mid'] & 0xff00) >> 8;
  240. $uuid[5] = ($src['time_mid'] & 0x00ff);
  241. $uuid[6] = ($src['time_hi'] & 0xff00) >> 8;
  242. $uuid[7] = ($src['time_hi'] & 0x00ff);
  243. $uuid[8] = $src['clock_seq_hi'];
  244. $uuid[9] = $src['clock_seq_low'];
  245. for ($i = 0; $i < 6; $i++)
  246. $uuid[10+$i] = $src['node'][$i];
  247. return ($uuid);
  248. }
  249. static private function conv_field2string($src) {
  250. $str = sprintf(
  251. '%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
  252. ($src['time_low']), ($src['time_mid']), ($src['time_hi']),
  253. $src['clock_seq_hi'], $src['clock_seq_low'],
  254. $src['node'][0], $src['node'][1], $src['node'][2],
  255. $src['node'][3], $src['node'][4], $src['node'][5]);
  256. return ($str);
  257. }
  258. static private function conv_field2binary($src) {
  259. $byte = self::conv_field2byte($src);
  260. return self::conv_byte2binary($byte);
  261. }
  262. static private function conv_byte2field($uuid) {
  263. $field = self::$m_uuid_field;
  264. $field['time_low'] = ($uuid[0] << 24) | ($uuid[1] << 16) |
  265. ($uuid[2] << 8) | $uuid[3];
  266. $field['time_mid'] = ($uuid[4] << 8) | $uuid[5];
  267. $field['time_hi'] = ($uuid[6] << 8) | $uuid[7];
  268. $field['clock_seq_hi'] = $uuid[8];
  269. $field['clock_seq_low'] = $uuid[9];
  270. for ($i = 0; $i < 6; $i++)
  271. $field['node'][$i] = $uuid[10+$i];
  272. return ($field);
  273. }
  274. static public function conv_byte2string($src) {
  275. $field = self::conv_byte2field($src);
  276. return self::conv_field2string($field);
  277. }
  278. static private function conv_byte2binary($src) {
  279. $raw = pack('C16', $src[0], $src[1], $src[2], $src[3],
  280. $src[4], $src[5], $src[6], $src[7], $src[8], $src[9],
  281. $src[10], $src[11], $src[12], $src[13], $src[14], $src[15]);
  282. return ($raw);
  283. }
  284. static private function conv_string2field($src) {
  285. $parts = sscanf($src, '%x-%x-%x-%x-%02x%02x%02x%02x%02x%02x');
  286. $field = self::$m_uuid_field;
  287. $field['time_low'] = ($parts[0]);
  288. $field['time_mid'] = ($parts[1]);
  289. $field['time_hi'] = ($parts[2]);
  290. $field['clock_seq_hi'] = ($parts[3] & 0xff00) >> 8;
  291. $field['clock_seq_low'] = $parts[3] & 0x00ff;
  292. for ($i = 0; $i < 6; $i++)
  293. $field['node'][$i] = $parts[4+$i];
  294. return ($field);
  295. }
  296. static private function conv_string2byte($src) {
  297. $field = self::conv_string2field($src);
  298. return self::conv_field2byte($field);
  299. }
  300. static private function conv_string2binary($src) {
  301. $byte = self::conv_string2byte($src);
  302. return self::conv_byte2binary($byte);
  303. }
  304. }