Skip to content

Commit 4f4a110

Browse files
committed
fix: use random_int() in random_string('numeric')
1 parent 8072c8d commit 4f4a110

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

system/Helpers/text_helper.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ function random_string(string $type = 'alnum', int $len = 8): string
543543
{
544544
switch ($type) {
545545
case 'alnum':
546-
case 'numeric':
547546
case 'nozero':
548547
case 'alpha':
549548
switch ($type) {
@@ -555,17 +554,19 @@ function random_string(string $type = 'alnum', int $len = 8): string
555554
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
556555
break;
557556

558-
case 'numeric':
559-
$pool = '0123456789';
560-
break;
561-
562557
case 'nozero':
563558
$pool = '123456789';
564559
break;
565560
}
566561

567562
return substr(str_shuffle(str_repeat($pool, (int) ceil($len / strlen($pool)))), 0, $len);
568563

564+
case 'numeric':
565+
$max = 10 ** $len - 1;
566+
$rand = random_int(0, $max);
567+
568+
return sprintf('%0' . $len . 'd', $rand);
569+
569570
case 'md5':
570571
return md5(uniqid((string) mt_rand(), true));
571572

0 commit comments

Comments
 (0)