function RandomNodeProvider::getNode
Overrides NodeProviderInterface::getNode
File
-
vendor/
ramsey/ uuid/ src/ Provider/ Node/ RandomNodeProvider.php, line 38
Class
- RandomNodeProvider
- RandomNodeProvider generates a random node ID
Namespace
Ramsey\Uuid\Provider\NodeCode
public function getNode() : Hexadecimal {
try {
$nodeBytes = random_bytes(6);
} catch (Throwable $exception) {
throw new RandomSourceException($exception->getMessage(), (int) $exception->getCode(), $exception);
}
// Split the node bytes for math on 32-bit systems.
$nodeMsb = substr($nodeBytes, 0, 3);
$nodeLsb = substr($nodeBytes, 3);
// Set the multicast bit; see RFC 4122, section 4.5.
$nodeMsb = hex2bin(str_pad(dechex(hexdec(bin2hex($nodeMsb)) | 0x10000), 6, '0', STR_PAD_LEFT));
// Recombine the node bytes.
$node = $nodeMsb . $nodeLsb;
return new Hexadecimal(str_pad(bin2hex($node), 12, '0', STR_PAD_LEFT));
}