QB64 Phoenix Edition - Steve's Lesson on Checksums

QB64 Phoenix Edition

You're currently viewing a stripped down version of our content. View the full version with proper formatting.
From time to time, anyone who works or plays on a computer is going to hear the term: checksums.

Do me a favor:  **Never try to look up the definition of a checksum.**

For some insane reason, you'll find folks trying to define checksums by saying they're, "a string of cryptographic representations of hashed data of unique representations of blah blah blah..." 

WTH?!!

A monkey on LSD couldn't decipher what the frick some of these guys are trying to say!!   Talk about overtalking a subject!!

All a checksum is.... dum dum de dumm....  is a SUM of values that can be used to CHECK the validity of something.

OMG!!   That was soooooooooooo hard to explain!!


Now, let me give you guys a few examples of the world's simplest checksums.

For starters, let's assign a few quick values to our uppercase alphabet.  I bet most of you can quickly figure these values out:
A = 1
B = 2
C = 3
...
X = 24
Y = 25
Z = 26

Now we're going to write a quick checksum to check for invalid input.  To do this, we're going to come up with some super-duper-Steve-approved-complex-math...   We're just going to add the values together.

So let's say you send me some data:
"ABC".....  I add the values together.  I get 1 + 2 + 3 = 6.   My checksum is now 6.

You send me some different data:
"XYZ"....  I add the values together.  I get 24 + 25 + 26 = 75.  My checksum is now 75.

Now, there are different ways that I can deal with this checksum.  One is you can send me the value that I'm expected to have, and I can simply compare it to the value I got.  You'll see this method a lot of times when downloading files.

Download this file...  checksums are blahblahblah...   You download the file, run the check on the sums, and see if they match.  If they do, then chances are your data/download is good and valid.  If they don't match, then someone hijacked the download and you may have infected files. Or a corrupted download.  Or whatever...  The thing is, you didn't get the correct data!


Another way you often see checksums in use is as an built-in data verifier.

Have you ever gotten a bill and had it look something like Account Number: 123-45-6789-2D?

Chances are, that account number has a built in checksum to it.  The first 9 digits may be your social security number, while those last 2 digits are the hex representation of those numbers added together.

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45...  which is 2D, if my brain translated the figures right.

When you read your account number off to the billing representative so they'll pay your damn doctor's bill, the person behind the keyboard can almost instantly know if they typed the value correctly, or if they misheard you, or whatnot.  If the two digits on the right don't add up to the sum of the first nine numbers, then it's an invalid account.


Now, of course, these are the very simplest of examples of checksums.  They're not UNIQUE checksums at all.

For example, let's compare the alphabet versions above:
"ABC" = 1 + 2 + 3 = 6
"AAD" = 1 + 1 + 4 = 6

You could tell me your name was "ABC" with 6 for a checksum.  I could screw up (man, was I drunk that day) and type in "AAD" instead, and I'd still get back the same checksum.

Obviously, my math formula could probably use a little tweaking to give more unique answers.  Big Grin

....

So, Steve, being as AMAZING as he is, decides:  EACH digit is going to multiplied by 100^digit !!
"ABC" = 1 * (100 *^1) + 2 * (100 ^ 2)  + 3 * (100 ^ 3).... or 300200100
"AAD" = 1 * (100 ^ 1) + 1 * (100 ^ 2) + 4 * (100 ^ 3)... or 400100100

See -- unique numbers there!!

Which would work fine until someone sent the data "ABCDEFGHIJKLMNO"....  oh crap! OVERFLOW ERROR!!!

.....

So that's not a very good checksum formula either, to be honest.  Big Grin

But that's why we decide to let other folks -- those crazy folks who are obsessed with math and such -- we let them come up with the formulas which we end up making the use of in most cases.

https://qb64phoenix.com/qb64wiki/index.php/CRC32
https://qb64phoenix.com/qb64wiki/index.php/ADLER32
https://qb64phoenix.com/qb64wiki/index.php/MD5$

I'll basically cover those for tomorrow's Extended Keyword of the Day, but basically all they are, are math formulas to create a checksum of whatever data you give them.

And all a checksum is, is a quick way to eliminate invalid data as easily and efficiently as possible.

"ABC" may have a checksum of six, so I might send that data to someone else as "ABC6".   If they get it and it's "ABD6", just by doing the same math as I did, it's easy to see that there's no way the data they got is going to be valid.  That last digit doesn't match the checksum of the first few digits!

Checksums.... just a means to check the value of something else.

Nothing complicated to see here, no matter how complex some folks want to try to obfuscate the definition.  Big Grin