diff --git a/.vs/SecureCore/v16/.suo b/.vs/SecureCore/v16/.suo index 672d819..3132ef9 100644 Binary files a/.vs/SecureCore/v16/.suo and b/.vs/SecureCore/v16/.suo differ diff --git a/SecureCore/Authentication.cs b/SecureCore/Authentication.cs new file mode 100644 index 0000000..ce14b66 --- /dev/null +++ b/SecureCore/Authentication.cs @@ -0,0 +1,57 @@ +using System; +using System.Security.Cryptography; +using Microsoft.AspNetCore.Cryptography.KeyDerivation; + +namespace SecureCore +{ + public class Authentication + { + private static int Iterations { get; set; } = 100000; + private static KeyDerivationPrf KeyType { get; } = KeyDerivationPrf.HMACSHA512; + private static int KeySize { get; } = 512 / 8; + private static int SaltSize { get; } = 128 / 8; //128 bit salt + private static int SessionKeySize { get; } = 32; //32 bytes + + public static string CreateSessionId() + { + var sessionId = new byte[SessionKeySize]; + + GetRandomBytes(ref sessionId); + + return Convert.ToBase64String(sessionId); + } + + public static (string Hash, string Salt) HashPassword(string password) + { + var salt = new byte[SaltSize]; + + GetRandomBytes(ref salt); + + return (GetPasswordHash(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); + } + + private static string GetPasswordHash(string password, byte[] salt) + { + return Convert.ToBase64String(KeyDerivation.Pbkdf2(password, salt, KeyType, Iterations, KeySize)); + } + + private static void GetRandomBytes(ref byte[] bytes) + { + using var rng = RandomNumberGenerator.Create(); + rng.GetBytes(bytes); + } + } + + public class LoginInfo + { + public string UserName { get; set; } + public string Password { get; set; } + } +} diff --git a/SecureCore/Controllers/AuthController.cs b/SecureCore/Controllers/AuthController.cs new file mode 100644 index 0000000..141179a --- /dev/null +++ b/SecureCore/Controllers/AuthController.cs @@ -0,0 +1,22 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SecureCore.Controllers +{ + [Route("[controller]")] + [ApiController] + public class AuthController : Controller + { + [HttpPost("login")] + [AcceptVerbs("POST")] + public IActionResult Login(LoginInfo info) + { + var (hash, salt) = Authentication.HashPassword(info.Password); + + return Ok($"Session Key: {Authentication.CreateSessionId()}{Environment.NewLine}Password Hash: {hash}{Environment.NewLine}Salt: {salt}{Environment.NewLine}"); + } + } +} diff --git a/SecureCore/Controllers/EmployeeController.cs b/SecureCore/Controllers/EmployeeController.cs index 6fb0af4..080fd09 100644 --- a/SecureCore/Controllers/EmployeeController.cs +++ b/SecureCore/Controllers/EmployeeController.cs @@ -34,65 +34,19 @@ namespace SecureCore.Controllers return Ok(DataService.GetById(id)); } - [HttpPost("login")] - [AcceptVerbs("POST")] - public IActionResult Login(LoginInfo info) - { - var hash = Auth.HashPassword(info.Password); - var h2 = Auth.HashPassword("Nonesense", "vRWp9TP1nQDLGtDZrd2yuw=="); + //[HttpPost("login")] + //[AcceptVerbs("POST")] + //public IActionResult Login(LoginInfo info) + //{ + // var (Hash, Salt) = Auth.HashPassword(info.Password); - return Ok($"Session Key: {Auth.CreateSessionId()}{Environment.NewLine}Password Hash: {hash.Hash}{Environment.NewLine}Salt: {hash.Salt}{Environment.NewLine}{h2 == "+IGLf8scewY2LOObXqfF5IkIbhcEuPrMFc12d78jH6ZyBEMQI+Z9zixWgkQANeV3VYvURBEXIVI0/TPZrnML3w=="}"); - } + // return Ok($"Session Key: {Auth.CreateSessionId()}{Environment.NewLine}Password Hash: {Hash}{Environment.NewLine}Salt: {Salt}{Environment.NewLine}"); + //} } - public class LoginInfo + public class AuthenticatedUser { public string UserName { get; set; } - public string Password { get; set; } - } - - public static class Auth - { - private static int Iterations { get; set; } = 100000; - private static KeyDerivationPrf KeyType { get; } = KeyDerivationPrf.HMACSHA512; - private static int KeySize { get; } = 512 / 8; - private static int SaltSize { get; } = 128 / 8; //128 bit salt - private static int SessionKeySize { get; } = 32; //32 bytes - - public static string CreateSessionId() - { - var sessionId = new byte[SessionKeySize]; - - GetRandomBytes(ref sessionId); - - return Convert.ToBase64String(sessionId); - } - - public static (string Hash, string Salt) HashPassword(string password) - { - var salt = new byte[SaltSize]; - - GetRandomBytes(ref salt); - - return (GetPasswordHash(password, salt), Convert.ToBase64String(salt)); - } - - public static string HashPassword(string password, string hash) - { - var salt = Convert.FromBase64String(hash); - - return GetPasswordHash(password, salt); - } - - private static string GetPasswordHash(string password, byte[] salt) - { - return Convert.ToBase64String(KeyDerivation.Pbkdf2(password, salt, KeyType, Iterations, KeySize)); - } - - private static void GetRandomBytes(ref byte[] bytes) - { - using var rng = RandomNumberGenerator.Create(); - rng.GetBytes(bytes); - } + public string SessionToken { get; set; } } } diff --git a/SecureCore/SecureCore.csproj.user b/SecureCore/SecureCore.csproj.user index 3577531..ea4f2b7 100644 --- a/SecureCore/SecureCore.csproj.user +++ b/SecureCore/SecureCore.csproj.user @@ -1,7 +1,7 @@  - MvcControllerWithActionsScaffolder + MvcControllerEmptyScaffolder root/Common/MVC/Controller FolderProfile diff --git a/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.dll b/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.dll index 5dc9aeb..109ab6c 100644 Binary files a/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.dll and b/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.dll differ diff --git a/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.pdb b/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.pdb index 797e81d..02e6240 100644 Binary files a/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.pdb and b/SecureCore/bin/Debug/netcoreapp3.1/SecureCore.pdb differ diff --git a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csproj.CoreCompileInputs.cache b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csproj.CoreCompileInputs.cache index d708cbc..423acda 100644 --- a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csproj.CoreCompileInputs.cache +++ b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -7632b3c3dac860a5ca892b30d9c2c673c6b08242 +fe49541232d842c53eb06770dc71b931991ff712 diff --git a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csprojAssemblyReference.cache b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csprojAssemblyReference.cache index 773aab8..626c031 100644 Binary files a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csprojAssemblyReference.cache and b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.csprojAssemblyReference.cache differ diff --git a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.dll b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.dll index 5dc9aeb..109ab6c 100644 Binary files a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.dll and b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.dll differ diff --git a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.pdb b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.pdb index 797e81d..02e6240 100644 Binary files a/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.pdb and b/SecureCore/obj/Debug/netcoreapp3.1/SecureCore.pdb differ