简体   繁体   English

SQL Uniqueidentifier哈希字节不匹配

[英]SQL Uniqueidentifier hashbytes does not match

I have C# application which makes connection SQL Server 2014 database. 我有建立连接SQL Server 2014数据库的C#应用程序。 When I create Guid on C# , it writes on SQL via lower case . 当我在C#上创建Guid时,它通过lower case在SQL上编写。 But when I make query on SQL , I get this value upper case . 但是,当我在SQL上进行查询时,此值将upper case This Guid value needs to be hashed by SHA1 , and as SQL returns different hashed value . Guid值需要由SHA1进行哈希处理,并且随着SQL返回不同的hashed value

Is it because of collation ? 是因为collation

I use Turkish_CI_AI . 我使用Turkish_CI_AI

GUIDs are 16-byte binary values , so they have no case. GUID是16字节的二进制值 ,因此没有大小写。 The multi-part hexadecimal string often seen is just one of the textual representations of GUIDs. 经常看到的多部分十六进制字符串只是GUID的文本表示形式之一。 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. 这也意味着在比较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. 如果要散列Uniqueidentifier值,则应将其作为GUID检索,并通过调用Guid.ToByteArray而不是其文本表示形式对实际字节进行散列。

Finally, you should consider whether you actually need to hash a GUID at all. 最后,您应该考虑是否真的需要对GUID进行哈希处理。 A GUID is 128-bit long while a SHA1 has is least 160-bit long. GUID的长度为128位,而SHA1的长度至少为160位。 If you want to calculate a hash value for quick comparisons, it would be better to simply use the raw GUID. 如果要计算散列值以进行快速比较,则最好只使用原始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 如果要散列多字段消息,最好获取所有字段的字节并散列结果字节数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM