Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 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.Data.SqlClient;
  11. namespace DBTestes
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.        
  16.         SqlConnection conn;
  17.         string input, username, password, usersalt, userlogin;
  18.         bool correctuser, passwordcerta;
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.        
  24.         private void Form1_Load(object sender, EventArgs e)
  25.         {
  26.      conn = new System.Data.SqlClient.SqlConnection();
  27.                 conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DemoDB.mdf;Integrated Security=True;Connect Timeout=30";
  28.                 conn.Open();
  29.     }
  30.  
  31.         public String CreateSalt(int size)
  32.         {
  33.             var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
  34.             var buff = new byte[size];
  35.             rng.GetBytes(buff);
  36.             return Convert.ToBase64String(buff);
  37.         }
  38.         public String GenerateSHA512Hash(String input, String salt)
  39.         {
  40.             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
  41.             System.Security.Cryptography.SHA256Managed sha256hashstring = new System.Security.Cryptography.SHA256Managed();
  42.             byte[] hash = sha256hashstring.ComputeHash(bytes);
  43.  
  44.             return Convert.ToBase64String(hash);
  45.         }
  46.  
  47.         private void button1_Click(object sender, EventArgs e)
  48.         {
  49.  
  50.                 username = textBox2.Text;
  51.                 password = textBox1.Text;
  52.                 String salt = CreateSalt(10);
  53.                 String hashedpassword = GenerateSHA512Hash(textBox1.Text, salt);
  54.                 usersalt = salt;
  55.             try
  56.             {
  57.                 SqlCommand cmd = new SqlCommand();
  58.                 cmd.CommandText = "INSERT INTO password(user,salt,hash) VALUES(@user,@salt,@hash)";
  59.                 cmd.CommandType = CommandType.Text;
  60.                 cmd.Connection = conn;
  61.                 cmd.Parameters.AddWithValue("@user", username);
  62.                 cmd.Parameters.AddWithValue("@salt", usersalt);
  63.                 cmd.Parameters.AddWithValue("@hash", hashedpassword);
  64.                 cmd.ExecuteNonQuery();
  65.             }
  66.             catch (SqlException sqlEx)
  67.             {
  68.  
  69.                 MessageBox.Show(sqlEx.Message);
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement