Guest User

Untitled

a guest
Nov 3rd, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Configuration;
  11. using MySql.Data.MySqlClient;
  12. using System.Data.SqlClient;
  13. namespace Parque_de_campismo
  14. {
  15.     public partial class Login : MetroFramework.Forms.MetroForm
  16.     {
  17.         SqlConnection cm;
  18.  
  19.         string input, username, password, usersalt, userlogin;
  20.         bool correctuser, passwordcerta;
  21.         public Login()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         private void Login_Load(object sender, EventArgs e)
  26.         {
  27.             input = metroTextBox1.Text;
  28.             try
  29.             {
  30.                 cm = new SqlConnection();
  31.                 cm.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\neuro\Desktop\Parque de campismo\Parque de campismo\login.mdf;Integrated Security=True";
  32.                 cm.Open();
  33.                 MessageBox.Show("Connected to the DataBase");
  34.             }
  35.             catch (SqlException ex)
  36.             {
  37.                 MessageBox.Show(ex.Message);
  38.             }
  39.         }
  40.         //LOGIN
  41.         private void metroButton1_Click(object sender, EventArgs e)
  42.         {
  43.             bool reader;
  44.             string salt1, hash;
  45.             userlogin = metroTextBox1.Text;
  46.             SqlCommand cmd = new SqlCommand();
  47.             cmd.CommandText = "SELECT salt,hash FROM [Table] WHERE user ='" + userlogin + "';";
  48.             cmd.CommandType = CommandType.Text;
  49.             cmd.Connection = cm;
  50.             SqlDataReader rdr = cmd.ExecuteReader();
  51.             while (rdr.Read())
  52.             {
  53.                 reader = true;
  54.                 salt1 = rdr[0].ToString();
  55.                 hash = rdr[1].ToString();
  56.                 string hashedpassword = GenerateSHA512Hash(input, salt1);
  57.                 MessageBox.Show("hello");
  58.                 if (hash == hashedpassword)
  59.                 {
  60.                     MessageBox.Show("Acertou Miseravel");
  61.                     MainForm childform = new MainForm();
  62.                     childform.Show();
  63.                     childform.WindowState = FormWindowState.Maximized;
  64.                     this.Hide();
  65.                 }
  66.             }
  67.             rdr.Close();
  68.         }
  69.             //string salt1, hash;
  70.             //userlogin = metroTextBox1.Text;
  71.             //SqlCommand cmd = new SqlCommand();
  72.             //cmd.CommandText = "SELECT salt,hash FROM [Table] WHERE user = '" + userlogin + "';";
  73.             //cmd.CommandType = CommandType.Text;
  74.             //cmd.Connection = cm;
  75.             //MessageBox.Show("Seleccionou");
  76.             //SqlDataReader reader = cmd.ExecuteReader();
  77.             //while (reader.Read())
  78.             //{
  79.             //    MessageBox.Show("Começou a ler");
  80.             //    salt1 = reader[0].ToString();
  81.             //    hash = reader[1].ToString();
  82.             //    MessageBox.Show("Continua a ler");
  83.             //    string hashedpassword = GenerateSHA512Hash(input, salt1);
  84.             //    if (hash == hashedpassword)
  85.             //    {
  86.             //        MessageBox.Show("Adicionou");
  87.             //    }
  88.             //}
  89.             //reader.Close();
  90.  
  91.  
  92.  
  93.  
  94.             //REGISTO
  95.         private void metroButton2_Click(object sender, EventArgs e)
  96.         {
  97.             input = metroTextBox3.Text;
  98.             String salt = CreateSalt(10);
  99.             String hashedpassword = GenerateSHA512Hash(input, salt);
  100.             usersalt = salt;
  101.             try
  102.             {
  103.                 SqlCommand cmd = new SqlCommand();
  104.                 cmd.CommandText = "INSERT INTO [Table](username,salt,hash,email) VALUES(@username,@salt,@hash,@email);";
  105.                 cmd.CommandType = CommandType.Text;
  106.                 cmd.Connection = cm;
  107.                 cmd.Parameters.AddWithValue("@username", metroTextBox4.Text);
  108.                 cmd.Parameters.AddWithValue("@salt", usersalt);
  109.                 cmd.Parameters.AddWithValue("@hash", hashedpassword);
  110.                 cmd.Parameters.AddWithValue("@email", metroTextBox6.Text);
  111.                 cmd.ExecuteNonQuery();
  112.             }
  113.             catch (SqlException ex)
  114.             {
  115.                 MessageBox.Show(ex.Message);
  116.             }
  117.         }
  118.  
  119.         public String GenerateSHA512Hash(String input, String salt)
  120.         {
  121.             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
  122.             System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
  123.             byte[] hash = sha256hashstring.ComputeHash(bytes);
  124.             return Convert.ToBase64String(hash);
  125.         }
  126.         public String CreateSalt(int size)
  127.         {
  128.             var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
  129.             var buff = new byte[size];
  130.             rng.GetBytes(buff);
  131.             return Convert.ToBase64String(buff);
  132.         }
  133.        
  134.     }
  135. }
Add Comment
Please, Sign In to add comment