]> git.mxchange.org Git - friendica.git/commitdiff
Revert "Refactor Crypto::randomDigits()"
authorArt4 <art4@wlabs.de>
Thu, 6 Feb 2025 08:29:14 +0000 (08:29 +0000)
committerArt4 <art4@wlabs.de>
Thu, 6 Feb 2025 08:29:14 +0000 (08:29 +0000)
This reverts commit 940884e4bd0c1f68757e464f46b4e76c1f4da5b1.

src/Util/Crypto.php
tests/Unit/Util/CryptoTest.php

index 4c0e9b72dc965007d3567c3c0748dece4993bd2b..588be8f93214559a33c2dd6ec8f3791c3d177e70 100644 (file)
@@ -305,6 +305,13 @@ class Crypto
         */
        public static function randomDigits($digits)
        {
-               return random_int(0, 10 ** $digits - 1);
+               $rn = '';
+
+               // generating cryptographically secure pseudo-random integers
+               for ($i = 0; $i < $digits; $i++) {
+                       $rn .= random_int(0, 9);
+               }
+
+               return (int) $rn;
        }
 }
index 41fb1e28265e0253033f9369b7e54dcd499e5775..55d24562a91e4a894965d65659827cd75bd6b798 100644 (file)
@@ -21,10 +21,11 @@ class CryptoTest extends TestCase
        {
                $random_int = $this->getFunctionMock('Friendica\Util', 'random_int');
                $random_int->expects($this->any())->willReturnCallback(function ($min, $max) {
-                       return 12345678;
+                       return 1;
                });
 
-               self::assertSame(12345678, Crypto::randomDigits(8));
+               self::assertSame(1, Crypto::randomDigits(1));
+               self::assertSame(11111111, Crypto::randomDigits(8));
        }
 
        public function testDiasporaPubRsaToMe()