Hash and salt passwords in C#

后端 未结 14 1849
野趣味
野趣味 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:35

    I have made a library SimpleHashing.Net to make the process of hashing easy with basic classes provided by Microsoft. Ordinary SHA is not really enough to have passwords stored securely anymore.

    The library use the idea of hash format from Bcrypt, but since there is no official MS implementation I prefer to use what's available in the framework (i.e. PBKDF2), but it's a bit too hard out of the box.

    This is a quick example how to use the library:

    ISimpleHash simpleHash = new SimpleHash();
    
    // Creating a user hash, hashedPassword can be stored in a database
    // hashedPassword contains the number of iterations and salt inside it similar to bcrypt format
    string hashedPassword = simpleHash.Compute("Password123");
    
    // Validating user's password by first loading it from database by username
    string storedHash = _repository.GetUserPasswordHash(username);
    isPasswordValid = simpleHash.Verify("Password123", storedHash);
    
    0 讨论(0)
  • 2020-11-22 04:37

    Actually this is kind of strange, with the string conversions - which the membership provider does to put them into config files. Hashes and salts are binary blobs, you don't need to convert them to strings unless you want to put them into text files.

    In my book, Beginning ASP.NET Security, (oh finally, an excuse to pimp the book) I do the following

    static byte[] GenerateSaltedHash(byte[] plainText, byte[] salt)
    {
      HashAlgorithm algorithm = new SHA256Managed();
    
      byte[] plainTextWithSaltBytes = 
        new byte[plainText.Length + salt.Length];
    
      for (int i = 0; i < plainText.Length; i++)
      {
        plainTextWithSaltBytes[i] = plainText[i];
      }
      for (int i = 0; i < salt.Length; i++)
      {
        plainTextWithSaltBytes[plainText.Length + i] = salt[i];
      }
    
      return algorithm.ComputeHash(plainTextWithSaltBytes);            
    }
    

    The salt generation is as the example in the question. You can convert text to byte arrays using Encoding.UTF8.GetBytes(string). If you must convert a hash to its string representation you can use Convert.ToBase64String and Convert.FromBase64String to convert it back.

    You should note that you cannot use the equality operator on byte arrays, it checks references and so you should simply loop through both arrays checking each byte thus

    public static bool CompareByteArrays(byte[] array1, byte[] array2)
    {
      if (array1.Length != array2.Length)
      {
        return false;
      }
    
      for (int i = 0; i < array1.Length; i++)
      {
        if (array1[i] != array2[i])
        {
          return false;
        }
      }
    
      return true;
    }
    

    Always use a new salt per password. Salts do not have to be kept secret and can be stored alongside the hash itself.

    0 讨论(0)
提交回复
热议问题