SQL在char列中存储MD5

发布于 2024-11-09 08:20:17 字数 273 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷弦 2024-11-16 08:20:17

HashKey 不是 SQL 函数,您是指 HASHBYTES 吗?一些实际的代码会有所帮助。 SQL 似乎正在计算原始二进制哈希并将其显示为 ASCII 字符。

.NET 正在计算哈希值,然后将其转换为十六进制(或者看起来如此)。 CHAR(32) 不是存储原始二进制数据的好方法,您可能需要使用 BINARY 类型。

SQL 中的示例:

SELECT SUBSTRING(sys.fn_varbintohexstr(HASHBYTES('MD5',0x2040)),3, 32)

.NET 中的示例:

using (MD5 md5 = MD5.Create())
{
    var data = new byte[] { 0x20, 0x40 };
    var hashed = md5.ComputeHash(data);
    var hexHash = BitConverter.ToString(hashed).Replace("-", "");
    Console.Out.WriteLine("hexHash = {0}", hexHash);
}

它们都将产生相同的值。 (其中 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:

SELECT SUBSTRING(sys.fn_varbintohexstr(HASHBYTES('MD5',0x2040)),3, 32)

And an Example in .NET:

using (MD5 md5 = MD5.Create())
{
    var data = new byte[] { 0x20, 0x40 };
    var hashed = md5.ComputeHash(data);
    var hexHash = BitConverter.ToString(hashed).Replace("-", "");
    Console.Out.WriteLine("hexHash = {0}", hexHash);
}

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).

九歌凝 2024-11-16 08:20:17

目前尚不清楚“当我通过 .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 mean HashKey by the way? I can't find a reference for it, but there's HashBytes...)

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.

怕倦 2024-11-16 08:20:17

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文