mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
Add Randomizer::getBytesFromString()
method (#9664)
* Add `Randomizer::getBytesFromAlphabet()` method * Rename `getBytesFromAlphabet` to `getBytesFromString` * [ci skip] Add NEWS/UPGRADING for Randomizer::getBytesFromString() Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
This commit is contained in:
parent
1b503a1f52
commit
ac3ecd03af
11 changed files with 238 additions and 7 deletions
|
@ -86,8 +86,6 @@ static zend_object_handlers random_engine_xoshiro256starstar_object_handlers;
|
|||
static zend_object_handlers random_engine_secure_object_handlers;
|
||||
static zend_object_handlers random_randomizer_object_handlers;
|
||||
|
||||
#define RANDOM_RANGE_ATTEMPTS (50)
|
||||
|
||||
static inline uint32_t rand_range32(const php_random_algo *algo, php_random_status *status, uint32_t umax)
|
||||
{
|
||||
uint32_t result, limit;
|
||||
|
@ -124,8 +122,8 @@ static inline uint32_t rand_range32(const php_random_algo *algo, php_random_stat
|
|||
/* Discard numbers over the limit to avoid modulo bias */
|
||||
while (UNEXPECTED(result > limit)) {
|
||||
/* If the requirements cannot be met in a cycles, return fail */
|
||||
if (++count > RANDOM_RANGE_ATTEMPTS) {
|
||||
zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", RANDOM_RANGE_ATTEMPTS);
|
||||
if (++count > PHP_RANDOM_RANGE_ATTEMPTS) {
|
||||
zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", PHP_RANDOM_RANGE_ATTEMPTS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -180,8 +178,8 @@ static inline uint64_t rand_range64(const php_random_algo *algo, php_random_stat
|
|||
/* Discard numbers over the limit to avoid modulo bias */
|
||||
while (UNEXPECTED(result > limit)) {
|
||||
/* If the requirements cannot be met in a cycles, return fail */
|
||||
if (++count > RANDOM_RANGE_ATTEMPTS) {
|
||||
zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", RANDOM_RANGE_ATTEMPTS);
|
||||
if (++count > PHP_RANDOM_RANGE_ATTEMPTS) {
|
||||
zend_throw_error(random_ce_Random_BrokenRandomEngineError, "Failed to generate an acceptable random number in %d attempts", PHP_RANDOM_RANGE_ATTEMPTS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue