diff --git a/ByteString.php b/ByteString.php index 5cbfd6d..42c87b7 100644 --- a/ByteString.php +++ b/ByteString.php @@ -56,38 +56,7 @@ public static function fromRandom(int $length = 16, ?string $alphabet = null): s throw new InvalidArgumentException('The length of the alphabet must in the [2^1, 2^56] range.'); } - if (\PHP_VERSION_ID >= 80300) { - return new static((new Randomizer())->getBytesFromString($alphabet, $length)); - } - - $ret = ''; - while ($length > 0) { - $urandomLength = (int) ceil(2 * $length * $bits / 8.0); - $data = random_bytes($urandomLength); - $unpackedData = 0; - $unpackedBits = 0; - for ($i = 0; $i < $urandomLength && $length > 0; ++$i) { - // Unpack 8 bits - $unpackedData = ($unpackedData << 8) | \ord($data[$i]); - $unpackedBits += 8; - - // While we have enough bits to select a character from the alphabet, keep - // consuming the random data - for (; $unpackedBits >= $bits && $length > 0; $unpackedBits -= $bits) { - $index = ($unpackedData & ((1 << $bits) - 1)); - $unpackedData >>= $bits; - // Unfortunately, the alphabet size is not necessarily a power of two. - // Worst case, it is 2^k + 1, which means we need (k+1) bits and we - // have around a 50% chance of missing as k gets larger - if ($index < $alphabetSize) { - $ret .= $alphabet[$index]; - --$length; - } - } - } - } - - return new static($ret); + return new static((new Randomizer())->getBytesFromString($alphabet, $length)); } public function bytesAt(int $offset): array diff --git a/composer.json b/composer.json index 10d0ee6..abbb9e5 100644 --- a/composer.json +++ b/composer.json @@ -16,19 +16,19 @@ } ], "require": { - "php": ">=8.2", - "symfony/polyfill-ctype": "~1.8", - "symfony/polyfill-intl-grapheme": "~1.0", - "symfony/polyfill-intl-normalizer": "~1.0", - "symfony/polyfill-mbstring": "~1.0" + "php": ">=8.4", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-intl-grapheme": "^1.0", + "symfony/polyfill-intl-normalizer": "^1.0", + "symfony/polyfill-mbstring": "^1.0" }, "require-dev": { - "symfony/error-handler": "^6.4|^7.0", - "symfony/emoji": "^7.1", - "symfony/http-client": "^6.4|^7.0", - "symfony/intl": "^6.4|^7.0", + "symfony/emoji": "^7.4|^8.0", + "symfony/error-handler": "^7.4|^8.0", + "symfony/http-client": "^7.4|^8.0", + "symfony/intl": "^7.4|^8.0", "symfony/translation-contracts": "^2.5|^3.0", - "symfony/var-exporter": "^6.4|^7.0" + "symfony/var-exporter": "^7.4|^8.0" }, "conflict": { "symfony/translation-contracts": "<2.5"