Note that there are some explanatory texts on larger screens.

plurals
  1. POHashing password to SqlServer
    primarykey
    data
    text
    <p>I've been reading over and over the code again to see where the error is being made but I'm unable to find it. I've copied this code from stackoverflow an never really checked it or understood it perfectly as to fix it. I'm receiving passwords from a webservice, hashing, salting and saving it to a SqlServer 2008. The variables on the SqlServer are declared as mail as nvarchar(64), hash as varbinary(128) and salt as varbinary(128). The passwords are being saved but when I try to check if the password are correct the method always returns false. This are my methods.</p> <pre><code>public int InsertData(string mail,string Password) { int lineas; UserData usuario = HashPassword(Password); using (SqlConnection connection = new SqlConnection(Connection)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "INSERT INTO Usuarios (Mail,Hash,Salt) VALUES (@mail,@hash,@salt)"; command.Parameters.AddWithValue("@mail", mail); command.Parameters.AddWithValue("@hash", usuario.Password); command.Parameters.AddWithValue("@salt", usuario.salt); connection.Open(); lineas=command.ExecuteNonQuery(); } usuario = null; return lineas; } private UserData HashPassword(string Password) { //This method hashes the user password and saves it into the object UserData using (var deriveBytes = new Rfc2898DeriveBytes(Password, 20)) { byte[] salt = deriveBytes.Salt; byte[] key = deriveBytes.GetBytes(20); // derive a 20-byte key UserData usuario = new UserData(); usuario.Password = key; usuario.salt = salt; return usuario; } } </code></pre> <p>And the next method is the one I use to validate de password, it always returns false</p> <pre><code>private bool CheckPassword(string Password, byte[] hash, byte[] salt) { // load salt and key from database using (var deriveBytes = new Rfc2898DeriveBytes(Password, salt)) { byte[] newKey = deriveBytes.GetBytes(20); // derive a 20-byte key if (!newKey.SequenceEqual(hash)) return false; else return true; } } </code></pre> <p>This method receives the login info</p> <pre><code> public bool ValidateLogIn(string mail, string Password) { using (SqlConnection connection = new SqlConnection(Connection)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = "Select * from Usuarios where Mail=@mail"; command.Parameters.AddWithValue("@mail",mail); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { reader.Read(); byte[] hash = (byte[])reader["Hash"]; byte[] salt = (byte[])reader["Salt"]; if(CheckPassword(Password,hash,salt)) { / UpdateData(mail, Password); return true; } else { return false; } } } } </code></pre> <p>Any ideas what could be wrong?</p> <p>EDIT: I've found the link where I got the hashing code <a href="https://stackoverflow.com/a/4330586/1861617">https://stackoverflow.com/a/4330586/1861617</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload