Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 29128

Comparing Hash of downloaded Files with PowerShell

$
0
0

I can’t claim 99% of this – main credit to : http://www.tinyint.com/index.php/2011/09/14/get-an-md5-or-sha1-checksum-with-powershell/

Param (
        [string]$File=$(throw("You must specify a filename to get the checksum of.")),
        [string]$ProvidedHash,
        [ValidateSet("sha1","md5")]
        [string]$Algorithm=("sha1")
)

function Get-Checksum
{
    Param (
        [string]$File=$(throw("You must specify a filename to get the checksum of.")),
        [ValidateSet("sha1","md5")]
        [string]$Algorithm=("sha1")
        
    )

    $fs = new-object System.IO.FileStream $File, “Open”, “Read”, “Read”;
    $algo = [type]"System.Security.Cryptography.$Algorithm"
	$crypto = $algo::Create()
    $hash = [BitConverter]::ToString($crypto.ComputeHash($fs)).Replace("-", "")
    $fs.Close()
    return $hash
}

$calchash = Get-Checksum $File $Algorithm $ProvidedHash

if ( $calchash -eq $ProvidedHash ) {
    Write-Host "hash match" -ForegroundColor Green;
}
else {
    Write-Host "hash doesn't match" -ForegroundColor Red
    write-host "Provided:" $ProvidedHash "Calculated: " $calchash
}

Viewing all articles
Browse latest Browse all 29128

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>