From a087ebf512dafe626179070d4c77e3e69473e424 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sat, 10 Jun 2017 16:02:25 +0200 Subject: [inc/Util] Add randomUuid() function --- inc/util.inc.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'inc') diff --git a/inc/util.inc.php b/inc/util.inc.php index 5d1a4563..697ae3f9 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -434,4 +434,32 @@ SADFACE; return $bytes; } + /** + * @return string a random UUID, v4. + */ + public static function randomUuid() + { + $b = unpack('h8a/h4b/h12c', self::randomBytes(12)); + return sprintf('%08s-%04s-%04x-%04x-%012s', + + // 32 bits for "time_low" + $b['a'], + + // 16 bits for "time_mid" + $b['b'], + + // 16 bits for "time_hi_and_version", + // four most significant bits holds version number 4 + mt_rand(0, 0x0fff) | 0x4000, + + // 16 bits, 8 bits for "clk_seq_hi_res", + // 8 bits for "clk_seq_low", + // two most significant bits holds zero and one for variant DCE1.1 + mt_rand(0, 0x3fff) | 0x8000, + + // 48 bits for "node" + $b['c'] + ); + } + } -- cgit v1.2.3-55-g7522