Skip to content

Commit 7e64d4e

Browse files
committed
Added the finished first version
- Added lib.php file (function for generating the UUID) - Added test.php file (file for testing the function)
1 parent 4dd8fe7 commit 7e64d4e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Alternative UUID Generation Script
4+
*
5+
* This is an alternative version of a randomly generated UUID, using both the timestamp and pseudo-random bytes.
6+
* Maybe I'm wrong about all that but I thought mixing the UUID v1 and v4 methods would make the generated UUID more secure, as it ensures a zero-chance of collision of a single computer and a -really- low chance of collision on multiple computers (because of both probability and time).
7+
* The UUID contains 62 randomly generated bits, so there is 2^62 different outcomes for that part of the generated UUID, which means you'd start geeting colisions after about 2^31 generations. That being said, that would only happen if you were able to generate that many UUIDs within 100 nanosecond to 1 microsecond (because the gettimeofday() function used here returns a microsecond-precise timestamp).
8+
*
9+
* This generated UUID format is {oooooooo-oooo-Mooo-Nxxx-xxxxxxxxxxxx}, it concatenates:
10+
* - o: The current timestamp (60 bits) (time_low, time_mid, time_high)
11+
* - M: The version (4 bits)
12+
* - N: The variant (2 bits)
13+
* - x: Pseudo-random values (62 bits)
14+
*
15+
* Based on:
16+
* - Code from an UUID v1 Generation script. https://github.com/fredriklindberg/class.uuid.php/blob/c1de11110970c6df4f5d7743a11727851c7e5b5a/class.uuid.php#L220
17+
* - Code from an UUID v4 Generation script. https://stackoverflow.com/a/15875555/5255556
18+
*
19+
* @author Matiboux <[email protected]>
20+
* @link https://github.com/matiboux/Time-Based-Random-UUID
21+
* @version 1.0
22+
* @return string Returns the generated UUID.
23+
*/
24+
function uuid($tp = null) {
25+
if(!empty($tp)) {
26+
if(is_array($tp)) $time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10);
27+
else if(is_numeric($tp)) $time = (int) ($tp * 10000000);
28+
else return false;
29+
} else $time = (int) (gettimeofday(true) * 10000000);
30+
$time += 0x01B21DD213814000;
31+
32+
$arr = str_split(dechex($time & 0xffffffff), 4); // time_low (32 bits)
33+
$high = intval($time / 0xffffffff);
34+
array_push($arr, dechex($high & 0xffff)); // time_mid (16 bits)
35+
array_push($arr, dechex(0x4000 | (($high >> 16) & 0x0fff))); // Version (4 bits) + time_high (12 bits)
36+
37+
// Variant (2 bits) + Cryptographically Secure Pseudo-Random Bytes (62 bits)
38+
if(function_exists('random_bytes')) $random = random_bytes(8);
39+
else $random = openssl_random_pseudo_bytes(8);
40+
$random[0] = chr(ord($random[0]) & 0x3f | 0x80); // Apply variant: Set the two first bits of the random set to 10.
41+
42+
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', array_merge($arr, str_split(bin2hex($random), 4)));
43+
return strlen($uuid) == 36 ? $uuid : false;
44+
}
45+
?>

test.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
header('Content-Type: text/plain');
3+
include 'lib.php';
4+
5+
echo 'Fixed Date (2018-06-27 22:40:00): '; var_dump(uuid(strtotime('2018-06-27 22:25:00')));
6+
echo 'Right now: '; var_dump(uuid());
7+
?>

0 commit comments

Comments
 (0)