From dbac7bb6b6ad5827ccc8d2726992ab34108041d7 Mon Sep 17 00:00:00 2001 From: Matheo THIERRY Date: Tue, 30 May 2023 17:15:03 +0200 Subject: [PATCH] Fix hashCode to password for stub and for the reste --- notus/Biblioteque_de_Class/User.cs | 6 ++++++ notus/Notus_Console/Program.cs | 29 +++-------------------------- notus/Notus_Persistence/Stub.cs | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/notus/Biblioteque_de_Class/User.cs b/notus/Biblioteque_de_Class/User.cs index ddb793a..20b6d5c 100644 --- a/notus/Biblioteque_de_Class/User.cs +++ b/notus/Biblioteque_de_Class/User.cs @@ -14,6 +14,9 @@ namespace Biblioteque_de_Class private string Email { get; set; } [DataMember] private string Password { get; set; } + [DataMember] + private string Picture { get; set; } + [DataMember] private Theme Theme; [DataMember] private List NoteList; @@ -31,6 +34,7 @@ namespace Biblioteque_de_Class Username = username; Email = email; Password = password; + Picture = "defaultpicture.png"; NoteList = new List(); TagList = new List(); FavList = new List(); @@ -40,6 +44,7 @@ namespace Biblioteque_de_Class public string GetUsername() { return Username; } public string GetEmail() { return Email; } public string GetPassword() { return Password; } + public string GetPicture() { return Picture;} public Theme GetTheme() { return Theme; } public List GetNoteList() { return NoteList; } public List GetTagList() { return TagList; } @@ -51,6 +56,7 @@ namespace Biblioteque_de_Class public void SetUsername(string username) { Username = username; } public void SetEmail(string email) { Email = email; } public void SetPassword(string password) { Password = password; } + public void SetPicture(string picture) { Picture = picture; } public void SetTheme(Theme theme) { Theme = theme; } public void SetIsConnected(bool isConnected) { IsConnected = isConnected; } diff --git a/notus/Notus_Console/Program.cs b/notus/Notus_Console/Program.cs index 81da245..bdc9bbe 100644 --- a/notus/Notus_Console/Program.cs +++ b/notus/Notus_Console/Program.cs @@ -7,10 +7,6 @@ using System.Text; // load database PersistenceManager manager = new(new Stub()); Database db = manager.LoadDatabaseData(); -foreach(User user in db.GetUserList()) -{ - user.SetPassword(GetSHA256Hash(user.GetPassword())); -} // initialization zone============================================================================== @@ -139,22 +135,6 @@ List? choix_couleur() return colorList; } -static string GetSHA256Hash(string input) -{ - using (SHA256 sha256Hash = SHA256.Create()) - { - byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); - - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < bytes.Length; i++) - { - builder.Append(bytes[i].ToString("x2")); - } - - return builder.ToString(); - } -} - while (menu) { Console.WriteLine("\n|--------------------------------------|"); @@ -190,7 +170,6 @@ while (menu) Console.WriteLine("\nEntrez un password :"); string? password = Console.ReadLine(); if (password == null) { continue; } - password = GetSHA256Hash(password); try { u = db.GetUser(nom); @@ -202,7 +181,7 @@ while (menu) } if (!connection) { - if (Database.ComparePassword(u, password)) + if (Database.ComparePassword(u, password.GetHashCode().ToString())) { u.SetIsConnected(true); Console.WriteLine("\nConnection réussie !\n"); @@ -234,8 +213,7 @@ while (menu) } catch (AlreadyUsedException) { - password = GetSHA256Hash(password); - u = new User(nom, "", password); + u = new User(nom, "", password.GetHashCode().ToString()); db.AddUser(u); db.GetUser(nom).SetIsConnected(true); Console.WriteLine("\nConnection réussie !\n"); @@ -548,8 +526,7 @@ while (u.GetIsConnected()) Console.WriteLine("\nLe mot de passe doit contenir au moins 8 caractères.\n"); break; } - wantedNewPassword = GetSHA256Hash(wantedNewPassword); - if(wantedNewPassword == u.GetPassword()){ + if(wantedNewPassword.GetHashCode().ToString() == u.GetPassword()){ Console.WriteLine("\nLe nouveau mot de passe doit être différent de l'ancien.\n"); break; } diff --git a/notus/Notus_Persistence/Stub.cs b/notus/Notus_Persistence/Stub.cs index e32b77f..71e32ff 100644 --- a/notus/Notus_Persistence/Stub.cs +++ b/notus/Notus_Persistence/Stub.cs @@ -4,8 +4,25 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization.Json; using System.Text; +using System.Security.Cryptography; using System.Threading.Tasks; +static string GetSHA256Hash(string input) +{ + using (SHA256 sha256Hash = SHA256.Create()) + { + byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); + + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < bytes.Length; i++) + { + builder.Append(bytes[i].ToString("x2")); + } + + return builder.ToString(); + } +} + namespace Notus_Persistance { public class Stub : IManager @@ -59,6 +76,10 @@ namespace Notus_Persistance colorListHexaCode = new("000000,FFFFFF,000000".Split(',')); database.AddTheme(new Theme("Theme_3", colorListHexaCode)); + foreach (User user in database.GetUserList()) + { + user.SetPassword(GetSHA256Hash(user.GetPassword())); + } return database; }