Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. string user = tbnome.Text;
  2.             string pass = tbpass.Text;
  3.  
  4.             System.Security.Cryptography.SHA256Managed crypt = new System.Security.Cryptography.SHA256Managed();
  5.             System.Text.StringBuilder hash = new System.Text.StringBuilder();
  6.          
  7.             byte[] password = crypt.ComputeHash(Encoding.UTF8.GetBytes(pass), 0, Encoding.UTF8.GetByteCount(pass));
  8.  
  9.             if (tbpass.Text.Count()>0 && tbnome.Text.Count()>0)
  10.             {
  11.                 MessageBox.Show("Well Done");
  12. /*
  13.  private const int SALTSIZE = 8;
  14.  
  15.         private bool VerifyLogin(string username, string password)
  16.         {
  17.             SqlConnection conn = null;
  18.             try
  19.             {
  20.                 // Configurar ligação à Base de Dados
  21.                 conn = new SqlConnection();
  22.                 conn.ConnectionString = String.Format(@"DataSource=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ASUS\Desktop\UNI\Segurança\FichaPratica8_Base\FichaPratica8_Base\Database1.mdf;IntegratedSecurity=True");
  23.  
  24.                 // Abrir ligação à Base de Dados
  25.                 conn.Open();
  26.  
  27.                 // Declaração do comando SQL
  28.                 String sql = "SELECT * FROM Users WHERE Username = @username";
  29.                 SqlCommand cmd = new SqlCommand();
  30.                 cmd.CommandText = sql;
  31.  
  32.                 // Declaração dos parâmetros do comando SQL
  33.                 SqlParameter param = new SqlParameter("@username", username);
  34.  
  35.                 // Introduzir valor ao parâmentro registado no comando SQL
  36.                 cmd.Parameters.Add(param);
  37.  
  38.                 // Associar ligação à Base de Dados ao comando a ser executado
  39.                 cmd.Connection = conn;
  40.  
  41.                 // Executar comando SQL
  42.                 SqlDataReader reader = cmd.ExecuteReader();
  43.  
  44.                 if (!reader.HasRows)
  45.                 {
  46.                     throw new Exception("Error while trying to access an user");
  47.                 }
  48.  
  49.                 // Ler resultado da pesquisa
  50.                 reader.Read();
  51.  
  52.                 // Obter Hash (password + salt)
  53.                 byte[] saltedPasswordHashStored = (byte[])reader["SaltedPasswordHash"];
  54.  
  55.                 // Obter salt
  56.                 byte[] saltStored = (byte[])reader["Salt"];
  57.  
  58.                 conn.Close();
  59.  
  60.                 byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  61.  
  62.                 byte[] hash = GenerateSaltedHash(passwordBytes, saltStored);
  63.  
  64.                 return saltedPasswordHashStored.SequenceEqual(hash);
  65.  
  66.             }
  67.             catch (Exception e)
  68.             {
  69.                 MessageBox.Show("An error occurred");
  70.                 return false;
  71.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement