Generating a CheckSum


A CheckSum is a single number generated from a larger amount of data. Its original use was to provide a way of checking that a message had been transmitted correctly. The checksum would be calculated by the sender and sent in the same tranmission as the text of the message. The receiver would recalculate the checksum of the message received and compare this with the checksum transmitted. If the two numbers matched then that would indicate that the message had been received correctly. If they did not match then either the message or the CheckSum must have been corrupted and the recipient would request a repeat transmission.

Fox provides two functions which you can use to generate a characteristic 16-bit or 32-bit number. One generates a checksum from a string, the other generates a checksum from a complete record.

Sys 2007

Use the Sys 2007() function to generate a CheckSum from a string:

?Sys(2007, 'Test')

will print "10376", the 16-bit checksum of the word "Test". Note that this is a numeric string, not an integer.

If you pass two extra parameters to Sys(2007) then you can generate a 32-bit checksum:

?Sys(2007, 'Test', 0, 1)

will print "2018365746", the 32-bit checksum of the word "Test". This too is a numeric string, not an integer.

Note that both checksum algorithms are case-sensitive because they are dealing with the ASCII representation of the characters. The 16-bit checksum for the word "test" is "8134" rather than the "10376" that was calculated for "Test".

You can improve the security of your application by storing the checksum of a password instead of the password itself. When the user logs in, take the checksum of the password they enter and compare it with the number stored in the login table.

Using a checksum in this way means that nobody - not even the developer - can read the login table and see a user's password in plain view. If they have a copy of FoxPro and if they have plenty of time to spare then they could try calculating the checksums of all the likely passwords. This is a long job but not a totally impossible one so do not rely on the simple built-in checksum if your system must have absolute security.

Sys 2017

Use the Sys(2017) function to generate a CheckSum from the current record inthe current work area:

?Sys(2017)

You can use this function to detect unauthorised changes to data. Add an extra field to the table and use Sys(2017) to calculate the checksum whenever a record is added or modified. Any authorised change to the data will be matched by a change to the stored value of the checksum. If the checksum does not match then the data must have been changed by somebody who was not using the correct data entry forms.

Another use for Sys 2017 is to detect whether the user has made changes to the current record. Take the CheckSum as the form loads and again when the user tries to close it. If both values are the same then no changes have been made to the data. You can use the GetFldState() function to give this information for buffered tables but that will record a change if the user alters a field and then undoes the change.

Sys(2017) can take extra parameters which force it to include Memo fields or to exclude certain named fields from the calculation.


Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map