Skip to content

Commit e0b5a53

Browse files
[10.x] Random function doesn't generate evenly distributed random chars (#45916)
* Bugfix: The random function does not generate evenly distributed random characters particularly for short strings (<12 chars) * fixed styling * fixed styling * formatting --------- Co-authored-by: Jan Kramer <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0b74637 commit e0b5a53

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

Str.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,9 @@ public static function random($length = 16)
735735
while (($len = strlen($string)) < $length) {
736736
$size = $length - $len;
737737

738-
$bytes = random_bytes($size);
738+
$bytesSize = (int) ceil(($size) / 3) * 3;
739+
740+
$bytes = random_bytes($bytesSize);
739741

740742
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
741743
}

0 commit comments

Comments
 (0)