From ba6b35da94960c1fd7bff1334d395a5f31bc300b Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Mon, 2 Jun 2025 16:30:00 +0200 Subject: [PATCH] Fixes random chars function to truly generate unpredictable character sequences --- lib/core/windows/Get-IcingaRandomChars.psm1 | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/core/windows/Get-IcingaRandomChars.psm1 b/lib/core/windows/Get-IcingaRandomChars.psm1 index 2c4f144..1bc97b1 100644 --- a/lib/core/windows/Get-IcingaRandomChars.psm1 +++ b/lib/core/windows/Get-IcingaRandomChars.psm1 @@ -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; }