diff --git a/ChangeLog.md b/ChangeLog.md index f24885a63..0959e4926 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,9 @@ XP Framework Core ChangeLog ### Heads up! +* Removed deprecated Mersenne Twister algorithm from `util.Random`, see + https://wiki.php.net/rfc/deprecations_php_8_3#global_mersenne_twister + (@thekid) * Removed the deprecated *resolve()* method from `lang.Process`. Use `lang.CommandLine::resolve()` instead! (@thekid) diff --git a/src/main/php/util/Random.class.php b/src/main/php/util/Random.class.php index 88fe2460a..e04c66300 100755 --- a/src/main/php/util/Random.class.php +++ b/src/main/php/util/Random.class.php @@ -5,16 +5,11 @@ /** * This random generator uses PHP's `random_bytes()` and `random_int()` - * functions if available (PHP >= 7.0) and provides alternatives if - * necessary. + * functions and provides alternatives. * - * _Note_: This RNG prefers secure pseudo-random sources. This may be - * slow; for considerably faster results, use Random::MTRAND (which - * uses the Mersenne Twister algorithm) + * _Note_: This RNG prefers secure pseudo-random sources. * - * @see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html * @see http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/ - * @see https://wiki.php.net/rfc/deprecations_php_8_3#global_mersenne_twister * @see https://wiki.php.net/rfc/rng_extension#prng_shootout * @test net.xp_framework.unittest.util.RandomTest */ @@ -22,7 +17,6 @@ class Random { const SYSTEM = 'system'; const OPENSSL = 'openssl'; const URANDOM = 'urandom'; - const MTRAND = 'mtrand'; const BEST = '(best)'; const FAST = '(fast)'; @@ -56,7 +50,7 @@ static function __static() { /** * Creates a new random * - * @param string|string[] $sources One or more of SYSTEM, OPENSSL, URANDOM, MTRAND, BEST, FAST and SECURE + * @param string|string[] $sources One or more of SYSTEM, OPENSSL, URANDOM, BEST, FAST and SECURE * @throws lang.IllegalArgumentException */ public function __construct($sources= self::BEST) { @@ -67,12 +61,6 @@ public function __construct($sources= self::BEST) { $this->ints= self::$sources[$source]['ints'] ?: [$this, 'random']; $this->source= $source; return; - } else if (self::MTRAND === $source) { - trigger_error('MT19937 is deprecated', E_USER_DEPRECATED); - $this->bytes= [self::class, self::MTRAND]; - $this->ints= 'mt_rand'; - $this->source= $source; - return; } } throw new IllegalArgumentException('None of the supplied sources '.implode(', ', $test).' are available'); @@ -118,21 +106,6 @@ private static function urandom($limit) { return $bytes; } - /** - * Implementation using `mt_rand()` - * - * @deprecated - * @param int $limit - * @return string $bytes - */ - private static function mtrand($limit) { - $bytes= ''; - for ($i= 0; $i < $limit; $i++) { - $bytes.= chr((mt_rand() ^ mt_rand()) % 0xFF); - } - return $bytes; - } - /** * Uses source to fetch random bytes and calculates a random int from * that within the given minimum and maximum limits. diff --git a/src/test/php/util/unittest/RandomTest.class.php b/src/test/php/util/unittest/RandomTest.class.php index c339c7ac8..5facacc22 100755 --- a/src/test/php/util/unittest/RandomTest.class.php +++ b/src/test/php/util/unittest/RandomTest.class.php @@ -62,13 +62,6 @@ public function urandom_bytes() { Assert::equals(20, (new Random(Random::URANDOM))->bytes(20)->size()); } - /** @deprecated */ - #[Test] - public function mtrand_bytes() { - Assert::equals(20, (new Random(Random::MTRAND))->bytes(20)->size()); - \xp::gc(); - } - #[Test, Expect(IllegalArgumentException::class), Values([-1, 0])] public function cannot_use_limit_smaller_than_one($limit) { (new Random())->bytes($limit); @@ -104,14 +97,6 @@ public function urandom_int() { Assert::true($random >= 0 && $random <= 10); } - /** @deprecated */ - #[Test] - public function mtrand_int() { - $random= (new Random(Random::MTRAND))->int(0, 10); - Assert::true($random >= 0 && $random <= 10); - \xp::gc(); - } - #[Test, Expect(IllegalArgumentException::class), Values([10, 11])] public function min_cannot_be_larger_or_equal_to_max($min) { (new Random())->int($min, 10);