I need to use Powershell to check if two files are the same but with the following restriction: there are eight specific bytes in the first 2K that are allowed to be different (if you're interested, it's certain timestamp bytes in the superblock of an ext4 image).
The code I found on Stack Overflow (obviously) for doing full checks is as follows:
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString(
$md5.ComputeHash([System.IO.File]::ReadAllBytes("fspec.bin")))
This gives me the hash of the entire file but what I really need is:
- the first 2K of the file as a byte array so I can check specifics; and
- the checksum of the remainder of the file to check equality.
The System.IO.File
class has ReadAllBytes
but does not appear to have the capacity to read a section of the file, nor seek to a specific place.
I have attempted to read in the byte array and use array slicing to get the parts as follows:
$restOfFile = [System.IO.File]::ReadAllBytes("fspec")
$firstTwoK = $restOfFile[0..2048]
$restOfFile = $restOfFile[2048..$restOfFile.Length]
# Then:
# 1. Check bytes in firstTwoK.
# 2. Check MD5 of all bytes in restOfFile.
Unfortunately, the fact that it's a 750M file is causing problems:
Array dimensions exceeded supported range.
At C:\testprog\testprog.ps1:42 char:1
+ ${devBytes} = ${devBytes}[2048..${devBytes}.Length]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], OutOfMemoryException
+ FullyQualifiedErrorId : System.OutOfMemoryException
Is there a functional way to do what I need?