Как получить MD5-хэш введенного пароля - C#
Формулировка задачи:
Всем доброго времени суток.
Сейчас пишу простенький лаунчер для своего сервера и он должен брать данные из базы данных.
В базе пароли хранятся в зашифрованном виде, авторизацию я написал, а вот как сделать так, чтобы программа проверяла именно хеш пароля - не знаю.
Это код авторизации. Что нужно дописать/изменить? Очень надеюсь на вашу помощь.
using System.Data;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
public bool tryLogin(string username, string password)
{
MySqlConnection con = new MySqlConnection("host=localhost;user=root;password=1234;database=test;");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM users WHERE users_login = '" + username + "' AND users_password = '" + GetHashString(GetHashString(password)) + "';");
cmd.Connection = con;
con.Open();
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read() != false)
{
if (reader.IsDBNull(0) == true)
{
cmd.Connection.Close();
reader.Dispose();
cmd.Dispose();
return false;
}
else
{
cmd.Connection.Close();
reader.Dispose();
cmd.Dispose();
return true;
}
}
else
{
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (tryLogin(textBox1.Text, textBox2.Text) == true)
{
this.Hide();
Form2 f1 = new Form2();
f1.Show();
}
else
{
MessageBox.Show("Неверный логин или пароль");
}
}Решение задачи: «Как получить MD5-хэш введенного пароля»
textual
Листинг программы
public static string getMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}