mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2026-02-18 10:08:00 -05:00
Fixes random chars function to truly generate unpredictable character sequences
This commit is contained in:
parent
440a04659c
commit
ba6b35da94
1 changed files with 20 additions and 5 deletions
|
|
@ -11,13 +11,28 @@ function Get-IcingaRandomChars()
|
|||
return $RandomChars;
|
||||
}
|
||||
|
||||
while ($Count -gt 0) {
|
||||
[int]$SymbolLength = $Symbols.Length;
|
||||
$CryptoProvider = New-Object System.Security.Cryptography.RNGCryptoServiceProvider;
|
||||
$ByteValue = New-Object Byte[] 4;
|
||||
$maxValid = [uint32]::MaxValue - ([uint32]::MaxValue % $SymbolLength);
|
||||
|
||||
[int]$SymbolLength = $Symbols.Length;
|
||||
$RandomValue = Get-Random -Minimum 0 -Maximum ($SymbolLength - 1);
|
||||
$RandomChars += $Symbols[$RandomValue];
|
||||
$Count -= 1;
|
||||
for ($index = 0; $index -lt $Count; $index++) {
|
||||
do {
|
||||
# Generate random bytes
|
||||
$CryptoProvider.GetBytes($ByteValue);
|
||||
$RandomNumber = [BitConverter]::ToUInt32($ByteValue, 0);
|
||||
# Ensure the random number is within the valid range to avoid maximum security
|
||||
} while ($RandomNumber -ge $maxValid);
|
||||
|
||||
# Calculate the index for the symbol array
|
||||
$randomIndex = $RandomNumber % $SymbolLength;
|
||||
$RandomChars += $Symbols[$randomIndex];
|
||||
}
|
||||
|
||||
# Clean up
|
||||
$CryptoProvider.Dispose();
|
||||
$CryptoProvider = $null;
|
||||
$ByteValue = $null;
|
||||
|
||||
return $RandomChars;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue