Hi, ich hab mich ein wenig mit 'hashing' beschäftigt. Ich möchte meine User-Passwörter gehashed in meiner Cloud Datenbank speichern.
Ich hab dazu eine Class mit zwei Methoden erstellt eine CreateHash()->HashString und ein ConfirmHash()->bool.
Könnte jemand sagen, ob ich dass korrekt umgesetzt habe oder hab ich mich da selber irgendwo versalzen oder verpfeffert ?
CreateHash:
Check Hash:
LG nogood
Ich hab dazu eine Class mit zwei Methoden erstellt eine CreateHash()->HashString und ein ConfirmHash()->bool.
Könnte jemand sagen, ob ich dass korrekt umgesetzt habe oder hab ich mich da selber irgendwo versalzen oder verpfeffert ?
CreateHash:
C#-Quellcode
- public enum Supported_Hashes
- {
- SHA256, SHA384, SHA512
- }
- public class Hashing
- {
- private static readonly Random rnd = new();
- private static readonly RNGCryptoServiceProvider rng = new();
- //TODO ist UTF8 richtig?
- public string CreateHash(string inputText, Supported_Hashes hashType,byte[] salt, string pepper)
- {
- int minSaltLength = 12;
- int maxSaltLenth = 16;
- byte[] SaltBytes;
- byte[] PepperBytes = Encoding.UTF8.GetBytes(pepper);
- if (salt != null)
- {
- SaltBytes = salt;
- }
- else
- {
- int Saltlength = rnd.Next(minSaltLength, maxSaltLenth);
- SaltBytes = new byte[Saltlength];
- rng.GetNonZeroBytes(SaltBytes);
- rng.Dispose();
- }
- byte[] inputData = Encoding.UTF8.GetBytes(inputText);
- byte[] inputDataAndSalt = new byte[inputText.Length + SaltBytes.Length];
- byte[] inputDataAndSaltAndPepper = new byte[inputText.Length + SaltBytes.Length + PepperBytes.Length];
- //preset
- for (int i = 0; i < inputData.Length; i++)
- inputDataAndSaltAndPepper[i] = inputData[i];
- for (int i = 0; i < SaltBytes.Length; i++)
- inputDataAndSalt[inputData.Length + i] = SaltBytes[i];
- for (int i = 0; i < PepperBytes.Length; i++)
- inputDataAndSaltAndPepper[inputDataAndSalt.Length + i] = PepperBytes[i];
- byte[] hashValue = null;
- switch (hashType)
- {
- case Supported_Hashes.SHA256:
- SHA256Managed sha256 = new();
- hashValue = sha256.ComputeHash(inputDataAndSaltAndPepper);
- sha256.Dispose();
- break;
- case Supported_Hashes.SHA384:
- SHA384Managed sha384 = new();
- hashValue = sha384.ComputeHash(inputDataAndSaltAndPepper);
- sha384.Dispose();
- break;
- case Supported_Hashes.SHA512:
- SHA512Managed sha512 = new();
- hashValue = sha512.ComputeHash(inputDataAndSaltAndPepper);
- sha512.Dispose();
- break;
- }
- //TODO hash+salt but not the pepper !? Correct
- byte[] result = new byte[hashValue.Length + SaltBytes.Length];
- for (int i = 0; i < hashValue.Length; i++)
- result[i] = hashValue[i];
- for (int i = 0; i < SaltBytes.Length; i++)
- result[hashValue.Length + i] = SaltBytes[i];
- return Convert.ToBase64String(result);
- }
Check Hash:
C#-Quellcode
- public bool ConfirmHash(string inputText, string confirmHashValue, Supported_Hashes hashType, string pepper)
- {
- byte[] confirmHashBytes = Convert.FromBase64String(confirmHashValue);
- int hashSize = 0;
- switch (hashType)
- {
- case Supported_Hashes.SHA256:
- hashSize = 32;
- break;
- case Supported_Hashes.SHA384:
- hashSize = 48;
- break;
- case Supported_Hashes.SHA512:
- hashSize = 64;
- break;
- }
- byte[] saltBytes = new byte[confirmHashBytes.Length - hashSize];
- for (int i = 0; i < saltBytes.Length; i++)
- saltBytes[i] = confirmHashBytes[hashSize + i];
- string controlRehashValue = CreateHash(inputText, hashType, saltBytes, pepper);
- return confirmHashValue == controlRehashValue;
- }
LG nogood
codewars.com Rank: 4 kyu