|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of unique string identifier format generation for generator services. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\Randomness; |
|
8
|
|
|
|
|
9
|
|
|
use CryptoManana\Core\Traits\Randomness\RandomnessTrait as RandomnessSpecification; |
|
10
|
|
|
use CryptoManana\Core\StringBuilder as StringBuilder; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait IdentifierOutputTrait - Reusable implementation of `IdentifierOutputInterface`. |
|
14
|
|
|
* |
|
15
|
|
|
* @see \CryptoManana\Core\Interfaces\Randomness\IdentifierOutputInterface The abstract specification. |
|
16
|
|
|
* |
|
17
|
|
|
* @package CryptoManana\Core\Traits\Randomness |
|
18
|
|
|
* |
|
19
|
|
|
* @mixin RandomnessSpecification |
|
20
|
|
|
*/ |
|
21
|
|
|
trait IdentifierOutputTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Forcing the implementation of the software abstract randomness. |
|
25
|
|
|
* |
|
26
|
|
|
* {@internal Forcing the implementation of `AbstractRandomness`. }} |
|
27
|
|
|
*/ |
|
28
|
|
|
use RandomnessSpecification; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Generate a random version 4 Globally Unique Identifier (GUID) standard string. |
|
32
|
|
|
* |
|
33
|
|
|
* Note: The identifier string uses 32 alphanumeric characters and 4 hyphens (optional). |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $prefix Optional prefix for output strings (default => ''). |
|
36
|
|
|
* @param bool $withDashes Flag for using dashes format (default => true). |
|
37
|
|
|
* @param bool $upperCase Flag for using uppercase format (default => false). |
|
38
|
|
|
* |
|
39
|
|
|
* @return string Randomly generated GUID string representing a 128-bit number. |
|
40
|
|
|
* @throws \Exception Validation errors. |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function getGloballyUniqueId($prefix = '', $withDashes = true, $upperCase = false) |
|
43
|
|
|
{ |
|
44
|
3 |
|
$tmp = $this->getBytes(16); |
|
45
|
|
|
|
|
46
|
3 |
|
$tmp[6] = StringBuilder::getChr(StringBuilder::getOrd($tmp[6]) & 0x0f | 0x40); |
|
47
|
3 |
|
$tmp[8] = StringBuilder::getChr(StringBuilder::getOrd($tmp[8]) & 0x3f | 0x80); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@internal Using only the built-in function to make it more encoding friendly (bigger than 8 bytes). }} |
|
51
|
|
|
*/ |
|
52
|
3 |
|
$id = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($tmp), 4)); |
|
53
|
|
|
|
|
54
|
3 |
|
$id = ($withDashes) ? $id : StringBuilder::stringReplace('-', '', $id); |
|
55
|
|
|
|
|
56
|
3 |
|
$id = ($upperCase) ? StringBuilder::stringToUpper($id) : $id; |
|
57
|
|
|
|
|
58
|
3 |
|
return StringBuilder::stringFullTrimming($prefix) . $id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Generate a strong Universally Unique Identifier (UUID) string in hexadecimal or alphanumeric format. |
|
63
|
|
|
* |
|
64
|
|
|
* Note: The identifier string is exactly 128 characters long. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $prefix Optional prefix for output strings (default => ''). |
|
67
|
|
|
* @param bool $alphaNumeric Flag for switching to alphanumerical format (default => false). |
|
68
|
|
|
* |
|
69
|
|
|
* @return string Randomly generated strong hexadecimal/alphanumerical UUID string. |
|
70
|
|
|
* @throws \Exception Validation errors. |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function getStrongUniqueId($prefix = '', $alphaNumeric = false) |
|
73
|
|
|
{ |
|
74
|
3 |
|
if ($alphaNumeric) { |
|
75
|
3 |
|
$id = $this->getAlphaNumeric(128); |
|
76
|
|
|
|
|
77
|
3 |
|
$id = $this->getBool() ? StringBuilder::stringReverse($id) : $id; |
|
78
|
|
|
} else { |
|
79
|
3 |
|
$id = hash_hmac( |
|
80
|
3 |
|
'sha512', // exactly 128 chars output (1024-bit) |
|
81
|
3 |
|
$this->getBytes(64), // 512-bit input |
|
82
|
3 |
|
$this->getBytes(64) // 512-bit key |
|
83
|
3 |
|
); |
|
84
|
|
|
|
|
85
|
3 |
|
$id = StringBuilder::stringToUpper($id); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
return StringBuilder::stringFullTrimming($prefix) . $id; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|