icinga-powershell-framework/lib/core/windows/Get-IcingaRandomChars.psm1
Lord Hepipud 854ef78f91
Fix: Improved security for random character generator (#814)
Fixes random chars function to truly generate unpredictable character sequences and to replace `Get-Random` which is not entirely secure
2025-06-16 14:14:05 +02:00

38 lines
1.2 KiB
PowerShell

function Get-IcingaRandomChars()
{
param (
[int]$Count = 10,
[string]$Symbols = 'abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ1234567890!§$%()=?}][{@#*+'
);
$RandomChars = '';
if ([string]::IsNullOrEmpty($Symbols)) {
return $RandomChars;
}
[int]$SymbolLength = $Symbols.Length;
$CryptoProvider = New-Object System.Security.Cryptography.RNGCryptoServiceProvider;
$ByteValue = New-Object Byte[] 4;
$maxValid = [uint32]::MaxValue - ([uint32]::MaxValue % $SymbolLength);
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;
}