gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Heh... is there someone that can help me with checksum?

#33977 - LOst? - Tue Jan 11, 2005 9:39 pm

I do understand the concept of CheckSum, but how can I write one. Is there anything I need to think about? Is it just to add each byte and store it in a word or dword?

Code:
while (buffer++)
{
 checksum += *buffer;
}


I feel I have missed something important, haven't I?

#33979 - tepples - Tue Jan 11, 2005 10:17 pm

Some people use "checksum" to mean only the arithmetic sum of bytes; others use it in a more general sense to refer to any message digest value used for hash table lookup, channel error detection, or authentication. What do you want to use the checksum for? Are you trying to match a particular format's checksum, or are you trying to make a hash table, or are you trying to check the consistency of a savegame or multiplayer comms packet?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33991 - LOst? - Wed Jan 12, 2005 4:11 am

tepples wrote:
Some people use "checksum" to mean only the arithmetic sum of bytes; others use it in a more general sense to refer to any message digest value used for hash table lookup, channel error detection, or authentication. What do you want to use the checksum for? Are you trying to match a particular format's checksum, or are you trying to make a hash table, or are you trying to check the consistency of a savegame or multiplayer comms packet?


Arithmetic sum of bytes for damage protection of data. I guess you can use message digest for that too since the hash will not be the same if there was any change in the data, but that's too advanced.

So all I need to do is add very byte with eachother and check the sum? What if the sum is bigger than the unsigned word or dword I will be using? Does it really matter in the end if the sum was bigger? Does it matter if I use signed or unsigned dwords?

#33992 - tepples - Wed Jan 12, 2005 4:22 am

LOst? wrote:
So all I need to do is add very byte with eachother and check the sum? What if the sum is bigger than the unsigned word or dword I will be using?

If you're adding less than 256 bytes of data, it'll be smaller than an unsigned short; if you're adding less than 16 MB of data, it'll be smaller than an unsigned long.

Quote:
Does it really matter in the end if the sum was bigger?

Not really. Just let it overflow.

Quote:
Does it matter if I use signed or unsigned dwords?

You'll probably want to use unsigned, but it shouldn't matter too much.

Checksum and XOR aren't exactly the best algorithms for error detection because they don't catch transpositions ("hello world" and "hello wrold" are the same) nor certain modifications to the data ("hello World" vs. "Hello world" vs. "hello worLd"). I'd suggest something that mixes the data up better such as CRC32.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33993 - LOst? - Wed Jan 12, 2005 4:34 am

tepples wrote:
LOst? wrote:
So all I need to do is add very byte with eachother and check the sum? What if the sum is bigger than the unsigned word or dword I will be using?

If you're adding less than 256 bytes of data, it'll be smaller than an unsigned short; if you're adding less than 16 MB of data, it'll be smaller than an unsigned long.

Quote:
Does it really matter in the end if the sum was bigger?

Not really. Just let it overflow.

Quote:
Does it matter if I use signed or unsigned dwords?

You'll probably want to use unsigned, but it shouldn't matter too much.

Checksum and XOR aren't exactly the best algorithms for error detection because they don't catch transpositions ("hello world" and "hello wrold" are the same) nor certain modifications to the data ("hello World" vs. "Hello world" vs. "hello worLd"). I'd suggest something that mixes the data up better such as CRC32.


How do I do CRC32? Is it possible to do it on the GBA? That's what I want to do!

#33996 - tepples - Wed Jan 12, 2005 5:18 am

umm... Google it?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#33997 - sajiimori - Wed Jan 12, 2005 6:29 am

It's like watching somebody chase quarters through the street. A sparkly acronym in the periphery...

#34020 - Miked0801 - Wed Jan 12, 2005 7:38 pm

Lol. So I gather we were looking for data integrity then right?

#34067 - LOst? - Thu Jan 13, 2005 5:13 am

Miked0801 wrote:
Lol. So I gather we were looking for data integrity then right?

Yea.

And thank you tepples for guiding me through the world of Google. now I feel like an idiot. But I got the CRC32 working!

I don't see why MD5 would be any better. It needs like 64 bytes or was it 128? Anyway, I just need to keep hackers away from my precious hi-score data!

#34076 - tepples - Thu Jan 13, 2005 7:13 am

LOst? wrote:
But I got the CRC32 working!

I don't see why MD5 would be any better.

CRC is good for detecting accidental tampering such as transmission line noise or storage bit rot, but it falls flat on its face for an intentional attack, as a bit of finite group algebra will allow anybody to forge a document with a given CRC. MD5 is much more resistant to an intentional attack.

Quote:
It needs like 64 bytes or was it 128?

MD5 needs 128 bits or 16 bytes, but you can use the first 8 bytes and ignore the rest in a pinch.

Quote:
Anyway, I just need to keep hackers away from my precious hi-score data!

If hackers really want to hack your high scores, they will hack your high scores. They will patch the game to accept a bogus CRC or MD5 when loading the high scores and then write a valid CRC or MD5 when writing them back to SRAM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.