Skip to content

Commit

Permalink
Remove deprecated Mersenne Twister algorithm from util.Random
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 23, 2024
1 parent 004aa05 commit 3ed4143
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 45 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 3 additions & 30 deletions src/main/php/util/Random.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@

/**
* 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
*/
class Random {
const SYSTEM = 'system';
const OPENSSL = 'openssl';
const URANDOM = 'urandom';
const MTRAND = 'mtrand';

const BEST = '(best)';
const FAST = '(fast)';
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 0 additions & 15 deletions src/test/php/util/unittest/RandomTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3ed4143

Please sign in to comment.