Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Configuration;
- using MySql.Data.MySqlClient;
- using System.Data.SqlClient;
- namespace Parque_de_campismo
- {
- public partial class Login : MetroFramework.Forms.MetroForm
- {
- SqlConnection cm;
- string input, username, password, usersalt, userlogin;
- bool correctuser, passwordcerta;
- public Login()
- {
- InitializeComponent();
- }
- private void Login_Load(object sender, EventArgs e)
- {
- input = metroTextBox1.Text;
- try
- {
- cm = new SqlConnection();
- cm.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\neuro\Desktop\Parque de campismo\Parque de campismo\login.mdf;Integrated Security=True";
- cm.Open();
- MessageBox.Show("Connected to the DataBase");
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //LOGIN
- private void metroButton1_Click(object sender, EventArgs e)
- {
- bool reader;
- string salt1, hash;
- userlogin = metroTextBox1.Text;
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = "SELECT salt,hash FROM [Table] WHERE user ='" + userlogin + "';";
- cmd.CommandType = CommandType.Text;
- cmd.Connection = cm;
- SqlDataReader rdr = cmd.ExecuteReader();
- while (rdr.Read())
- {
- reader = true;
- salt1 = rdr[0].ToString();
- hash = rdr[1].ToString();
- string hashedpassword = GenerateSHA512Hash(input, salt1);
- MessageBox.Show("hello");
- if (hash == hashedpassword)
- {
- MessageBox.Show("Acertou Miseravel");
- MainForm childform = new MainForm();
- childform.Show();
- childform.WindowState = FormWindowState.Maximized;
- this.Hide();
- }
- }
- rdr.Close();
- }
- //string salt1, hash;
- //userlogin = metroTextBox1.Text;
- //SqlCommand cmd = new SqlCommand();
- //cmd.CommandText = "SELECT salt,hash FROM [Table] WHERE user = '" + userlogin + "';";
- //cmd.CommandType = CommandType.Text;
- //cmd.Connection = cm;
- //MessageBox.Show("Seleccionou");
- //SqlDataReader reader = cmd.ExecuteReader();
- //while (reader.Read())
- //{
- // MessageBox.Show("Começou a ler");
- // salt1 = reader[0].ToString();
- // hash = reader[1].ToString();
- // MessageBox.Show("Continua a ler");
- // string hashedpassword = GenerateSHA512Hash(input, salt1);
- // if (hash == hashedpassword)
- // {
- // MessageBox.Show("Adicionou");
- // }
- //}
- //reader.Close();
- //REGISTO
- private void metroButton2_Click(object sender, EventArgs e)
- {
- input = metroTextBox3.Text;
- String salt = CreateSalt(10);
- String hashedpassword = GenerateSHA512Hash(input, salt);
- usersalt = salt;
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = "INSERT INTO [Table](username,salt,hash,email) VALUES(@username,@salt,@hash,@email);";
- cmd.CommandType = CommandType.Text;
- cmd.Connection = cm;
- cmd.Parameters.AddWithValue("@username", metroTextBox4.Text);
- cmd.Parameters.AddWithValue("@salt", usersalt);
- cmd.Parameters.AddWithValue("@hash", hashedpassword);
- cmd.Parameters.AddWithValue("@email", metroTextBox6.Text);
- cmd.ExecuteNonQuery();
- }
- catch (SqlException ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public String GenerateSHA512Hash(String input, String salt)
- {
- byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
- System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
- byte[] hash = sha256hashstring.ComputeHash(bytes);
- return Convert.ToBase64String(hash);
- }
- public String CreateSalt(int size)
- {
- var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
- var buff = new byte[size];
- rng.GetBytes(buff);
- return Convert.ToBase64String(buff);
- }
- }
- }
Add Comment
Please, Sign In to add comment