WPF hash haseł do bazy

0

Mama programik do hash-owania w różnych odmianach SHA1/256/384/512, MD5, SHA256+sól. Mianowicie jak zrobić funkcję porównującą hash-e zapisywane np w bazie danych MSSql czy nawet w zmiennych i dawać dostęp użytkownikowi po sprawdzeniu istnienia hash-a , hash-a z solą. A i czy programik który skonstruowałem do hash-owania jest w porządku. Uczę się więc nie miejcie pretensji o ten Post nie mogę trafić w Google na to co mnie interesuje więc pytam. Dziękuję wszystkim za pomoc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using System.Diagnostics;


namespace szyfrowanie
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private string MD5(string Value)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);
            data = x.ComputeHash(data);
            string ret = "";
            for (int i = 0; i < data.Length; i++)
                ret += data[i].ToString("x2").ToLower();
            return ret;
        }

        public static string sha1encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }
/*?
 /
 /
 */        public static string sha256encrypt(string phrase)
        {
           UTF8Encoding encoder = new UTF8Encoding();
            SHA256Managed sha256hasher = new SHA256Managed();
            byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        public string GenSHA256hash(string input, string salt)// metoda taka jak wyrzej tylko z solą/salt
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input + salt);
            SHA256Managed sha256hasher = new SHA256Managed();
            byte[] hashedDataBytes = sha256hasher.ComputeHash(bytes);
            return byteArrayToString(hashedDataBytes);
        }
        
        public string Createsalt(int size)
        {
            var rng = new RNGCryptoServiceProvider();
            var buff = new byte[size];
            rng.GetBytes(buff);
            return Convert.ToBase64String(buff);
        }
  /*
   f
   f
   f
   */
        public static string sha384encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA384Managed sha384hasher = new SHA384Managed();
            byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }
        public static string sha512encrypt(string phrase)
        {
            UTF8Encoding encoder = new UTF8Encoding();
            SHA512Managed sha512hasher = new SHA512Managed();
            byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }
        public static string byteArrayToString(byte[] inputArray)
        {
            StringBuilder output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string salt = Createsalt(10);
            string hashedpass = GenSHA256hash(txtBox.Text, salt);

            txtBlock.Text = salt;
            txtBlock_Copy.Text = hashedpass;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            txtBlock_sha256.Text = sha256encrypt(txtBox.Text);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            txtBlock_Copy2.Text = MD5(txtBox.Text);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            txtBlock_Copy1.Text = sha1encrypt(txtBox.Text);
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            txtBlock_Copy3.Text = sha512encrypt(txtBox.Text);
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            txtBlock_Copy4.Text = sha384encrypt(txtBox.Text);
        }

       /* private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            Window1 w1 = new Window1();
            Close();
            w1.Show();
        }*/
    }
}
1

W bazie danych trzymasz 2 rzeczy: SALT oraz hash hasła. w momencie logowania user podaj hasło i login. z bazy danych pobierasz SALT dołączasz do niego hasło, generujesz hash i sprawdzasz czy ten nowy hash jest równy temu w bazie. A skąd brać właściwy rekord? no choćby po mailu po prostu który musi być unikatowy.

0

Dzięki bardzo naprawdę mi pomogłeś wielki plus dla takich ludzi. :) wieczorem sprawdzę :)

0

Od siebie dodam, ze metody generowania wartosci funkcji skrotu powinny byc w osobnych klasach, a nie byc metodami klasy dziedziczacej po Window, bo lamiesz zasade SRP.

1 użytkowników online, w tym zalogowanych: 0, gości: 1