Skip to content

Commit 04cd0cd

Browse files
Merge branch 'random-bytes'
2 parents c89de14 + c504323 commit 04cd0cd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/RandomHash.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,25 @@ public function generate()
3636
$charset = $this->config->getCharset();
3737
$length = $this->config->getLength();
3838

39-
$maxCombinations = pow(strlen($this->config->getCharset()), $this->config->getLength());
39+
$charsetLength = strlen($this->config->getCharset());
40+
$maxCombinations = pow($charsetLength, $this->config->getLength());
4041

4142
for ($i = 0; $i < $this->config->getCount(); $i++) {
4243
if ($maxCombinations <= count($this->skipped)) {
4344
throw InvalidConfigException::maxCombinationReached();
4445
}
4546

47+
$randomBytes = random_bytes($length);
48+
49+
for ($j = $length - 1; $j > 0; $j--) {
50+
$rand = random_int(0, $j);
51+
[$randomBytes[$j], $randomBytes[$rand]] = [$randomBytes[$rand], $randomBytes[$j]];
52+
}
53+
4654
$hash = '';
4755

4856
for ($j = 0; $j < $length; $j++) {
49-
$hash .= $charset[rand(0, strlen($charset) - 1)];
57+
$hash .= $charset[ord($randomBytes[$j]) % $charsetLength];
5058
}
5159

5260
$hash = $this->config->getPrefix().$hash.$this->config->getSuffix();

0 commit comments

Comments
 (0)