PowerShell: Get CRC Signature of a File

2/10/20 Update: xxHash will beat both of these in term of speed. Hence, this blog post is no longer useful.

Why CRC over MD5?

  1. CRC was 230% faster than MD5 on my tests
    1. 5 minutes: 7019 of 14601 processed (48.07 %)
    2. 5 minutes: 3019 of 14601 processed (20.82 %)
  2. Simple file compares may not require complicated hashes as probabilities of “collisions” are extremely low. More info: https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks
  3. CRC is typically used for check sums, rather than security. At this time, MD5 is no longer considered secured. Hence, the use of either protocol should only be for simple purposes.
# getCRC.ps1
# Requires PowerShell 3.0+

$fileToCheck="C:\temp\testFile.txt"

function installGetCrc{
function expandZipfile($file, $destination){
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)

foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item)
}
}

$crcInstalled=(Get-Command getcrc.exe -ErrorAction SilentlyContinue)
if (!($crcInstalled)){
$tempDir="C:\Temp";
$extractionDir="C:\Windows"
$sourceFile = "https://kimconnect.com/wp-content/uploads/2020/01/getcrc.zip";
$destinationFile = "$tempDir\getcrc.zip";
try{[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12}catch{}
New-Item -ItemType Directory -Force -Path $tempDir
New-Item -ItemType Directory -Force -Path $extractionDir
$webclient = New-Object System.Net.WebClient;
$WebClient.DownloadFile($sourceFile,$destinationFile);
expandZipfile $destinationFile -Destination $extractionDir
$crcInstalled=(Get-Command getcrc.exe -ErrorAction SilentlyContinue)
if ($crcInstalled){
write-host "getcrc.exe was not available, and it had been installed.";
return $true;
}else{
write-host "unable to install getcrc.";
return $false;
}
}else{
#write-host "getcrc is available on this system.";
return $true
}
}

function getCrcHash ($file){
if (installGetCrc){
return getcrc.exe $file
}
}

getCrcHash -file $fileToCheck

Leave a Reply

Your email address will not be published. Required fields are marked *