I could not find any simple Powershell-function to calculate the CRC32 of a given string. Therefore I decided to write my own function which I would like to share in my answer.
Here the CRC32 function:
function CRC32($str) {
$crc = [uint32]::MaxValue
$poly = [uint32]0xEDB88320L
$bytes = [System.Text.Encoding]::UTF8.GetBytes($str)
foreach ($byte in $bytes) {
$crc = ($crc -bxor $byte)
foreach ($bit in 0..7) {
$crc = ($crc -shr 1) -bxor ($poly * ($crc -band 1))
}
}
return [uint32]::MaxValue - $crc
}
$str = "123456789"
$crc32 = CRC32 $str
Write-Host "CRC32 of '$str' is: 0x$($crc32.ToString("X8"))"
I decided skipping the creation of a lookup-table which means I have to do a bitwise-loop for each byte. Therefore it should only be used mainly for shorter strings (calculation time of a string with a length of 1MB is still below 1sec).