From 7bf89dfeb8018dfd652e3ccfde56f91e622d4e20 Mon Sep 17 00:00:00 2001 From: Taarly Date: Mon, 28 Apr 2025 15:36:11 +0200 Subject: [PATCH] added hashing and implemented it for the masterpassword on login --- Project.Persistence/ShaHash.cs | 23 +++++++++++++++++++++++ Project.View/NewLogin/NewLogin.axaml.cs | 6 +++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 Project.Persistence/ShaHash.cs diff --git a/Project.Persistence/ShaHash.cs b/Project.Persistence/ShaHash.cs new file mode 100644 index 0000000..6ee8185 --- /dev/null +++ b/Project.Persistence/ShaHash.cs @@ -0,0 +1,23 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Project.Persistence; +public static class ShaHash +{ + public static string HashPassword(string password) + { + using (SHA256 sha256Hash = SHA256.Create()) + { + // convert string to byte array and hash + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password)); + + // Byte-Array als Hex-String formatieren + StringBuilder builder = new StringBuilder(); + foreach (var b in bytes) + { + builder.Append(b.ToString("x2")); // x2 = hex format + } + return builder.ToString(); + } + } +} \ No newline at end of file diff --git a/Project.View/NewLogin/NewLogin.axaml.cs b/Project.View/NewLogin/NewLogin.axaml.cs index 5943c38..7fb5aed 100644 --- a/Project.View/NewLogin/NewLogin.axaml.cs +++ b/Project.View/NewLogin/NewLogin.axaml.cs @@ -26,13 +26,13 @@ public partial class NewLogin : Window string username = NewLoginUsernameBox.Text?.Trim(); string password = NewLoginPasswordBox.Text?.Trim(); string email = NewLoginEmailBox.Text?.Trim(); - - var user = new Model.NewUser(username, password, email); + string hashedPassword = ShaHash.HashPassword(password); + var user = new Model.NewUser(username, hashedPassword, email); SQLite.SaveUser(user); Close(); - } + private void NewLoginCancelOnClick(object? sender, RoutedEventArgs e) { Close();