Help with checksum-8

Help on how to use HxD.
Post Reply
asdvw43
Posts: 10
Joined: 29 Jun 2022 19:54

Help with checksum-8

Post by asdvw43 »

Hi there

I am editing files that I notice uses checksum-8, I can copy ~270kB data and the HxD checksum-8 corresponds to the file's checksum.

My own checksum doesn't match, it's a sum of all bytes Modulo 0xFF

So if sum would be 0x45678 my checksum would be 0x78, the last byte of sum (that itself is a new discovery, it's the very last byte...)

in Visual Studio Basic: checksum = sum Mod &HFF

What is the checksum-8 formula?
Maël
Site Admin
Posts: 1455
Joined: 12 Mar 2005 14:15

Re: Help with checksum-8

Post by Maël »

I wrote this a while ago, see here:
viewtopic.php?p=1077#p1077
Maël
Site Admin
Posts: 1455
Joined: 12 Mar 2005 14:15

Re: Help with checksum-8

Post by Maël »

The modulo needs to be 256 ($100) not 255 ($FF). You also have to make sure the variable holding the intermediate results can hold them (i.e., handle values that large) before you apply the modulo. If you use a UInt8 type for example, it will automatically apply the modulo, by cutting off too large numbers (>255), automatically, since they don't fit.

But how the cutting off works (using modulo) for UIntX is an implementation detail, so it's better to explicitly use the modulo operation, and use a larger integer, such as UInt32.
Maël
Site Admin
Posts: 1455
Joined: 12 Mar 2005 14:15

Re: Help with checksum-8

Post by Maël »

asdvw43 wrote: 25 Jan 2023 16:34 So if sum would be 0x45678 my checksum would be 0x78, the last byte of sum (that itself is a new discovery, it's the very last byte...)
Yes that's because the data is stored in little endian on x86 CPUs.

So 0x45678 is really 0x045678 which means it is represented by four hex bytes (the CPU has 16 bit or 32 bit registers, no 24 bit registers, and a 16 bit register is too small, so 32 bit register it ist).

Therefore the hex bytes for 0x045678 are (in little endian order): 78 56 04 00. When you cast that data to an UInt8, only the first byte is considered, so you get 0x78. This is the same effect as computing 0x045678 mod $100, eventhough the method to reach the same result was different.
Notice it is $100 not $FF! 0x045678 and $FF would result in $78 as well, because here you apply a bitmask, i.e., keep only the bits that are 1 in both values.

That's why it's best to keep it generic, and use modulo and not rely on casting (or accidentally use type casting). Especially if your checksum modulo is not a power of two (rarely the case, because of efficiency considerations, but still it's good to first understand the principle, before relying on implementation details to optimize).
asdvw43
Posts: 10
Joined: 29 Jun 2022 19:54

Re: Help with checksum-8

Post by asdvw43 »

thx, there were few faults in my code, checksum needs to be larger than byte (Integer I set) and Modulo 0x100, 256. I also found a bug that read too many bytes data

Tested with the 4 files I have, all 4 files modified become 100% original with the calculated checksum, so it has been fixed ! thx again
Post Reply