PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 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 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Generator / DceSecurityGenerator.php

DceSecurityGenerator.php in Media Cloud Sync 1.4.2, at includes/sdk/google/ramsey/uuid/src/Generator/DceSecurityGenerator.php

96 lines 4.2 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 <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Generator;
14
15 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface;
16 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\DceSecurityException;
17 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Provider\DceSecurityProviderInterface;
18 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal;
19 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject;
20 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid;
21 use function hex2bin;
22 use function in_array;
23 use function pack;
24 use function str_pad;
25 use function strlen;
26 use function substr_replace;
27 use const STR_PAD_LEFT;
28 /**
29 * DceSecurityGenerator generates strings of binary data based on a local domain, local identifier, node ID, clock
30 * sequence, and the current time
31 */
32 class DceSecurityGenerator implements DceSecurityGeneratorInterface
33 {
34 private const DOMAINS = [Uuid::DCE_DOMAIN_PERSON, Uuid::DCE_DOMAIN_GROUP, Uuid::DCE_DOMAIN_ORG];
35 /**
36 * Upper bounds for the clock sequence in DCE Security UUIDs.
37 */
38 private const CLOCK_SEQ_HIGH = 63;
39 /**
40 * Lower bounds for the clock sequence in DCE Security UUIDs.
41 */
42 private const CLOCK_SEQ_LOW = 0;
43 public function __construct(private NumberConverterInterface $numberConverter, private TimeGeneratorInterface $timeGenerator, private DceSecurityProviderInterface $dceSecurityProvider)
44 {
45 }
46 public function generate(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null) : string
47 {
48 if (!in_array($localDomain, self::DOMAINS)) {
49 throw new DceSecurityException('Local domain must be a valid DCE Security domain');
50 }
51 if ($localIdentifier && $localIdentifier->isNegative()) {
52 throw new DceSecurityException('Local identifier out of bounds; it must be a value between 0 and 4294967295');
53 }
54 if ($clockSeq > self::CLOCK_SEQ_HIGH || $clockSeq < self::CLOCK_SEQ_LOW) {
55 throw new DceSecurityException('Clock sequence out of bounds; it must be a value between 0 and 63');
56 }
57 switch ($localDomain) {
58 case Uuid::DCE_DOMAIN_ORG:
59 if ($localIdentifier === null) {
60 throw new DceSecurityException('A local identifier must be provided for the org domain');
61 }
62 break;
63 case Uuid::DCE_DOMAIN_PERSON:
64 if ($localIdentifier === null) {
65 $localIdentifier = $this->dceSecurityProvider->getUid();
66 }
67 break;
68 case Uuid::DCE_DOMAIN_GROUP:
69 default:
70 if ($localIdentifier === null) {
71 $localIdentifier = $this->dceSecurityProvider->getGid();
72 }
73 break;
74 }
75 $identifierHex = $this->numberConverter->toHex($localIdentifier->toString());
76 // The maximum value for the local identifier is 0xffffffff, or 4,294,967,295. This is 8 hexadecimal digits, so
77 // if the length of hexadecimal digits is greater than 8, we know the value is greater than 0xffffffff.
78 if (strlen($identifierHex) > 8) {
79 throw new DceSecurityException('Local identifier out of bounds; it must be a value between 0 and 4294967295');
80 }
81 $domainByte = pack('n', $localDomain)[1];
82 $identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT));
83 if ($node instanceof Hexadecimal) {
84 $node = $node->toString();
85 }
86 // Shift the clock sequence 8 bits to the left, so it matches 0x3f00.
87 if ($clockSeq !== null) {
88 $clockSeq = $clockSeq << 8;
89 }
90 $bytes = $this->timeGenerator->generate($node, $clockSeq);
91 // Replace bytes in the time-based UUID with DCE Security values.
92 $bytes = substr_replace($bytes, $identifierBytes, 0, 4);
93 return substr_replace($bytes, $domainByte, 9, 1);
94 }
95 }
96