SQL在char列中存储MD5
我有一个 char(32) 类型的列,我想在其中存储 MD5 哈希密钥。问题是我已经使用 SQL 使用 HashBytes() 函数更新现有记录,该函数创建类似的值
:›=k! ©úw"5Ýâ‘<\
,但是当我通过 .NET 进行插入时,它会出现为“
3A9B3D6B2120A9FA772235DDE2913C5C
我需要做什么才能使这些匹配?”是编码吗?
I have a column of type char(32) where I want to store an MD5 hash key. The problem is i've used SQL to update the existing records using HashBytes() function which creates values like
:›=k! ©úw"5Ýâ‘<\
but when I do the insert via .NET it comes through as
3A9B3D6B2120A9FA772235DDE2913C5C
What do I need to do to get these to match up? Is it the encoding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HashKey 不是 SQL 函数,您是指
HASHBYTES
吗?一些实际的代码会有所帮助。 SQL 似乎正在计算原始二进制哈希并将其显示为 ASCII 字符。.NET 正在计算哈希值,然后将其转换为十六进制(或者看起来如此)。 CHAR(32) 不是存储原始二进制数据的好方法,您可能需要使用 BINARY 类型。
SQL 中的示例:
.NET 中的示例:
它们都将产生相同的值。 (其中 0x2040 是示例数据)。
您可以将十六进制数据存储为 CHAR(32) 或 BINARY(16)。存储二进制数据的空间效率是存储为十六进制数据的两倍。您不应该做的是将二进制数据存储为 CHAR(16)。
HashKey isn't a SQL function, did you mean
HASHBYTES
? Some actual code would help. SQL appears to be computing the raw binary hash and displaying it as ASCII characters..NET is computing the hash, then converting it to hexadecimal (or so it appears). CHAR(32) isn't a good way to store raw binary data, you would want to use the BINARY type.
An Example in SQL:
And an Example in .NET:
These will both produce the same value. (Where 0x2040 is sample data).
You can either store the hexadecimal data as CHAR(32), or as BINARY(16). Storing the Binary data is twice as space efficient than storing it as hex. What you should not be doing is storing the binary data as CHAR(16).
目前尚不清楚“当我通过 .NET 进行插入时”是什么意思 - 但您不应该仅以原始形式存储二进制数据,因为它看起来像您正在使用 <代码>HashKey()。 (顺便说一句,您肯定是指
HashKey
吗?我找不到它的参考,但有HashBytes
...)两个常见选项是对原始二进制文件进行编码数据为十六进制 - 看起来你在第二种情况下正在做 - 或者使用 base64。无论哪种方式,在 .NET 中都应该很容易(Base64 稍微容易一些,使用
Convert.ToBase64String
),并且您可能只需要找到等效的 SQL Server 函数。It's not clear what you mean by "when I do the insert via .NET" - but you shouldn't be storing binary data just in a raw form, as it looks like your'e doing using
HashKey()
. (Do you definitely meanHashKey
by the way? I can't find a reference for it, but there'sHashBytes
...)Two common options are to encode the raw binary data as hex - which it looks like you're doing in the second case - or to use base64. Either way should be easy from .NET (Base64 marginally easier, using
Convert.ToBase64String
) and you probably just need to find the equivalent SQL Server function.MD5 通常以十六进制编码形式存储。我猜测您的 hashkey() SQL 函数不是对 MD5 哈希值进行十六进制编码,而是只是返回表示哈希值的 ASCII 字符。但你的.NET方法是十六进制编码。如果您将 MD5 散列一致地存储为十六进制(或不存储 - 由您决定,但通常存储为十六进制),那么两者之间的结果应该始终一致。
例如,SQL 哈希中的 : 符号是 HashKey() 返回的第一个字符。在.NET方法中,前2个字符是3A。 31 的十进制是 51。 ASCII 代码 51 是冒号 (:) 字符。同样,您可以按自己的方式处理每个其他字符,并进行十六进制转换。
请参阅任何 ASCII 代码表以供参考,即 http://www.asciitable.com/
MD5 is typically stored as in hex encoding. I'd guess that your hashkey() SQL function is not hex encoding the MD5 hash, rather it's just returning the ASCII characters representing the hash. But your .NET method is HEX encoding. If you store your MD5 hashing consistently as HEX (or not - up to you but usually stored as HEX), then the results between the two should always be consistent.
For example, the : symbol from your SQL hash is the first character returned from HashKey(). In the .NET method, the first 2 characters are 3A. 31 is 51 in decimal. ASCII code 51 is the colon (:) character. Similarly, you can work your way through each other character, and do the HEX conversion.
See any ASCII codes table for reference, i.e. http://www.asciitable.com/