# Verify-Hashes.ps1
param(
[Parameter(Mandatory=$true)]
[string]$HashListFile,
[Parameter(Mandatory=$false)]
[string]$Algorithm = "SHA256"
)
function Verify-Hashes {
param(
[string]$HashListPath,
[string]$HashAlgorithm
)
# Check if hash list file exists
if (-not (Test-Path $HashListPath)) {
Write-Error "Hash list file not found: $HashListPath"
return
}
# Read hash list file
$hashEntries = Get-Content $HashListPath
Write-Host "Verifying files against hash list..." -ForegroundColor Cyan
Write-Host "Algorithm: $HashAlgorithm" -ForegroundColor Cyan
Write-Host ("-" * 60) -ForegroundColor Cyan
$verifiedCount = 0
$failedCount = 0
$missingCount = 0
foreach ($entry in $hashEntries) {
# Skip empty lines and comments
if ([string]::IsNullOrWhiteSpace($entry) -or $entry.Trim().StartsWith("#")) {
continue
}
# Parse the line (format: "hash filename")
$parts = $entry -split '\s+', 2
if ($parts.Length -lt 2) {
Write-Warning "Invalid format in hash list: $entry"
continue
}
$expectedHash = $parts[0].ToUpper()
$fileName = $parts[1].Trim()
# Check if file exists
if (-not (Test-Path $fileName)) {
Write-Host "[MISSING] $fileName" -ForegroundColor Red
$missingCount++
continue
}
# Calculate actual hash
try {
$actualHash = (Get-FileHash $fileName -Algorithm $HashAlgorithm).Hash.ToUpper()
# Compare hashes
if ($actualHash -eq $expectedHash) {
Write-Host "[PASS] $fileName" -ForegroundColor Green
$verifiedCount++
} else {
Write-Host "[FAIL] $fileName" -ForegroundColor Red
Write-Host " Expected: $expectedHash" -ForegroundColor Yellow
Write-Host " Actual: $actualHash" -ForegroundColor Yellow
$failedCount++
}
}
catch {
Write-Host "[ERROR] $fileName - $($_.Exception.Message)" -ForegroundColor Red
$failedCount++
}
}
# Summary
Write-Host ("-" * 60) -ForegroundColor Cyan
Write-Host "Verification Summary:" -ForegroundColor Cyan
Write-Host " Verified: $verifiedCount" -ForegroundColor Green
Write-Host " Failed: $failedCount" -ForegroundColor Red
Write-Host " Missing: $missingCount" -ForegroundColor Yellow
if ($failedCount -eq 0 -and $missingCount -eq 0) {
Write-Host "All files verified successfully!" -ForegroundColor Green
return $true
} else {
Write-Host "Verification completed with issues." -ForegroundColor Red
return $false
}
}
# Execute the verification
Verify-Hashes -HashListPath $HashListFile -HashAlgorithm $Algorithm