Rev 72 | Rev 75 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 72 | Rev 73 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | <?php |
1 | <?php |
2 | 2 | ||
3 | /* |
3 | /* |
4 | * UUID utils for PHP |
4 | * UUID utils for PHP |
5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
5 | * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft |
6 | * Version 2023-09-23 |
6 | * Version 2023-09-24 |
7 | * |
7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | * you may not use this file except in compliance with the License. |
9 | * you may not use this file except in compliance with the License. |
10 | * You may obtain a copy of the License at |
10 | * You may obtain a copy of the License at |
11 | * |
11 | * |
Line 1264... | Line 1264... | ||
1264 | 1264 | ||
1265 | $ns_fraction = $ms_fraction * 1000000; // <ns> |
1265 | $ns_fraction = $ms_fraction * 1000000; // <ns> |
1266 | $val = (int)ceil($ns_fraction / $resolution_ns); |
1266 | $val = (int)ceil($ns_fraction / $resolution_ns); |
1267 | 1267 | ||
1268 | // Currently, for the output we only allow frac bits 0, 4, 8, 12 (0-3 nibbles), |
1268 | // Currently, for the output we only allow frac bits 0, 4, 8, 12 (0-3 nibbles), |
1269 | // since UUIDs are usually sorted in their hex notation |
1269 | // since UUIDs are usually sorted in their hex notation, and one of the main |
- | 1270 | // reasons for using the sub-millisecond fractions it to increase monotonicity |
|
1270 | $num_nibbles = (int)ceil($num_ms_frac_bits/4); |
1271 | $num_nibbles = (int)ceil($num_ms_frac_bits/4); |
1271 | $uuid_nibbles .= str_pad(dechex($val), $num_nibbles, '0', STR_PAD_LEFT); |
1272 | $uuid_nibbles .= str_pad(dechex($val), $num_nibbles, '0', STR_PAD_LEFT); |
1272 | } |
1273 | } |
1273 | 1274 | ||
1274 | // TODO Not implemented: Optional counter (to be defined as parameter to this method) |
1275 | // TODO Not implemented: Optional counter (to be defined as parameter to this method) |
Line 1340... | Line 1341... | ||
1340 | 1341 | ||
1341 | if ($hash_algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false); |
1342 | if ($hash_algo == 'shake128') $hash = shake128($payload, 16/*min. required bytes*/, false); |
1342 | else if ($hash_algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false); |
1343 | else if ($hash_algo == 'shake256') $hash = shake256($payload, 16/*min. required bytes*/, false); |
1343 | else $hash = hash($hash_algo, $payload, false); |
1344 | else $hash = hash($hash_algo, $payload, false); |
1344 | 1345 | ||
1345 | $hash = str_pad($hash, 32, '0', STR_PAD_RIGHT); // fill short hashes with zeros to the right |
- | |
1346 | - | ||
1347 | if ($hash == null) { |
1346 | if ($hash == null) { |
1348 | throw new Exception("Unknown Hash Algorithm $hash_algo"); |
1347 | throw new Exception("Unknown Hash Algorithm $hash_algo"); |
1349 | } |
1348 | } |
1350 | 1349 | ||
- | 1350 | $hash = str_pad($hash, 32, '0', STR_PAD_RIGHT); // fill short hashes with zeros to the right |
|
- | 1351 | ||
1351 | $hash[12] = '8'; // Set version: 8 = Custom |
1352 | $hash[12] = '8'; // Set version: 8 = Custom |
1352 | $hash[16] = dechex(hexdec($hash[16]) & 0b0011 | 0b1000); // Set variant to "0b10__" (RFC4122/DCE1.1) |
1353 | $hash[16] = dechex(hexdec($hash[16]) & 0b0011 | 0b1000); // Set variant to "0b10__" (RFC4122/DCE1.1) |
1353 | 1354 | ||
1354 | return substr($hash, 0, 8).'-'. |
1355 | return substr($hash, 0, 8).'-'. |
1355 | substr($hash, 8, 4).'-'. |
1356 | substr($hash, 8, 4).'-'. |