From 4bd9ad6969485417e1c8243eeba4cef4608e6ec9 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Sun, 5 Nov 2017 18:10:48 -0800 Subject: [Get-(File)Hash] Use namespaces and add Pscx check --- scripts/fetchDependency.ps1 | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'scripts') diff --git a/scripts/fetchDependency.ps1 b/scripts/fetchDependency.ps1 index 4f629dfc9..b884f3da8 100644 --- a/scripts/fetchDependency.ps1 +++ b/scripts/fetchDependency.ps1 @@ -31,6 +31,30 @@ function Get-Credential-Backwards-Compatible() } } +function Get-Hash-SHA265() +{ + if (Test-Command -commandName 'Microsoft.PowerShell.Utility\Get-FileHash') + { + Write-Verbose("Hashing with Microsoft.PowerShell.Utility\Get-FileHash") + $downloadedFileHash = (Get-FileHash -Path $downloadPath -Algorithm SHA256).Hash + } + elseif(Test-Command -commandName 'Pscx\Get-Hash') + { + Write-Verbose("Hashing with Pscx\Get-Hash") + $downloadedFileHash = (Get-Hash -Path $downloadPath -Algorithm SHA256).HashString + } + else + { + Write-Verbose("Hashing with .NET") + $hashAlgorithm = [Security.Cryptography.HashAlgorithm]::Create("SHA256") + $fileAsByteArray = [io.File]::ReadAllBytes($downloadPath) + $hashByteArray = $hashAlgorithm.ComputeHash($fileAsByteArray) + $downloadedFileHash = -Join ($hashByteArray | ForEach-Object {"{0:x2}" -f $_}) + } + + return $downloadedFileHash.ToLower() +} + if (Test-Module -moduleName 'BitsTransfer') { Import-Module BitsTransfer -Verbose:$false @@ -233,20 +257,7 @@ function SelectProgram([Parameter(Mandatory=$true)][string]$Dependency) performDownload $Dependency $url $downloadsDir $downloadPath $downloadVersion $requiredVersion - #calculating the hash - if (Test-Command -commandName 'Get-FileHash') - { - $downloadedFileHash = (Get-FileHash -Path $downloadPath -Algorithm SHA256).Hash - } - else - { - $hashAlgorithm = [Security.Cryptography.HashAlgorithm]::Create("SHA256") - $fileAsByteArray = [io.File]::ReadAllBytes($downloadPath) - $hashByteArray = $hashAlgorithm.ComputeHash($fileAsByteArray) - $downloadedFileHash = -Join ($hashByteArray | ForEach-Object {"{0:x2}" -f $_}) - } - - $downloadedFileHash = $downloadedFileHash.ToLower() + $downloadedFileHash = Get-Hash-SHA265 $downloadPath if ($expectedDownloadedFileHash -ne $downloadedFileHash) { Write-Host ("`nFile does not have expected hash:`n" + -- cgit v1.2.3