0

I have C# application which makes connection SQL Server 2014 database. When I create Guid on C#, it writes on SQL via lower case. But when I make query on SQL, I get this value upper case. This Guid value needs to be hashed by SHA1, and as SQL returns different hashed value.

Is it because of collation?

I use Turkish_CI_AI.

Praveen
  • 8,945
  • 4
  • 31
  • 49
Sertan Pekel
  • 593
  • 2
  • 5
  • 23

1 Answers1

3

GUIDs are 16-byte binary values, so they have no case. The multi-part hexadecimal string often seen is just one of the textual representations of GUIDs. This means that whether it's upper or lower case depends on the formatting code and has nothing to do with the actual value. It also means that case shouldn't be taken into consideration when comparing any textual representation of a GUID.

If you want to hash a Uniqueidentifier value, you should retrieve it as a GUID and hash the actual bytes by calling Guid.ToByteArray, not its textual representation.

Finally, you should consider whether you actually need to hash a GUID at all. A GUID is 128-bit long while a SHA1 has is least 160-bit long. If you want to calculate a hash value for quick comparisons, it would be better to simply use the raw GUID.

If you want to hash a multi-field message, it would be better to get the bytes of all fields and hash the resulting byte array

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236