Hash and salt passwords in C#

后端 未结 14 1822
野趣味
野趣味 2020-11-22 04:00

I was just going through one of DavidHayden\'s articles on Hashing User Passwords.

Really I can\'t get what he is trying to achieve.

Here is his code:

<
14条回答
  •  清酒与你
    2020-11-22 04:16

    I created a class that has the following method:

    1. Create Salt

    2. Hash Input

    3. Validate input

      public class CryptographyProcessor
      {
          public string CreateSalt(int size)
          {
              //Generate a cryptographic random number.
              RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
              byte[] buff = new byte[size];
              rng.GetBytes(buff);
              return Convert.ToBase64String(buff);
          }
      
          public string GenerateHash(string input, string salt)
          { 
              byte[] bytes = Encoding.UTF8.GetBytes(input + salt);
              SHA256Managed sHA256ManagedString = new SHA256Managed();
              byte[] hash = sHA256ManagedString.ComputeHash(bytes);
              return Convert.ToBase64String(hash);
          }
      
          public bool AreEqual(string plainTextInput, string hashedInput, string salt)
          {
              string newHashedPin = GenerateHash(plainTextInput, salt);
              return newHashedPin.Equals(hashedInput); 
          }
      }
      

提交回复
热议问题