From 86fc97012636e10f06e1e10e87b6c0186aa771ff Mon Sep 17 00:00:00 2001 From: KASR Date: Mon, 24 Apr 2023 10:43:52 +0200 Subject: [PATCH] verify sha256 checksums for windows users feat: Add PowerShell script to verify file hashes This commit adds a PowerShell script that can be used to verify the hash sums of model files on Windows. The script reads the 'SHA256SUMS' file with the expected hash sums and filenames, calculates the SHA256 hash of each file using `certUtil`, and compares it with the expected hash. The output is a table with the filename, valid checksum (if the hash matches), and file missing (if the file is not found). --- check_SHA256_windows.ps1 | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 check_SHA256_windows.ps1 diff --git a/check_SHA256_windows.ps1 b/check_SHA256_windows.ps1 new file mode 100644 index 000000000..fa44b0ea0 --- /dev/null +++ b/check_SHA256_windows.ps1 @@ -0,0 +1,69 @@ +# Get the working directory +$modelsPath = $pwd + +# Define the file with the list of hashes and filenames +$hashListPath = "SHA256SUMS" + +# Check if the hash list file exists +if (-not(Test-Path -Path $hashListPath)) { + Write-Error "Hash list file not found: $hashListPath" + exit 1 +} + +# Read the hash file content and split it into an array of lines +$hashList = Get-Content -Path $hashListPath +$hashLines = $hashList -split "`n" + +# Create an array to store the results +$results = @() + +# Loop over each line in the hash list +foreach ($line in $hashLines) { + + # Split the line into hash and filename + $hash, $filename = $line -split " " + + # Get the full path of the file by joining the models path and the filename + $filePath = Join-Path -Path $modelsPath -ChildPath $filename + + # Informing user of the progress of the integrity check + Write-Host "Verifying the checksum of $filePath" + + # Check if the file exists + if (Test-Path -Path $filePath) { + + # Calculate the SHA256 checksum of the file using certUtil + $fileHash = certUtil -hashfile $filePath SHA256 | Select-Object -Index 1 + + # Remove any spaces from the hash output + $fileHash = $fileHash -replace " ", "" + + # Compare the file hash with the expected hash + if ($fileHash -eq $hash) { + $validChecksum = "V" + $fileMissing = "" + } + else { + $validChecksum = "" + $fileMissing = "X" + } + } + else { + $validChecksum = "" + $fileMissing = "X" + } + + # Add the results to the array + $results += [PSCustomObject]@{ + "filename" = $filename + "valid checksum" = $validChecksum + "file missing" = $fileMissing + } +} + +# Output the results as a table +$results | Format-Table ` + -Property @{Expression={$_.filename}; Label="filename"; Alignment="left"}, ` + @{Expression={$_."valid checksum"}; Label="valid checksum"; Alignment="center"}, ` + @{Expression={$_."file missing"}; Label="file missing"; Alignment="center"} ` + -AutoSize \ No newline at end of file