In my previous post, I discussed how you can use the HashBytes() function to generate hashes.Â I gave a couple of examples where you could use this for your benefit, but a follow-up post is necessary.
Just to recap, a hash is a one-way encryption of a set of data of any size to come up with a smaller (eg, 20 bytes for SHA1) that can almost-uniquely identify the original source.Â It is NOT a compressed version of the source â€“ you cannot reconstruct the original source with the hash.
The main reason for using hashes is to see if things have changed.Â The MDx hashing algorithms (Message Digest) was originally used to determine if email or Usenet messages had been read.Â Instead of sending all messages, you could receive the hashes, and then compare the list of hashes with what you have stored locally, and only download those that you are missing.
A similar use of hashes is used to protect the integrity of executable files available for download.Â You can create a hash of the executable, and when the user has downloaded your file, they can re-generate the hash to ensure that nothing has changed it (for example, a virus latching onto the executable).Â If the hashes do not match, you would be wary of running the program.
Back in the database world, here are three scenarios where you might like to use hashes:
- Password and authentication
- Data Integrity
- Detecting changes to a table
Passwords and authentication
If you have a website and youâ€™ve rolled your own authentication system, one thing you do not want to do is store passwords in your database.Â By running the password through a hashing algorithm, you can store the hash in your database.Â When the user tries to log in, you simply hash the password entered, and compare the two hashes to see if they match.Â This means that you can never recover the password â€“ you can only reset the password.
This looks great from an initial glance, but has some drawbacks.Â If youâ€™ve used a standard hashing algorithm, and an attack can get a copy of the hashed password, it is possible to compare this hash with a known list of hashed passwords, or generate the hashes by brute force until the generated hash matches the hashed password.Â Alternatively, if the attacker has write access to your table, they could insert the hash for a known password onto any account.
A way around this is to â€œsaltâ€ the password.Â Salting involves adding extra information to the password before hashing, and this works well as long as the attacker does not know what the extra information is.Â You could add the username to the password, or add â€œabcd!%*(&â€ to the end of the password, and then hash the result.Â As long as you do this every time you generate your hash, a standard set of pre-hashed values will not match your system.Â Once the attacker has seen your source code, however, they will know how your hashes have been salted.
Data Integrity is another useful feature you can create with hashing.Â Imagine that you donâ€™t want your database administrators to be able to update tables via Management Studio â€“ you only want them to update the tables via a specific interface, whether by an encrypted stored procedure, or through your application.Â You can create a hash column on your table, and create a hash based on the other columns.Â For example, if you had a Student table:
CREATE TABLE Student ( StudentID int NOT NULL IDENTITY PRIMARY KEY , FirstName varchar(25) NOT NULL , Surname varchar(25) NOT NULL , DateOfBirth datetime NOT NULL , EmergencyContactPhone varchar(15) NOT NULL , RowHash varchar(50) )
Now, when you insert your Students, you need to supply a value for the RowHash:
INSERT INTO Student (FirstName, Surname, DateOfBirth , EmergencyContactPhone, RowHash) VALUES ('Robert', 'Jackson', '1995-10-21', '+61399991234' , sys.fn_varbintohexstr(HashBytes('SHA1', 'SALTRobertJackson1995-10-21+61399991234')) )
As you can see, the hash is the word SALT plus the FirstName, Surname, DateOfBirth and EmergencyContactPhone concatenated together.Â Note that if you have CONCATE_NULL_YIELDS_NULL turned off, youâ€™ll end up with an empty string if you try to add a NULL value to a string.
The next step is to look at the data.Â As weâ€™re not encrypting the data, your DBA can happily look at the rows.Â However, in your application, you want to check the hash every time you load in the row to see if it has been modified and the hash not updated correctly.Â For example, to retrieve our Robertâ€™s row:
SELECT FirstName, Surname, DateOfBirth , EmergencyContactPhone, RowHash , sys.fn_varbintohexstr(HashBytes('SHA1', 'SALT' + FirstName + Surname + CONVERT(varchar(10), DateOfBirth, 121) + EmergencyContactPhone)) AS GeneratedHash FROM Student WHERE StudentID = 1
We get the existing RowHash, and a GeneratedHash, which should match.Â Note that you would only generate this new hash in an encrypted stored procedure, or in your application, otherwise your DBA can see how the hash is constructed and generate a hash for his changed data.
Letâ€™s update the row without updating the hash, and rerun the select query (click the image for a bigger version):
UPDATE Student SET FirstName = 'Bob' WHERE StudentID = 1
Oh dear, the hashes donâ€™t match.Â Sound an alarm!
Detecting changes to a table
Another use for hashing is to detect changes over a large number of rows.Â For example, if you have a Students table that also includes their year level, and you want to check monthly to see if new students have been added to that year, you can either have a Modified Date column to store when rows are modified, keep track of any changes via a trigger, or use a hash function.
Create a separate table to hold the hashed values:
CREATE TABLE MonthlyStudentHash ( SampleDate datetime , YearLevel int , GroupHash varchar(50) , PRIMARY KEY (SampleDate, YearLevel) )
Each month, we can run the following query to get a hashed version of the students:
INSERT INTO MonthlyStudentHash SELECT GETDATE(), YearLevel , sys.fn_varbintohexstr(HashBytes('SHA1' , CONVERT(varchar(20), NumStudents * SumDateOfBirth ))) FROM ( SELECT YearLevel , COUNT(*) AS NumStudents , SUM(CONVERT(int, DateOfBirth)) AS SumDateOfBirth FROM Student GROUP BY YearLevel ) years
In the inner query (years) we get all the students, and group them by year level, counting the number of students in the YearLevel, and adding together their dates of birth, converted to an integer.Â In the outer query, we then multiply the number of students by the SumDateOfBirth and hash this.Â No salting is necessary in this case.
The NumStudents * SumDateOfBirth doesnâ€™t make any sense, but it can be whatever you like â€“ the aim is to get a unique value that can be hashed.Â (Actually, NumStudents * SumDateOfBirth is likely to be small enough that hashing it will just take up more space, but if you want more accuracy (uniqueness), you can concatenate all the studentsâ€™ names together and hash that!)
Now, each month, run this query, and compare the current hash value to last monthâ€™s hash value.Â It will be easy to see if thereâ€™s been a change.Â This is a great solution if you have no power to modify the source table to add a ModifiedDate column.
There are plenty of other uses for hashed values â€“ this article has only mentioned a few uses.Â Iâ€™d love to hear any other ideas â€“ please leave a comment!