Avoid ConvertTo-SecureString -AsPlainText -Force (#40658)

This commit is contained in:
Billy O'Neal 2024-08-27 12:34:26 -07:00 committed by GitHub
parent 91b615d88a
commit b6fcbeaa7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -44,30 +44,14 @@ The length of the returned password.
#> #>
function New-Password { function New-Password {
Param ([int] $Length = 32) Param ([int] $Length = 32)
# This 64-character alphabet generates 6 bits of entropy per character.
# The power-of-2 alphabet size allows us to select a character by masking a random Byte with bitwise-AND.
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-" $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"
$mask = 63
if ($alphabet.Length -ne 64) { if ($alphabet.Length -ne 64) {
throw 'Bad alphabet length' throw 'Bad alphabet length'
} }
[Byte[]]$randomData = [Byte[]]::new($Length) $result = New-Object SecureString
$rng = $null
try {
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rng.GetBytes($randomData)
}
finally {
if ($null -ne $rng) {
$rng.Dispose()
}
}
$result = ''
for ($idx = 0; $idx -lt $Length; $idx++) { for ($idx = 0; $idx -lt $Length; $idx++) {
$result += $alphabet[$randomData[$idx] -band $mask] $result.AppendChar($alphabet[[System.Security.Cryptography.RandomNumberGenerator]::GetInt32($alphabet.Length)])
} }
return $result return $result
@ -109,8 +93,7 @@ function Wait-Shutdown {
$AdminPW = New-Password $AdminPW = New-Password
$AdminPWSecure = ConvertTo-SecureString $AdminPW -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential ("AdminUser", $AdminPW)
$Credential = New-Object System.Management.Automation.PSCredential ("AdminUser", $AdminPWSecure)
$VirtualNetwork = Get-AzVirtualNetwork -ResourceGroupName 'vcpkg-image-minting' -Name 'vcpkg-image-mintingNetwork' $VirtualNetwork = Get-AzVirtualNetwork -ResourceGroupName 'vcpkg-image-minting' -Name 'vcpkg-image-mintingNetwork'
@ -299,3 +282,5 @@ Remove-AzNetworkInterface -ResourceGroupName 'vcpkg-image-minting' -Name $NicNam
Write-Progress -Activity $ProgressActivity -Completed Write-Progress -Activity $ProgressActivity -Completed
Write-Host "Generated Image: $GalleryImageVersion" Write-Host "Generated Image: $GalleryImageVersion"
Write-Host 'Finished!' Write-Host 'Finished!'
$AdminPW.Dispose()