changes on passmgr hasher for a permanent one - add user ok

pull/48/head
Alexandre AGOSTINHO 2 years ago
parent d47911bbed
commit 8580a6c1a4

@ -45,8 +45,7 @@ namespace ConsoleApp.Menu
preparationSteps: steps.ToArray() preparationSteps: steps.ToArray()
); );
masterMgr.AddRecipe(recipe);
masterMgr.DataMgr.Data[nameof(Recipe)].Add(recipe);
return null; return null;
} }
} }

@ -0,0 +1,44 @@
using ConsoleApp.Menu.Core;
using Model;
using Model.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal class AddUserMenu : Entry
{
MasterManager masterMgr;
public AddUserMenu(MasterManager masterManager)
: base("Add Manager",
new Entry.EntryStep("Mail: ", typeof(string)),
new Entry.EntryStep("Name: ", typeof(string)),
new Entry.EntryStep("Surname: ", typeof(string)),
new Entry.EntryStep("Password: ", typeof(string), true))
{
masterMgr = masterManager;
}
public override IMenu? Return()
{
string mail = _selectList[0].Item.Input;
string name = _selectList[1].Item.Input;
string surname = _selectList[2].Item.Input;
string passwd = _selectList[3].Item.Input;
User user = new User(
name: name,
surname: surname,
mail: mail,
password: passwd
);
masterMgr.Register(user);
return null;
}
}
}

@ -0,0 +1,40 @@
using ConsoleApp.Menu.Core;
using Model.Managers;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal class ExportRecipeMenu : SearcherRecipe
{
public ExportRecipeMenu(MasterManager masterManager)
: base(masterManager)
{
}
public override IMenu? Return()
{
if (CurrentSelected is null)
throw new ArgumentNullException();
Recipe recipe = CurrentSelected;
try
{
_masterMgr.DataMgr.Export(recipe, "");
}
catch (ArgumentNullException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("error: " + e.Message);
Console.ResetColor();
return this;
}
return null;
}
}
}

@ -0,0 +1,40 @@
using ConsoleApp.Menu.Core;
using Model;
using Model.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp.Menu
{
internal class ImportRecipeMenu : Entry
{
MasterManager masterMgr;
public ImportRecipeMenu(MasterManager masterManager)
: base("Import recipe",
new Entry.EntryStep("Path file: ", typeof(string)))
{
masterMgr = masterManager;
}
public override IMenu? Return()
{
string path = _selectList[0].Item.Input;
try
{
masterMgr.DataMgr.Import<Recipe>(path);
}
catch(ArgumentNullException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("error: " + e.Message);
Console.ResetColor();
return this;
}
return null;
}
}
}

@ -30,6 +30,13 @@ namespace ConsoleApp.Menu
_allSelectors.Add( _allSelectors.Add(
new Selector<IMenu>(new AddRecipeMenu(masterManager), "Add recipe")); new Selector<IMenu>(new AddRecipeMenu(masterManager), "Add recipe"));
_allSelectors.Add(
new Selector<IMenu>(new AddUserMenu(masterManager), "Add user"));
_allSelectors.Add(
new Selector<IMenu>(new ImportRecipeMenu(masterManager), "Import recipe"));
_allSelectors.Add(
new Selector<IMenu>(new ExportRecipeMenu(masterManager), "Export recipe"));
} }
protected override List<Selector<IMenu>> SearchInSelection() protected override List<Selector<IMenu>> SearchInSelection()

@ -48,7 +48,8 @@ namespace DataPersistence
typeof(Review), typeof(Review),
typeof(User), typeof(User),
typeof(Ingredient), typeof(Ingredient),
typeof(Quantity) typeof(Quantity),
typeof(PasswordManager)
}, },
PreserveObjectReferences = true PreserveObjectReferences = true
}; };

@ -8,8 +8,8 @@ namespace Model
{ {
public interface IPasswordManager public interface IPasswordManager
{ {
public int HashPassword(string password); public string HashPassword(string password);
public bool VerifyPassword(int hashedPassword,string password); public bool VerifyPassword(string hashedPassword,string password);
} }
} }

@ -1,27 +1,34 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Model namespace Model
{ {
[DataContract(Name = "passmgr")]
public class PasswordManager : IPasswordManager public class PasswordManager : IPasswordManager
{ {
public string HashPassword(string password)
public int HashPassword(string password)
{ {
int hashedPassword = password.GetHashCode(); byte[] data;
return hashedPassword; using (SHA256 sha256Hash = SHA256.Create())
data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
var sb = new StringBuilder();
foreach (byte b in data) sb.Append(b.ToString("x2"));
return sb.ToString();
} }
public bool VerifyPassword(int hashedPassword, string passwordEntered ) public bool VerifyPassword(string hashedPassword, string passwordEntered)
{ {
int passwordEnteredHashed = passwordEntered.GetHashCode(); string hashedInput = HashPassword(passwordEntered);
if ( passwordEnteredHashed == hashedPassword) StringComparer strcmp = StringComparer.OrdinalIgnoreCase;
return true;
else return false;
return strcmp.Compare(hashedPassword, hashedInput) == 0;
} }
} }
} }

@ -25,7 +25,7 @@ namespace Model
[DataMember] private string surname=""; [DataMember] private string surname="";
[DataMember] private string mail = ""; [DataMember] private string mail = "";
[DataMember] private string picture = ""; [DataMember] private string picture = "";
[DataMember] private int password ; [DataMember] private string password = "";
[DataMember] private List<Priority> priorities; [DataMember] private List<Priority> priorities;
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
@ -81,7 +81,7 @@ namespace Model
} }
} }
public int Password public string Password
{ {
get => password; get => password;
set => password = value; set => password = value;
@ -141,6 +141,7 @@ namespace Model
return $"{Name} {Surname}"; return $"{Name} {Surname}";
} }
[DataMember(Name = "passmgr")]
public IPasswordManager psswMgr { get; private set; } public IPasswordManager psswMgr { get; private set; }
@ -156,7 +157,7 @@ namespace Model
/// <param name="mail">The user needs an email to login.</param> /// <param name="mail">The user needs an email to login.</param>
/// <param name="password">The password of the new user.</param> /// <param name="password">The password of the new user.</param>
/// <param name="passwordManager">The password manager to manage the user password.</param> /// <param name="passwordManager">The password manager to manage the user password.</param>
public User(string name, string surname, string mail, string password, IPasswordManager passwordManager ) public User(string name, string surname, string mail, string password, IPasswordManager passwordManager)
{ {
Name = name; Name = name;
Surname = surname; Surname = surname;

Loading…
Cancel
Save