将文本输入与MySQL中的哈希值进行匹配
本文关键字:哈希值 文本 输入 MySQL | 更新日期: 2023-09-27 17:59:39
我有一个winform c#应用程序。它远程连接到MySQL,并匹配存储在MySQL数据库表中的密码。密码使用SHA1进行存储。我存储了一个默认值:"mypassword",并使用SHA1对其进行了哈希处理。
当应用程序运行时,首先要求用户输入密码。为此,如果我使用下面提到的函数,那么与MySQL SHA1函数生成的哈希字符串相比,它会生成不同的哈希字符串。如何匹配这些值?请帮忙。
public static string HashCode(string str)
{
string rethash = "";
try
{
System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] combined = encoder.GetBytes(str);
hash.ComputeHash(combined);
rethash = Convert.ToBase64String(hash.Hash);
}
catch (Exception ex)
{
string strerr = "Error in HashCode : " + ex.Message;
}
return rethash;
}
}
我这样称呼它:
string hashedvalue = HashCode("mypassword");
MessageBox.Show("The hashed value is : " + hashedvalue);
两者的散列不同吗?
MySQL中的SHA-1哈希是一个十六进制编码的字符串,而C#给出的是Base64编码的字符串
rethash = BitConverter.ToString(hash.Hash).Replace("-", "");