PowerShell Cryptographic Hash Checker

I decided to build a little cryptographic hash checker in PowerShell leveraging some built-in cmdlets and other functions. While there are lots of tools that already do this, many with very nice GUIs I decided it would be fun to build one of my own. The newly made cmdlet is very simple. You select what cryptographic hash algorithm you want to use (MD5, SHAx) and then provide the function with the original hash. Next you select the file you want to compare and it tells you whether the two hashes are a match.

I’m no coding expert so I’m sure this could be streamlined and optimised and this is something I do intend to do, especially to make it work nicely on a Windows Server Core installation where the Explorer file select menu call doesn’t work.

If anyone wishes to comment or provide feedback please feel free to comment below or drop me a message on Twitter.

Example

Assume we have downloaded a file from a website/internal file server and we wish to check it is as expected. In my case I will use a simple text file called ‘Test.txt’.

  • Test.txt SHA1 hash = 26D82F1931CBDBD83C2A6871B2CECD5CBCC8C26B

Next I will run my function, selecting SHA1 as the hashing mechanism and then selecting the test file.

PS C:\> check-hash
1. MD5
2. SHA1
3. SHA256
4. Exit
Please select a hashing option -: 2
SHA1
Please enter the hash you wish to validate: 26D82F1931CBDBD83C2A6871B2CECD5CBCC8C26B
Please select the file you wish to hash
MATCH

Now I will run the same test but I will enter a different hash to emulate some form of corruption or other change resulting in a hash mismatch.

PS C:\> check-hash
1. MD5
2. SHA1
3. SHA256
4. Exit
Please select a hashing option -: 2
SHA1
Please enter the hash you wish to validate: 1234567891234567891234567891234567891234
Please select the file you wish to hash
NO MATCH
The computed hash is 26D82F1931CBDBD83C2A6871B2CECD5CBCC8C26B 
The provided hash is 1234567891234567891234567891234567891234

As you can see it’s a pretty basic system. The Microsoft get-filehash cmdlet does of course provide you with a hash but I wanted a nice way to easily compare the value as well as something I can give to a ‘lay’ person at work who may need to do this regularly but without having to install extra software or learn PowerShell cmdlet usage.

Below is the full function –

# Function to check a file hash
# Author - Alex Bytes
# Source - http://www.bytesizedalex.com/
# Date - 23-11-15
# Version 1.0
#


# This is where we declare our main function.

function check-hash
{
[int]$menuChoice = 0
while ( $menuChoice -lt 1 -or $menuChoice -gt 4 )
{
Write-host "1. MD5"
Write-host "2. SHA1"
Write-host "3. SHA256"
Write-host "4. Exit"

[Int]$menuChoice = read-host "Please select a hashing option -" 
}

Switch ($MenuChoice)

{
 1{"MD5"}
 2{"SHA1"}
 3{"SHA256"}
 4{exit}
}

if ($menuChoice -eq "1")
{
$hashAlgorithm = "MD5"
}
elseif ($menuChoice -eq "2")
{
$hashAlgorithm = "SHA1"
}
elseif ($menuChoice -eq "3")
{
$hashAlgorithm = "SHA256"
}

verify-hash

}


#
# REQUIRED FUNCTIONS
#

# Function to open an Explorer window for user to select file

Function Get-File($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
$OpenFileDialog.ShowHelp = $true
}


# Function to verify the hash based on gathered information

function verify-hash
{
$originalHash = Read-host "Please enter the hash you wish to validate"
$filePath = (write-host "Please select the file you wish to hash") | Get-File
$actualHash = Get-FileHash -Path $filePath -Algorithm $hashAlgorithm
if ($originalHash -eq ($actualHash.Hash))
{
Write-Host "MATCH"
}
else
{
Write-Host "NO MATCH"
Write-Host 'The computed hash is '$actualHash.hash''
Write-Host "The provided hash is $originalHash"
}

}

 

I have added a few comments but I think it’s pretty self explanatory for the most part – if not let me know!

1 thought on “PowerShell Cryptographic Hash Checker”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.