|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 9:52 AM
Points: 1,277,
Visits: 1,608
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 9:46 AM
Points: 2,
Visits: 53
|
|
About HASHBYTES (from http://msdn.microsoft.com/en-us/library/ms174415.aspx):
1. input size is limited to 8000 2. output size depends on used algorithm, for MD5 you can use CAST(HASHBYTES('MD5', (EmployeeName + CityName)) AS BINARY(16)) 3. beware addition NULL values
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 18, 2013 3:35 AM
Points: 2,
Visits: 41
|
|
While the return type of hashbytes might be 8000 bytes, that's more for future proofing, the actual value length will be:
Allowed input values are limited to 8000 bytes. The output conforms to the algorithm standard: 128 bits (16 bytes) for MD2, MD4, and MD5; 160 bits (20 bytes) for SHA and SHA1; 256 bits (32 bytes) for SHA2_256, and 512 bits (64 bytes) for SHA2_512.
from http://msdn.microsoft.com/en-us/library/ms174415.aspx
so your absolute longest using one of the currently supported hashes, is going to be 64 bytes, so your actual storage should be 66 bytes. And you could save those two bytes by doing a cast to a fixed length binary since you will know what algorithm you will be using.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:28 AM
Points: 9,
Visits: 21
|
|
Hi
It is dangerous to use hashbytes this way:
DataHashBytes AS HASHBYTES('MD5', (EmployeeName + CityName))
When EmployeeName = '' and CityName = 'Paris' and someone changes it to EmployeeName = 'Paris' and CityName = '' the hash stays the same. The solution (not very elegant but still a solution) is to use a delimiter, e.g.:
DataHashBytes AS HASHBYTES('MD5', (EmployeeName + char(0) + CityName))
The article is very useful, thank you.
|
|
|
|