Quote: "In your example, the application needs to know what it will receive, correct?"
No, it doesn't need to know. When you create the hash, it is an encrypted record of the data at the time you wrote it to the file. You can change the file contents all of the time (like a hiscore file) so long as you recreate the hash key each time you save the file.
When you read the file (script), you read the hash key that you saved with it. You then create the hash key again based on the data you read in, exactly as you did when you saved the file. Now you have the read-in hash key and the newly generated hash key. As long as they are the same, you know that nobody altered the script.
e.g create a file that contains:
The Quick Brown Fox
ABCD123
Read the file, it contains:
The Quick Brown Fox
ABCD123
Hash "The Quick Brown Fox" again and you get:
Hash Key = "ABCD123"
ABCD123 = ABCD123, Everything is OK
Read the file, it contains:
The Slow Blue Fox
ABCD123
Hash "The Slow Blue Fox" and you get:
Hash Key = "EFGH456"
ABCD123 != DEFG456
Therefore the file was edited.
I prefer this method so that I can still read my parameter files visually.
I also have a dummy hash key (e.g 99999). If the file contains the dummy key, I know that
I have edited the file, skip the hash key check and rewrite the file with a genuine hash key in it.
I start the hash key line with "HSH" so I know this is the hash key line.
[EDIT][ By the way, I used to do
almost the same as you, you'll find a tutorial about it in an old edition of the newsletter I wrote. I use Sha1() now because it is neater and more secure.
Quidquid latine dictum sit, altum sonatur