diff --git a/API/Hashing/HashingFactory.cs b/API/Hashing/HashingFactory.cs new file mode 100644 index 0000000..7494d62 --- /dev/null +++ b/API/Hashing/HashingFactory.cs @@ -0,0 +1,19 @@ +using API.Hashing.Interfaces; +using DAL.Models; + +namespace API.Hashing +{ + public class HashingFactory : IHashingFactory + { + public IHashingAlgorithm? getAlgorithm(HashingType type) + { + switch (type) + { + case HashingType.PBKDF2_SHA512_64_250000: + return new Pbkdf2(); + default: + return null; + } + } + } +} diff --git a/API/Hashing/Interfaces/IHashingAlgorithm.cs b/API/Hashing/Interfaces/IHashingAlgorithm.cs new file mode 100644 index 0000000..79ce48b --- /dev/null +++ b/API/Hashing/Interfaces/IHashingAlgorithm.cs @@ -0,0 +1,9 @@ +namespace API.Hashing.Interfaces +{ + public interface IHashingAlgorithm + { + public string hash(string password, out byte[] salt); + + public string hash(string password, byte[] salt); + } +} diff --git a/API/Hashing/Interfaces/IHashingFactory.cs b/API/Hashing/Interfaces/IHashingFactory.cs new file mode 100644 index 0000000..480bca1 --- /dev/null +++ b/API/Hashing/Interfaces/IHashingFactory.cs @@ -0,0 +1,9 @@ +using DAL.Models; + +namespace API.Hashing.Interfaces +{ + public interface IHashingFactory + { + public IHashingAlgorithm? getAlgorithm(HashingType type); + } +} diff --git a/API/Hashing/Pbkdf2.cs b/API/Hashing/Pbkdf2.cs new file mode 100644 index 0000000..3fbad8a --- /dev/null +++ b/API/Hashing/Pbkdf2.cs @@ -0,0 +1,28 @@ +using API.Hashing.Interfaces; +using System.Security.Cryptography; +using System.Text; + +namespace API.Hashing +{ + public class Pbkdf2 : IHashingAlgorithm + { + private const int KeySize = 512; + private const int Iterations = 250000; + private readonly HashAlgorithmName _algorithmName = HashAlgorithmName.SHA512; + + public string hash(string password, out byte[] salt) + { + salt = RandomNumberGenerator.GetBytes(KeySize); + + byte[] hash = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, Iterations, _algorithmName, KeySize); + + return Convert.ToHexString(hash); + } + public string hash(string password, byte[] salt) + { + byte[] hash = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, Iterations, _algorithmName, KeySize); + + return Convert.ToHexString(hash); + } + } +} diff --git a/API/Program.cs b/API/Program.cs index 690da9b..99d8cbe 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -1,5 +1,7 @@ using API.Authentication; using API.Authentication.Interfaces; +using API.Hashing; +using API.Hashing.Interfaces; using API.Services; using DAL.Contexts; using Microsoft.EntityFrameworkCore; @@ -40,6 +42,10 @@ namespace API builder.Services.AddTransient(); builder.Services.AddTransient(); + builder.Services.AddTransient(); + + builder.Services.AddTransient(); + WebApplication app = builder.Build(); if (app.Environment.IsDevelopment()) diff --git a/DAL/Models/User.cs b/DAL/Models/User.cs index fdbd035..482f8a1 100644 --- a/DAL/Models/User.cs +++ b/DAL/Models/User.cs @@ -9,7 +9,7 @@ namespace DAL.Models [JsonConverter(typeof(JsonStringEnumConverter))] public enum HashingType { - PBKDF2_SHA512_64_210000 + PBKDF2_SHA512_64_250000 } [Table("users")]