]> git.mxchange.org Git - friendica.git/commitdiff
Refactor Crypto::randomDigits()
authorArt4 <art4@wlabs.de>
Sat, 9 Nov 2024 23:18:13 +0000 (23:18 +0000)
committerArt4 <art4@wlabs.de>
Sat, 9 Nov 2024 23:18:13 +0000 (23:18 +0000)
src/Util/Crypto.php
tests/src/Util/CryptoTest.php

index ba3c46bbc3aae0e0a9c82d62095bec269a06fa06..8113697639ef96b176f20c28096874b7e34adad0 100644 (file)
@@ -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);
        }
 }
index 544561bc3ba063b4cc3e91ef434583a522c4856c..dae87865b9f1ffc41147309889aab8182b5bf034 100644 (file)
@@ -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