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