SQL 2005 MD5哈希和C#MD5哈希 [英] SQL 2005 MD5 Hash and C# MD5 Hash

查看:116
本文介绍了SQL 2005 MD5哈希和C#MD5哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前拥有一个遗留数据库(SQL 2005),用于为令牌生成哈希字符串。它是这样的...

  DECLARE @RowID INT 
DECLARE @hashString VARCHAR(128)

SET @RowID = 12345
SET @ salt = 0xD2779428A5328AF9

SET @hashBinary = HASHBYTES(('MD5',@salt + CAST(@RowID AS VARBINARY(30) ))
SET @hashString = sys.fn_varbintohexstr(@hashBinary)

如果我执行此,我的哈希字符串看起来像这样:0x69a947fd71b853485007f0d0be0763a5



现在,我需要在C#中复制相同的逻辑,所以我可以删除生成这些散列的数据库依赖关系,它必须向后兼容。



我已经在C#中实现了这样:

 code> byte [] saltBytes = BitConverter.GetBytes(0xD2779428A5328AF9); 
byte [] pidBytes = BitConverter.GetBytes(12345);

byte [] bytesToHash = new byte [saltBytes (int i = 0; i< saltBytes.Length; i ++)
{
bytesToHash [i] = saltBytes [i] ;
} (int i = 0;

i< pidBytes.Length; i ++)
{
bytesToHash [saltBytes.Length + 1] = pidBytes [i];
}

MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();
byte [] hashedBytes = hasher.ComputeHash(bytesToHash);

string hashString = BitConverter.ToString(hashedBytes).ToLower()。Replace( - ,);

问题是,我的C#实现生成这个哈希:715f5d6341722115a1bfb2c82e4421bf



他们显然是不同的。



那么,是否可以一致地进行比赛?

解决方案

我已经解决了这个问题:



在SQL中:

  DECLARE @Value INT 
SET @Value = 12345

SELECT sys.fn_varbintohexstr(CAST(@Value AS VARBINARY(30)))

我得到这个结果:0x00003039



现在,如果我在C#中执行此操作:

  int value = 12345; 
byte [] bytes = BitConverter.GetBytes(value);
Console.Write(BitConverter.ToString(bytes))

我得到这个结果:39 -30-00-00



字节似乎是相反的顺序。因此,一旦将这些字节数组应用于MD5哈希值,我得到明显不同的散列值。



如果在将C#字节数组放入MD5 Hasher之前,我得到SQL生成的相同哈希。


I currently have a legacy database (SQL 2005) that generates hash strings for tokens. It does it like this...

DECLARE @RowID INT
DECLARE @hashString VARCHAR(128)

SET @RowID = 12345
SET @salt= 0xD2779428A5328AF9

SET @hashBinary = HASHBYTES(('MD5', @salt + CAST(@RowID AS VARBINARY(30)))
SET @hashString = sys.fn_varbintohexstr(@hashBinary)

If I execute this, I my hash string looks like this: "0x69a947fd71b853485007f0d0be0763a5"

Now, I need to replicate the same logic in C# so I can remove the database dependency for generating these hashes, and it has to be backward compatible.

I have implemented in C# like this:

byte[] saltBytes = BitConverter.GetBytes(0xD2779428A5328AF9);
byte[] pidBytes = BitConverter.GetBytes(12345);

byte[] bytesToHash = new byte[saltBytes.Length + pidBytes.Length];

for (int i = 0; i < saltBytes.Length; i++)
{
    bytesToHash[i] = saltBytes[i];
}

for (int i = 0; i < pidBytes.Length; i++)
{
    bytesToHash[saltBytes.Length + 1] = pidBytes[i];
}

MD5CryptoServiceProvider hasher = new MD5CryptoServiceProvider();
byte[] hashedBytes = hasher.ComputeHash(bytesToHash);

string hashString = BitConverter.ToString(hashedBytes).ToLower().Replace("-", "");

The problem is, my C# implementation generates this hash: "715f5d6341722115a1bfb2c82e4421bf"

They are obviously different.

So, is it possible to make the match consistently?

解决方案

I've solved the problem:

If I do this in SQL:

DECLARE @Value INT
SET @Value = 12345

SELECT sys.fn_varbintohexstr(CAST(@Value AS VARBINARY(30)))

I get this result: 0x00003039

Now, if I do this in C#:

int value = 12345;
byte[] bytes = BitConverter.GetBytes(value);
Console.Write(BitConverter.ToString(bytes))

I get this result: 39-30-00-00

The bytes appear to be in reverse order. Hence, once I apply these byte arrays to the MD5 hasher, I get distinctly different hash values.

If I reverse the C# byte array before putting it through the MD5 Hasher, I get the same hash generated by SQL.

这篇关于SQL 2005 MD5哈希和C#MD5哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆