From 940884e4bd0c1f68757e464f46b4e76c1f4da5b1 Mon Sep 17 00:00:00 2001 From: Art4 Date: Sat, 9 Nov 2024 23:18:13 +0000 Subject: [PATCH] Refactor Crypto::randomDigits() --- src/Util/Crypto.php | 9 +-------- tests/src/Util/CryptoTest.php | 5 ++--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Util/Crypto.php b/src/Util/Crypto.php index ba3c46bbc3..8113697639 100644 --- a/src/Util/Crypto.php +++ b/src/Util/Crypto.php @@ -306,13 +306,6 @@ class Crypto */ public static function randomDigits($digits) { - $rn = ''; - - // generating cryptographically secure pseudo-random integers - for ($i = 0; $i < $digits; $i++) { - $rn .= random_int(0, 9); - } - - return (int) $rn; + return random_int(0, 10 ** $digits - 1); } } diff --git a/tests/src/Util/CryptoTest.php b/tests/src/Util/CryptoTest.php index 544561bc3b..dae87865b9 100644 --- a/tests/src/Util/CryptoTest.php +++ b/tests/src/Util/CryptoTest.php @@ -28,11 +28,10 @@ class CryptoTest extends TestCase { $random_int = $this->getFunctionMock('Friendica\Util', 'random_int'); $random_int->expects($this->any())->willReturnCallback(function($min, $max) { - return 1; + return 12345678; }); - self::assertSame(1, Crypto::randomDigits(1)); - self::assertSame(11111111, Crypto::randomDigits(8)); + self::assertSame(12345678, Crypto::randomDigits(8)); } public function dataRsa(): array