title: How to validate Password from database when password is hashed
body: <p>Hi I have a program where I request a username and password from a user through WPF, I then send the data to a database where it is stored.</p>
  <pre><code> private void btnRegister_Click(object sender, RoutedEventArgs e)
          {
              try
              {
                  using (SqlConnection connection = new SqlConnection(ConnectionSQL.conn))
                  {
                      SqlCommand command = new SqlCommand(&quot;INSERT INTO USERTABLE &quot; +
                                                          &quot;VALUES(@Username, @Password);&quot; +
                                                          &quot;Select SCOPE_IDENTITY();&quot;, connection);
                      command.Parameters.AddWithValue(&quot;@Username&quot;, txtbUsername.Text);
                      command.Parameters.AddWithValue(&quot;@Password&quot;, Utils.hashPassword(txtbPassword.Text));
                      connection.Open();
  
                      SqlDataAdapter adapter = new SqlDataAdapter();
                      adapter.InsertCommand = command;
  
                      int id = Convert.ToInt32(adapter.InsertCommand.ExecuteScalar());
                      MessageBox.Show(&quot;User Registered! User has been added to the database: &quot; + id);
                      adapter.Dispose();
  
                      String Username = txtbUsername.Text;
                      String Password = txtbPassword.Text;
                      Users temp = new Users(id, Username, Password);
  
                      Login L = new Login();
                      arrUsers.Add(temp);
                      Hide();
                      L.ShowDialog();
                      
                  }
              }
              catch (SqlException ex)
              {
                  MessageBox.Show(&quot;Error Connecting to the Database&quot;, &quot;Connection Error&quot; + ex.ToString());
              }
  
          }
  </code></pre>
  <p>I created a function to store the password as a hash in the database.</p>
  <pre><code> public class Utils
      {
          public static string hashPassword(String password)
          {
              SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  
              byte[] password_bytes = Encoding.ASCII.GetBytes(password);
              byte[] encrypted_bytes = sha1.ComputeHash(password_bytes);
              return Convert.ToBase64String(encrypted_bytes);
  
  
          }
      }
  </code></pre>
  <p>When I try log in and validate password when its stored as a hash it doesnt work.</p>
  <pre><code> private void btnLogin_Click(object sender, RoutedEventArgs e)
         {
             try
             { 
                 using (SqlConnection connection = new SqlConnection(ConnectionSQL.conn))
                 {
                     connection.Open();
                     String Username = Convert.ToString(txtbUsername.Text);
                     String Password = Convert.ToString(txtbPassword.Text);
  
                     String sql = &quot;SELECT * FROM USERTABLE where Username = '&quot; + Username + &quot;' &quot; +
                         &quot;AND Password = '&quot; + Password + &quot;' ;&quot;;
                     SqlCommand command = new SqlCommand(sql, connection);
                     SqlDataReader reader = command.ExecuteReader();
                     if (reader.HasRows)
                     {
                         MessageBox.Show(&quot;You Have Successfully Logged In&quot;);
                         MainWindow Main = new MainWindow();
                         txtbUsername.Text = &quot;&quot;;
                         txtbPassword.Text = &quot;&quot;;
                         this.Hide();
                         Main.ShowDialog();
                         this.Show();
                     }
                     else
                     {
                         MessageBox.Show(&quot;Invalid Credentials&quot;);
                     }
                     reader.Close();
                     command.Dispose();
                 }
             }
             catch (SqlException ex)
             {
                 Console.WriteLine(ex.ToString());
             }
             Console.ReadLine();
         }
  </code></pre>
  <p>Although when i dont hash the password in the database it works. Any tips on solving this?</p>