From 7373e63c6694453c746f87b669c374f058a7abc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=20M=C3=A4ki?= Date: Mon, 3 Jun 2024 13:09:58 +0300 Subject: [PATCH] Add checksum support for Install-FromUri --- Utils.ps1 | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Utils.ps1 b/Utils.ps1 index d70d8fe..6849043 100644 --- a/Utils.ps1 +++ b/Utils.ps1 @@ -318,7 +318,9 @@ function Install-FromUri { [Parameter(mandatory=$true)][string]$Uri, [Parameter(mandatory=$true)][string]$Filename, [Parameter(mandatory=$false)][string]$UnzipFolderName, - [Parameter(mandatory=$false)][string]$UnzippedFilePath + [Parameter(mandatory=$false)][string]$UnzippedFilePath, + [Parameter(mandatory=$false)][string]$MD5, + [Parameter(mandatory=$false)][string]$SHA256 ) Show-Output "Downloading ${Name}" $Path = "${Downloads}\${Filename}" @@ -329,6 +331,23 @@ function Install-FromUri { Show-Output "Downloaded file was not found at ${Path}" return 1 } + if ($PSBoundParameters.ContainsKey("SHA256")) { + $FileHash = (Get-FileHash -Path "${Path}" -Algorithm "SHA256").Hash + if ($FileHash -eq $SHA256) { + Show-Output "SHA256 checksum OK" + } else { + Show-Output -ForegroundColor Red "Downloaded file has an invalid SHA256 checksum. Expected: ${SHA256}, got: ${FileHash}" + return 1 + } + } elseif ($PSBoundParameters.ContainsKey("MD5")) { + $FileHash = (Get-FileHash -Path "${Path}" -Algorithm "MD5").Hash + if ($FileHash -eq $MD5) { + Show-Output "MD5 checksum OK" + } else { + Show-Output -ForegroundColor Red "Downloaded file has an invalid MD5 checksum. Expected: ${MD5}, got: ${FileHash}" + return 1 + } + } if ($File.Extension -eq ".zip") { if (-not $PSBoundParameters.ContainsKey("UnzipFolderName")) { Show-Output -ForegroundColor Red "UnzipFolderName was not provided for a zip file."