Answer the question
In order to leave comments, you need to log in
How to store password hash in MySQL DB?
Hello, I need to save account passwords in the database, I read about it and realized that it is necessary to store in the database not the passwords themselves, but the keys. Tried to implement something, here is the code:
public static void SetPassword(string userName, string userPassword)
{
string sql = "";
byte[] salt = new byte[20], key = new byte[20];
using (var deriveBytes = new Rfc2898DeriveBytes(userPassword, 20))
{
salt = deriveBytes.Salt;
key = deriveBytes.GetBytes(20);
sql = $"UPDATE `accounts` SET `password_key`= '?key', `password_salt`= '?salt' WHERE `username` = '{userName}'";
}
using (var cmd = new MySqlCommand(sql, conn))
{
cmd.Parameters.Add(new MySqlParameter("?key", key));
cmd.Parameters.Add(new MySqlParameter("?salt", salt));
cmd.ExecuteNonQuery();
}
}]
Answer the question
In order to leave comments, you need to log in
In general, everything is clear, you write there the value ?key equal to 4 bytes (one byte per character) and ?salt equal to 5 bytes. Change your SQL to
sql = @"UPDATE `accounts` SET `password_key`= ?key, `password_salt`= ?salt WHERE `username` = '{userName}'";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question