From d559f1f0546f04c6b7d1c8d21da4fed7939d18e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ben=C3=AD=C4=8Dek?= Date: Sat, 28 Oct 2023 14:28:06 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99idejte=20soubory=20projektu.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HashedPassword.cs | 14 ++++ Lib_PasswordHashing.csproj | 9 +++ Lib_PasswordHashing.sln | 25 +++++++ PasswordHashing.cs | 144 +++++++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 HashedPassword.cs create mode 100644 Lib_PasswordHashing.csproj create mode 100644 Lib_PasswordHashing.sln create mode 100644 PasswordHashing.cs diff --git a/HashedPassword.cs b/HashedPassword.cs new file mode 100644 index 0000000..56a0121 --- /dev/null +++ b/HashedPassword.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lib_PasswordHashing +{ + public class HashedPassword + { + public string Salt { get; set; } + public string Hash { get; set; } + } +} diff --git a/Lib_PasswordHashing.csproj b/Lib_PasswordHashing.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/Lib_PasswordHashing.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Lib_PasswordHashing.sln b/Lib_PasswordHashing.sln new file mode 100644 index 0000000..1cd4589 --- /dev/null +++ b/Lib_PasswordHashing.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.6.33723.286 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib_PasswordHashing", "Lib_PasswordHashing.csproj", "{81FE90EF-3886-4226-B644-ACF0D881C0BF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {81FE90EF-3886-4226-B644-ACF0D881C0BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81FE90EF-3886-4226-B644-ACF0D881C0BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81FE90EF-3886-4226-B644-ACF0D881C0BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81FE90EF-3886-4226-B644-ACF0D881C0BF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A3715767-5B5F-409C-93F3-EB71DD970884} + EndGlobalSection +EndGlobal diff --git a/PasswordHashing.cs b/PasswordHashing.cs new file mode 100644 index 0000000..4f8be6b --- /dev/null +++ b/PasswordHashing.cs @@ -0,0 +1,144 @@ +using System.Security.Cryptography; +using System.Text; + +namespace Lib_PasswordHashing +{ + public class PasswordHashing + { + private readonly char[] SaltChars; + private readonly HashAlgorithm Algorithm; + + /// + /// Initialize Password hashing function + /// + /// Hashing algorithm + /// Chars for generate salt in string form + public PasswordHashing(HashAlgorithm? algorithm = null, string saltChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") + { + if (algorithm == null) + { + Algorithm = SHA512.Create(); //if not specified use default SHA512 + } + else + { + Algorithm = algorithm; //if algorithm specified use them + } + + SaltChars = saltChars.ToCharArray(); //convert salt chars string to array for use him + } + + /// + /// Hash password with salt + /// + /// Password string + /// Password hash and salt + public HashedPassword HashPass(string password) + { + HashedPassword hp = GenerateSaltedPassword(password); //Generate Salted Password and salt + byte[] sp = Encoding.UTF32.GetBytes(hp.Hash); //Convert salted password to Byte array + + byte[] hash = Algorithm.ComputeHash(sp); //Compute hash + hp.Hash = Convert.ToBase64String(hash); //Convert Byte array to hash + + return hp; //return hash and salt + } + + /// + /// Compare password with hash and salt + /// + /// password + /// hash + /// salt + /// Hash and Pass with salt same? + public bool VerifyPass(string password, string Hash, string Salt) + { + string hash = Convert.ToBase64String( Algorithm.ComputeHash( Encoding.UTF8.GetBytes( GenerateSaltedPassword(password, Salt.ToArray()) ) ) ); //generate hash from password and salt + + if (hash == Hash) //is password correct? + { + return true; //yes + } + else //or + { + return false; //no + } + } + + + + + /// + /// Generate new salted Password + /// + /// Password + /// Salted password and salt + private HashedPassword GenerateSaltedPassword(string password) + { + char[] passwordSalt = GenerateSalt(password.Length); //Generate password salt + + return new HashedPassword() { Hash = GenerateSaltedPassword(password, passwordSalt), Salt = new string(passwordSalt) }; //return HashedPassword with salted password as Hash and Salt + } + + /// + /// Generate salted Password + /// + /// Password + /// Salt for mix with password + /// Salted password and salt + private string GenerateSaltedPassword(string password, char[] passwordSalt) + { + List saltedPassword = new List(); //Initialize list for create Salted Password + char[] passwordChars = password.ToCharArray(); //Transfer password string to Array + + for (int i = 0; passwordChars.Length >= i; i++) //Loop for mixing password and salt + { + saltedPassword.Add(passwordChars[i]); //Add password char to salted password + saltedPassword.Add(passwordSalt[i]); //add salt char to salted password + } + + return new string(saltedPassword.ToArray()); //Return string with saltedpassword + } + + /// + /// Generate salt + /// + /// Number of salt chars + /// Salt + private char[] GenerateSalt(int lenght) + { + List salt = new List(); //Create List for generate Salt string + + for (int i = lenght; i > 0; i--) //Loop for generate requested lenght of salt + { + salt.Add(SaltChars[Random.Shared.Next(0, SaltChars.Length - 1)]); //Random generate salt char + } + + return salt.ToArray(); //return salt in array + } + + + + + + + + + + + + + + + + + + + + + + + + + + } +} \ No newline at end of file