ASP.NET Identity's default Password Hasher - How does it work and is it secure? ASP.NET Identity's default Password Hasher - How does it work and is it secure? asp.net asp.net

ASP.NET Identity's default Password Hasher - How does it work and is it secure?


Here is how the default implementation (ASP.NET Framework or ASP.NET Core) works. It uses a Key Derivation Function with random salt to produce the hash. The salt is included as part of the output of the KDF. Thus, each time you "hash" the same password you will get different hashes. To verify the hash the output is split back to the salt and the rest, and the KDF is run again on the password with the specified salt. If the result matches to the rest of the initial output the hash is verified.

Hashing:

public static string HashPassword(string password){    byte[] salt;    byte[] buffer2;    if (password == null)    {        throw new ArgumentNullException("password");    }    using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))    {        salt = bytes.Salt;        buffer2 = bytes.GetBytes(0x20);    }    byte[] dst = new byte[0x31];    Buffer.BlockCopy(salt, 0, dst, 1, 0x10);    Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);    return Convert.ToBase64String(dst);}

Verifying:

public static bool VerifyHashedPassword(string hashedPassword, string password){    byte[] buffer4;    if (hashedPassword == null)    {        return false;    }    if (password == null)    {        throw new ArgumentNullException("password");    }    byte[] src = Convert.FromBase64String(hashedPassword);    if ((src.Length != 0x31) || (src[0] != 0))    {        return false;    }    byte[] dst = new byte[0x10];    Buffer.BlockCopy(src, 1, dst, 0, 0x10);    byte[] buffer3 = new byte[0x20];    Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);    using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))    {        buffer4 = bytes.GetBytes(0x20);    }    return ByteArraysEqual(buffer3, buffer4);}


Because these days ASP.NET is open source, you can find it on GitHub: AspNet.Identity 3.0 and AspNet.Identity 2.0.

From the comments:

/* ======================= * HASHED PASSWORD FORMATS * ======================= *  * Version 2: * PBKDF2 with HMAC-SHA1, 128-bit salt, 256-bit subkey, 1000 iterations. * (See also: SDL crypto guidelines v5.1, Part III) * Format: { 0x00, salt, subkey } * * Version 3: * PBKDF2 with HMAC-SHA256, 128-bit salt, 256-bit subkey, 10000 iterations. * Format: { 0x01, prf (UInt32), iter count (UInt32), salt length (UInt32), salt, subkey } * (All UInt32s are stored big-endian.) */


I understand the accepted answer, and have up-voted it but thought I'd dump my laymen's answer here...

Creating a hash

  1. The salt is randomly generated using the functionRfc2898DeriveBytes which generates a hash and a salt. Inputs to Rfc2898DeriveBytes are the password, the size of the salt to generate and the number of hashing iterations to perform.https://msdn.microsoft.com/en-us/library/h83s4e12(v=vs.110).aspx
  2. The salt and the hash are then mashed together(salt first followedby the hash) and encoded as a string (so the salt is encoded in thehash). This encoded hash (which contains the salt and hash) is thenstored (typically) in the database against the user.

Checking a password against a hash

To check a password that a user inputs.

  1. The salt is extracted from the stored hashed password.
  2. The salt is used to hash the users input password using an overload of Rfc2898DeriveBytes which takes a salt instead of generating one. https://msdn.microsoft.com/en-us/library/yx129kfs(v=vs.110).aspx
  3. The stored hash and the test hash are then compared.

The Hash

Under the covers the hash is generated using the SHA1 hash function (https://en.wikipedia.org/wiki/SHA-1). This function is iteratively called 1000 times (In the default Identity implementation)

Why is this secure

  • Random salts means that an attacker can’t use a pre-generated tableof hashs to try and break passwords. They would need to generate ahash table for every salt. (Assuming here that the hacker has also compromised your salt)
  • If 2 passwords are identical they willhave different hashes. (meaning attackers can’t infer ‘common’passwords)
  • Iteratively calling SHA1 1000 times means that theattacker also needs to do this. The idea being that unless they havetime on a supercomputer they won’t have enough resource to bruteforce the password from the hash. It would massively slow down the time to generate a hash table for a given salt.