Fix hashCode to password for stub and for the reste

pull/19/head
Matheo THIERRY 2 years ago
parent 4129d5399f
commit dbac7bb6b6

@ -14,6 +14,9 @@ namespace Biblioteque_de_Class
private string Email { get; set; } private string Email { get; set; }
[DataMember] [DataMember]
private string Password { get; set; } private string Password { get; set; }
[DataMember]
private string Picture { get; set; }
[DataMember]
private Theme Theme; private Theme Theme;
[DataMember] [DataMember]
private List<Note> NoteList; private List<Note> NoteList;
@ -31,6 +34,7 @@ namespace Biblioteque_de_Class
Username = username; Username = username;
Email = email; Email = email;
Password = password; Password = password;
Picture = "defaultpicture.png";
NoteList = new List<Note>(); NoteList = new List<Note>();
TagList = new List<Tags>(); TagList = new List<Tags>();
FavList = new List<Note>(); FavList = new List<Note>();
@ -40,6 +44,7 @@ namespace Biblioteque_de_Class
public string GetUsername() { return Username; } public string GetUsername() { return Username; }
public string GetEmail() { return Email; } public string GetEmail() { return Email; }
public string GetPassword() { return Password; } public string GetPassword() { return Password; }
public string GetPicture() { return Picture;}
public Theme GetTheme() { return Theme; } public Theme GetTheme() { return Theme; }
public List<Note> GetNoteList() { return NoteList; } public List<Note> GetNoteList() { return NoteList; }
public List<Tags> GetTagList() { return TagList; } public List<Tags> GetTagList() { return TagList; }
@ -51,6 +56,7 @@ namespace Biblioteque_de_Class
public void SetUsername(string username) { Username = username; } public void SetUsername(string username) { Username = username; }
public void SetEmail(string email) { Email = email; } public void SetEmail(string email) { Email = email; }
public void SetPassword(string password) { Password = password; } public void SetPassword(string password) { Password = password; }
public void SetPicture(string picture) { Picture = picture; }
public void SetTheme(Theme theme) { Theme = theme; } public void SetTheme(Theme theme) { Theme = theme; }
public void SetIsConnected(bool isConnected) { IsConnected = isConnected; } public void SetIsConnected(bool isConnected) { IsConnected = isConnected; }

@ -7,10 +7,6 @@ using System.Text;
// load database // load database
PersistenceManager manager = new(new Stub()); PersistenceManager manager = new(new Stub());
Database db = manager.LoadDatabaseData(); Database db = manager.LoadDatabaseData();
foreach(User user in db.GetUserList())
{
user.SetPassword(GetSHA256Hash(user.GetPassword()));
}
// initialization zone============================================================================== // initialization zone==============================================================================
@ -139,22 +135,6 @@ List<string>? choix_couleur()
return colorList; 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) while (menu)
{ {
Console.WriteLine("\n|--------------------------------------|"); Console.WriteLine("\n|--------------------------------------|");
@ -190,7 +170,6 @@ while (menu)
Console.WriteLine("\nEntrez un password :"); Console.WriteLine("\nEntrez un password :");
string? password = Console.ReadLine(); string? password = Console.ReadLine();
if (password == null) { continue; } if (password == null) { continue; }
password = GetSHA256Hash(password);
try try
{ {
u = db.GetUser(nom); u = db.GetUser(nom);
@ -202,7 +181,7 @@ while (menu)
} }
if (!connection) if (!connection)
{ {
if (Database.ComparePassword(u, password)) if (Database.ComparePassword(u, password.GetHashCode().ToString()))
{ {
u.SetIsConnected(true); u.SetIsConnected(true);
Console.WriteLine("\nConnection réussie !\n"); Console.WriteLine("\nConnection réussie !\n");
@ -234,8 +213,7 @@ while (menu)
} }
catch (AlreadyUsedException) catch (AlreadyUsedException)
{ {
password = GetSHA256Hash(password); u = new User(nom, "", password.GetHashCode().ToString());
u = new User(nom, "", password);
db.AddUser(u); db.AddUser(u);
db.GetUser(nom).SetIsConnected(true); db.GetUser(nom).SetIsConnected(true);
Console.WriteLine("\nConnection réussie !\n"); 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"); Console.WriteLine("\nLe mot de passe doit contenir au moins 8 caractères.\n");
break; break;
} }
wantedNewPassword = GetSHA256Hash(wantedNewPassword); if(wantedNewPassword.GetHashCode().ToString() == u.GetPassword()){
if(wantedNewPassword == u.GetPassword()){
Console.WriteLine("\nLe nouveau mot de passe doit être différent de l'ancien.\n"); Console.WriteLine("\nLe nouveau mot de passe doit être différent de l'ancien.\n");
break; break;
} }

@ -4,8 +4,25 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.Serialization.Json; using System.Runtime.Serialization.Json;
using System.Text; using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks; 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 namespace Notus_Persistance
{ {
public class Stub : IManager public class Stub : IManager
@ -59,6 +76,10 @@ namespace Notus_Persistance
colorListHexaCode = new("000000,FFFFFF,000000".Split(',')); colorListHexaCode = new("000000,FFFFFF,000000".Split(','));
database.AddTheme(new Theme("Theme_3", colorListHexaCode)); database.AddTheme(new Theme("Theme_3", colorListHexaCode));
foreach (User user in database.GetUserList())
{
user.SetPassword(GetSHA256Hash(user.GetPassword()));
}
return database; return database;
} }

Loading…
Cancel
Save