|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | | -namespace Tempest\Support\Random { |
6 | | - use InvalidArgumentException; |
7 | | - |
8 | | - use function log; |
9 | | - |
10 | | - /** |
11 | | - * Returns a securely generated random string of the given length. The string is |
12 | | - * composed of characters from the given alphabet string. |
13 | | - * |
14 | | - * If the alphabet argument is not specified, the returned string will be composed of |
15 | | - * the alphanumeric characters. |
16 | | - * |
17 | | - * @param int<0, max> $length The length of the string to generate. |
18 | | - * |
19 | | - * @throws InvalidArgumentException If $alphabet length is outside the [2^1, 2^56] range. |
20 | | - */ |
21 | | - function secure_string(int $length, ?string $alphabet = null): string |
22 | | - { |
23 | | - if ($length === 0) { |
24 | | - return ''; |
25 | | - } |
| 5 | +namespace Tempest\Support\Random; |
26 | 6 |
|
27 | | - $alphabet ??= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
28 | | - $alphabet_size = mb_strlen($alphabet); |
29 | | - $bits = (int) \ceil(log($alphabet_size, 2.0)); |
| 7 | +use DateTimeInterface as NativeDateTimeInterface; |
| 8 | +use InvalidArgumentException; |
| 9 | +use Symfony\Component\Uid\Ulid; |
| 10 | +use Symfony\Component\Uid\Uuid; |
| 11 | +use Tempest\DateTime\DateTime; |
| 12 | +use Tempest\DateTime\DateTimeInterface; |
30 | 13 |
|
31 | | - if ($bits < 1 || $bits > 56) { |
32 | | - throw new InvalidArgumentException('$alphabet\'s length must be in [2^1, 2^56]'); |
33 | | - } |
| 14 | +use function log; |
| 15 | + |
| 16 | +/** |
| 17 | + * Returns a securely generated random string of the given length. The string is |
| 18 | + * composed of characters from the given alphabet string. |
| 19 | + * |
| 20 | + * If the alphabet argument is not specified, the returned string will be composed of |
| 21 | + * the alphanumeric characters. |
| 22 | + * |
| 23 | + * @param int<0, max> $length The length of the string to generate. |
| 24 | + * |
| 25 | + * @throws InvalidArgumentException If $alphabet length is outside the [2^1, 2^56] range. |
| 26 | + */ |
| 27 | +function secure_string(int $length, ?string $alphabet = null): string |
| 28 | +{ |
| 29 | + if ($length === 0) { |
| 30 | + return ''; |
| 31 | + } |
| 32 | + |
| 33 | + $alphabet ??= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 34 | + $alphabet_size = mb_strlen($alphabet); |
| 35 | + $bits = (int) \ceil(log($alphabet_size, 2.0)); |
34 | 36 |
|
35 | | - $ret = ''; |
36 | | - while ($length > 0) { |
37 | | - /** @var int<0, max> $urandom_length */ |
38 | | - $urandom_length = (int) ceil(((float) (2 * $length * $bits)) / 8.0); |
39 | | - $data = random_bytes($urandom_length); |
40 | | - |
41 | | - $unpacked_data = 0; |
42 | | - $unpacked_bits = 0; |
43 | | - for ($i = 0; $i < $urandom_length && $length > 0; ++$i) { |
44 | | - // Unpack 8 bits |
45 | | - /** @var array<int, int> $v */ |
46 | | - $v = unpack('C', $data[$i]); |
47 | | - $unpacked_data = ($unpacked_data << 8) | $v[1]; |
48 | | - $unpacked_bits += 8; |
49 | | - |
50 | | - // While we have enough bits to select a character from the alphabet, keep |
51 | | - // consuming the random data |
52 | | - for (; $unpacked_bits >= $bits && $length > 0; $unpacked_bits -= $bits) { |
53 | | - $index = $unpacked_data & ((1 << $bits) - 1); |
54 | | - $unpacked_data >>= $bits; |
55 | | - // Unfortunately, the alphabet size is not necessarily a power of two. |
56 | | - // Worst case, it is 2^k + 1, which means we need (k+1) bits and we |
57 | | - // have around a 50% chance of missing as k gets larger |
58 | | - if ($index < $alphabet_size) { |
59 | | - $ret .= $alphabet[$index]; |
60 | | - --$length; |
61 | | - } |
| 37 | + if ($bits < 1 || $bits > 56) { |
| 38 | + throw new InvalidArgumentException('$alphabet\'s length must be in [2^1, 2^56]'); |
| 39 | + } |
| 40 | + |
| 41 | + $ret = ''; |
| 42 | + while ($length > 0) { |
| 43 | + /** @var int<0, max> $urandom_length */ |
| 44 | + $urandom_length = (int) ceil(((float) (2 * $length * $bits)) / 8.0); |
| 45 | + $data = random_bytes($urandom_length); |
| 46 | + |
| 47 | + $unpacked_data = 0; |
| 48 | + $unpacked_bits = 0; |
| 49 | + for ($i = 0; $i < $urandom_length && $length > 0; ++$i) { |
| 50 | + // Unpack 8 bits |
| 51 | + /** @var array<int, int> $v */ |
| 52 | + $v = unpack('C', $data[$i]); |
| 53 | + $unpacked_data = ($unpacked_data << 8) | $v[1]; |
| 54 | + $unpacked_bits += 8; |
| 55 | + |
| 56 | + // While we have enough bits to select a character from the alphabet, keep |
| 57 | + // consuming the random data |
| 58 | + for (; $unpacked_bits >= $bits && $length > 0; $unpacked_bits -= $bits) { |
| 59 | + $index = $unpacked_data & ((1 << $bits) - 1); |
| 60 | + $unpacked_data >>= $bits; |
| 61 | + // Unfortunately, the alphabet size is not necessarily a power of two. |
| 62 | + // Worst case, it is 2^k + 1, which means we need (k+1) bits and we |
| 63 | + // have around a 50% chance of missing as k gets larger |
| 64 | + if ($index < $alphabet_size) { |
| 65 | + $ret .= $alphabet[$index]; |
| 66 | + --$length; |
62 | 67 | } |
63 | 68 | } |
64 | 69 | } |
| 70 | + } |
65 | 71 |
|
66 | | - return $ret; |
| 72 | + return $ret; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Generates a UUID v7 (time-based) identifier. |
| 77 | + */ |
| 78 | +function uuid(): string |
| 79 | +{ |
| 80 | + return Uuid::v7()->toString(); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Generates a 128-bit universally unique lexicographically sortable identifier. |
| 85 | + */ |
| 86 | +function ulid(null|DateTimeInterface|NativeDateTimeInterface $time = null): string |
| 87 | +{ |
| 88 | + return Ulid::generate($time ? DateTime::parse($time)->toNativeDateTime() : null); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Determines whether the specified string is a valid UUID. |
| 93 | + */ |
| 94 | +function is_uuid(?string $uuid): bool |
| 95 | +{ |
| 96 | + if ($uuid === null) { |
| 97 | + return false; |
67 | 98 | } |
| 99 | + |
| 100 | + return Uuid::isValid($uuid); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Determines whether the specified string is a valid ULID. |
| 105 | + */ |
| 106 | +function is_ulid(?string $ulid): bool |
| 107 | +{ |
| 108 | + if ($ulid === null) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return Ulid::isValid($ulid); |
68 | 113 | } |
0 commit comments