Advertisement
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.Data.SqlClient;
- namespace DBTestes
- {
- public partial class Form1 : Form
- {
- SqlConnection conn;
- string input, username, password, usersalt, userlogin;
- bool correctuser, passwordcerta;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- conn = new System.Data.SqlClient.SqlConnection();
- conn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DemoDB.mdf;Integrated Security=True;Connect Timeout=30";
- conn.Open();
- }
- public String CreateSalt(int size)
- {
- var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
- var buff = new byte[size];
- rng.GetBytes(buff);
- return Convert.ToBase64String(buff);
- }
- 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);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- username = textBox2.Text;
- password = textBox1.Text;
- String salt = CreateSalt(10);
- String hashedpassword = GenerateSHA512Hash(textBox1.Text, salt);
- usersalt = salt;
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.CommandText = "INSERT INTO password(user,salt,hash) VALUES(@user,@salt,@hash)";
- cmd.CommandType = CommandType.Text;
- cmd.Connection = conn;
- cmd.Parameters.AddWithValue("@user", username);
- cmd.Parameters.AddWithValue("@salt", usersalt);
- cmd.Parameters.AddWithValue("@hash", hashedpassword);
- cmd.ExecuteNonQuery();
- }
- catch (SqlException sqlEx)
- {
- MessageBox.Show(sqlEx.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement