First attempt at getting a little meta data on the client. Included a value for peppering all password hashing functions.
This commit is contained in:
@@ -6,6 +6,10 @@ namespace SecureCore.Authentication
|
||||
{
|
||||
public class PasswordManager
|
||||
{
|
||||
//Add some pepper to the passwords for good measure:
|
||||
//https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
|
||||
public static string Pepper { get; } = "rVk/OwQUw01qy76Q+5WimPk+NdqUMMghftMXyJzzckOj/+eFn056PDYzBD61E/ZNjRdgiMK6RhcHEcdfpJdbcw==";
|
||||
|
||||
private static string ConnectionString = @"Server=DESKTOP-OEDDVKC\SQLEXPRESS;Database=main;Integrated Security=true;";
|
||||
|
||||
public PasswordManager()
|
||||
@@ -18,14 +22,14 @@ namespace SecureCore.Authentication
|
||||
|
||||
ByteGenerator.GetRandomBytes(ref salt);
|
||||
|
||||
return (GetPasswordHash(password, salt), Convert.ToBase64String(salt));
|
||||
return (GetHash(password, salt), Convert.ToBase64String(salt));
|
||||
}
|
||||
|
||||
public static bool PasswordIsValid(string password, string salt, string passwordHash)
|
||||
{
|
||||
var saltBytes = Convert.FromBase64String(salt);
|
||||
|
||||
return passwordHash == GetPasswordHash(password, saltBytes);
|
||||
return passwordHash == GetHash(password, saltBytes);
|
||||
}
|
||||
|
||||
public static (string PasswordHash, string SaltHash) GetUserPasswordHash(int userId)
|
||||
@@ -49,9 +53,26 @@ namespace SecureCore.Authentication
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetPasswordHash(string password, byte[] salt)
|
||||
public static string HashStringData(string data, byte[] salt = null)
|
||||
{
|
||||
return Convert.ToBase64String(KeyDerivation.Pbkdf2(password, salt, Settings.KeyType, Settings.Iterations, Settings.KeySize));
|
||||
if (salt == null) salt = new byte[0];
|
||||
|
||||
return GetHash(data, salt);
|
||||
}
|
||||
|
||||
public static string HashStringData(string data, string salt = "")
|
||||
{
|
||||
var saltBytes = new byte[0];
|
||||
|
||||
if(!string.IsNullOrEmpty(salt))
|
||||
saltBytes = Convert.FromBase64String(salt);
|
||||
|
||||
return GetHash(data, saltBytes);
|
||||
}
|
||||
|
||||
private static string GetHash(string password, byte[] salt)
|
||||
{
|
||||
return Convert.ToBase64String(KeyDerivation.Pbkdf2($"{password}{Pepper}", salt, Settings.KeyType, Settings.Iterations, Settings.KeySize));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user