PluginProbe
Media Cloud Sync / 1.2.9
Media Cloud Sync v1.2.9
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Provider / Node / RandomNodeProvider.php

RandomNodeProvider.php in Media Cloud Sync 1.2.9, at includes/sdk/google/ramsey/uuid/src/Provider/Node/RandomNodeProvider.php

50 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the ramsey/uuid library
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 *
9 * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\Ramsey\Uuid\Provider\Node;
14
15 use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\RandomSourceException;
16 use Dudlewebs\WPMCS\Ramsey\Uuid\Provider\NodeProviderInterface;
17 use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal;
18 use Throwable;
19 use function bin2hex;
20 use function dechex;
21 use function hex2bin;
22 use function hexdec;
23 use function str_pad;
24 use function substr;
25 use const STR_PAD_LEFT;
26 /**
27 * RandomNodeProvider generates a random node ID
28 *
29 * @link http://tools.ietf.org/html/rfc4122#section-4.5 RFC 4122, ยง 4.5: Node IDs that Do Not Identify the Host
30 */
31 class RandomNodeProvider implements NodeProviderInterface
32 {
33 public function getNode(): Hexadecimal
34 {
35 try {
36 $nodeBytes = random_bytes(6);
37 } catch (Throwable $exception) {
38 throw new RandomSourceException($exception->getMessage(), (int) $exception->getCode(), $exception);
39 }
40 // Split the node bytes for math on 32-bit systems.
41 $nodeMsb = substr($nodeBytes, 0, 3);
42 $nodeLsb = substr($nodeBytes, 3);
43 // Set the multicast bit; see RFC 4122, section 4.5.
44 $nodeMsb = hex2bin(str_pad(dechex(hexdec(bin2hex($nodeMsb)) | 0x10000), 6, '0', STR_PAD_LEFT));
45 // Recombine the node bytes.
46 $node = $nodeMsb . $nodeLsb;
47 return new Hexadecimal(str_pad(bin2hex($node), 12, '0', STR_PAD_LEFT));
48 }
49 }
50