mirror of
https://github.com/friendica/friendica
synced 2025-03-13 11:08:25 +00:00
Merge pull request #14789 from Art4/fix-randomdigits
Fix Crypto::randomDigits()
This commit is contained in:
commit
87e3d8c790
2 changed files with 13 additions and 5 deletions
|
@ -299,12 +299,19 @@ class Crypto
|
|||
* Creates cryptographic secure random digits
|
||||
*
|
||||
* @param string $digits The count of digits
|
||||
* @return int The random Digits
|
||||
* @return string The random Digits
|
||||
*
|
||||
* @throws \Exception In case 'random_int' isn't usable
|
||||
*/
|
||||
public static function randomDigits($digits)
|
||||
public static function randomDigits($digits): string
|
||||
{
|
||||
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 $rn;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue