matching a text input with a hashed value in MySQL
I have a winform c# application. It connects to MySQL remotely and matches passwords stored in the MySQL database table . The passwords are stored using SHA1. I have stored a default value : "mypassword" and hashed it using SHA1.
When th开发者_开发百科e application runs, first the user is asked to enter the password. For this if i use the below mentioned function then it generates a different hash string when compared to the one generated by MySQL SHA1 function. How to match the values? Please help.
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;
}
}
Am calling it like this :
string hashedvalue = HashCode("mypassword");
MessageBox.Show("The hashed value is : " + hashedvalue);
Is hashing of both are different?
SHA-1 hash from MySQL is a hex-encoded string, while C# gives Base64-encoded string, so try this
rethash = BitConverter.ToString(hash.Hash).Replace("-", "");
精彩评论