Skip to content

Instantly share code, notes, and snippets.

@gravejester
Last active November 28, 2021 22:48
Show Gist options
  • Save gravejester/f9939d3062f6d7875f227fe9afd866f6 to your computer and use it in GitHub Desktop.
Save gravejester/f9939d3062f6d7875f227fe9afd866f6 to your computer and use it in GitHub Desktop.
function Get-CRC32 {
<#
.SYNOPSIS
Calculate CRC.
.DESCRIPTION
This function calculates the CRC of the input data using the CRC32 algorithm.
.EXAMPLE
Get-CRC32 $data
.EXAMPLE
$data | Get-CRC32
.NOTES
C to PowerShell conversion based on code in https://www.w3.org/TR/PNG/#D-CRCAppendix
Author: Øyvind Kallstad
Date: 06.02.2017
Version: 1.0
.INPUTS
byte[]
.OUTPUTS
uint32
.LINK
https://communary.net/
.LINK
https://www.w3.org/TR/PNG/#D-CRCAppendix
#>
[CmdletBinding()]
param (
# Array of Bytes to use for CRC calculation
[Parameter(Position = 0, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[byte[]]$InputObject
)
Begin {
function New-CrcTable {
[uint32]$c = $null
$crcTable = New-Object 'System.Uint32[]' 256
for ($n = 0; $n -lt 256; $n++) {
$c = [uint32]$n
for ($k = 0; $k -lt 8; $k++) {
if ($c -band 1) {
$c = (0xEDB88320 -bxor ($c -shr 1))
}
else {
$c = ($c -shr 1)
}
}
$crcTable[$n] = $c
}
Write-Output $crcTable
}
function Update-Crc ([uint32]$crc, [byte[]]$buffer, [int]$length) {
[uint32]$c = $crc
if (-not($script:crcTable)) {
$script:crcTable = New-CrcTable
}
for ($n = 0; $n -lt $length; $n++) {
$c = ($script:crcTable[($c -bxor $buffer[$n]) -band 0xFF]) -bxor ($c -shr 8)
}
Write-output $c
}
$dataArray = @()
}
Process {
foreach ($item in $InputObject) {
$dataArray += $item
}
}
End {
$inputLength = $dataArray.Length
Write-Output ((Update-Crc -crc 0xffffffffL -buffer $dataArray -length $inputLength) -bxor 0xffffffffL)
}
}
[byte[]]$testCrc = 44,6,119,207
[byte[]]$testData = 73,72,68,82,0,0,0,32,0,0,0,32,1,0,0,0,1
# should be the same
[array]::Reverse($testCrc)
[System.BitConverter]::ToUInt32($testCrc,0)
Get-CRC32 $testData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment