With PowerShell entering the IoT world, sometimes it is necessary to deal with binary sensor data and ancient checksum models to verify data integrity.
Here is a real-world example how PowerShell can take sensor data and validate it using a checksum. The example described here is a specific use case, however the techniques employed may well be useful for similar scenarios.
In this example, PowerShell receives a series of hex data ($data). The checksum is the last byte (3A). It is defined as the sum of all bytes in the data package. From this sum, the least significant byte represents the checksum, however only when its bits are inverted. Sounds strange but makes sense. This way, the checksum is always just one byte.
Now here is how PowerShell can handle these requirements, compare the checksum with the calculated checksum, and test the data integrity:
$data = '00030028401D2E8D4022C0EE4022C0E64022C0E6418B4ACD419FE7B641A05F0E41A060D041A061C23F0A7CDA3A' # checksum is last byte $checksum = [Convert]::ToByte($data.Substring($data.Length-2,2), 16) # remove checksum from data $data = $data.Substring(0, $data.Length -2) # sum up all bytes $sum = $data -split '(?<=\G.{2})(?=.)' | Foreach-Object {$c = 0}{$c+=[Convert]::ToByte($_,16)}{ $c } # get the least significant byte $lsb = ([Net.IPAddress]$sum).GetAddressBytes()[0] # invert bits $checksumReal = $lsb -bxor 0xFF # compare if ($checksum -ne $checksumReal) { throw "Checksum does not match" } else { Write-Warning "Checksum ok" }
Your learning points:
- Use [Convert]::ToByte($number, 16) to convert hex strings to numbers
- With regular expressions, you can easily split a character stream (i.e. hex pairs) into a list of pairs so you can calculate the sum
- By converting a number to an IPAddress, you get easy access to the most and least significant bytes (LSB, MSB)
- Use -bxor to invert bits
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!