C# ASP.NET标识';s默认密码哈希器-它是如何工作的,是否安全?

C# ASP.NET标识';s默认密码哈希器-它是如何工作的,是否安全?,c#,asp.net,security,passwords,asp.net-identity,C#,Asp.net,Security,Passwords,Asp.net Identity,我想知道MVC 5和ASP.NET Identity Framework附带的默认实现的密码哈希器是否足够安全?如果是这样的话,你能给我解释一下它是怎么工作的吗 IPasswordHasher接口如下所示: public interface IPasswordHasher { string HashPassword(string password); PasswordVerificationResult VerifyHashedPassword(string hashedPass

我想知道MVC 5和ASP.NET Identity Framework附带的默认实现的密码哈希器是否足够安全?如果是这样的话,你能给我解释一下它是怎么工作的吗

IPasswordHasher接口如下所示:

public interface IPasswordHasher
{
    string HashPassword(string password);
    PasswordVerificationResult VerifyHashedPassword(string hashedPassword, 
                                                       string providedPassword);
}
正如你所看到的,它不需要盐,但在这篇文章中提到了它:“ 事实上,它确实在幕后给它加盐。所以我想知道它是怎么做到的?这盐是从哪里来的


我担心的是salt是静态的,这使得它非常不安全。

以下是默认实现(or)的工作方式。它使用随机盐生成散列。盐是KDF输出的一部分。因此,每次“散列”相同密码时,您将得到不同的散列。为了验证散列,将输出拆分回salt和其余部分,并使用指定的salt在密码上再次运行KDF。如果结果与初始输出的其余部分匹配,则验证哈希

散列:

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);
}
验证:

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);
}

因为现在ASP.NET是开源的,所以您可以在GitHub上找到它: 和

从评论中:

/* =======================
 * 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.)
 */

对于像我这样的新手来说,这里有一段使用const的代码和一种比较字节[]的实际方法。我从stackoverflow获得了所有这些代码,但定义了常量,因此值可以更改,也可以更改

// 24 = 192 bits
    private const int SaltByteSize = 24;
    private const int HashByteSize = 24;
    private const int HasingIterationsCount = 10101;


    public static string HashPassword(string password)
    {
        // http://stackoverflow.com/questions/19957176/asp-net-identity-password-hashing

        byte[] salt;
        byte[] buffer2;
        if (password == null)
        {
            throw new ArgumentNullException("password");
        }
        using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, SaltByteSize, HasingIterationsCount))
        {
            salt = bytes.Salt;
            buffer2 = bytes.GetBytes(HashByteSize);
        }
        byte[] dst = new byte[(SaltByteSize + HashByteSize) + 1];
        Buffer.BlockCopy(salt, 0, dst, 1, SaltByteSize);
        Buffer.BlockCopy(buffer2, 0, dst, SaltByteSize + 1, HashByteSize);
        return Convert.ToBase64String(dst);
    }

    public static bool VerifyHashedPassword(string hashedPassword, string password)
    {
        byte[] _passwordHashBytes;

        int _arrayLen = (SaltByteSize + HashByteSize) + 1;

        if (hashedPassword == null)
        {
            return false;
        }

        if (password == null)
        {
            throw new ArgumentNullException("password");
        }

        byte[] src = Convert.FromBase64String(hashedPassword);

        if ((src.Length != _arrayLen) || (src[0] != 0))
        {
            return false;
        }

        byte[] _currentSaltBytes = new byte[SaltByteSize];
        Buffer.BlockCopy(src, 1, _currentSaltBytes, 0, SaltByteSize);

        byte[] _currentHashBytes = new byte[HashByteSize];
        Buffer.BlockCopy(src, SaltByteSize + 1, _currentHashBytes, 0, HashByteSize);

        using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, _currentSaltBytes, HasingIterationsCount))
        {
            _passwordHashBytes = bytes.GetBytes(SaltByteSize);
        }

        return AreHashesEqual(_currentHashBytes, _passwordHashBytes);

    }

    private static bool AreHashesEqual(byte[] firstHash, byte[] secondHash)
    {
        int _minHashLength = firstHash.Length <= secondHash.Length ? firstHash.Length : secondHash.Length;
        var xor = firstHash.Length ^ secondHash.Length;
        for (int i = 0; i < _minHashLength; i++)
            xor |= firstHash[i] ^ secondHash[i];
        return 0 == xor;
    }
//24=192位
私有常量int SaltByteSize=24;
私有常量int HashByteSize=24;
私有常量int HasingIterationCount=10101;
公共静态字符串HashPassword(字符串密码)
{
// http://stackoverflow.com/questions/19957176/asp-net-identity-password-hashing
字节[]盐;
字节[]缓冲区2;
如果(密码==null)
{
抛出新的ArgumentNullException(“密码”);
}
使用(Rfc2898DeriveBytes=新的Rfc2898DeriveBytes(密码、SaltByteSize、hasingIterationCount))
{
salt=bytes.salt;
buffer2=bytes.GetBytes(HashByteSize);
}
字节[]dst=新字节[(SaltByteSize+HashByteSize)+1];
块拷贝(salt,0,dst,1,SaltByteSize);
Buffer.BlockCopy(buffer2,0,dst,SaltByteSize+1,HashByteSize);
返回Convert.tobase64字符串(dst);
}
公共静态bool VerifyHashedPassword(字符串hashedPassword,字符串密码)
{
字节[]_密码hashbytes;
int_arrayLen=(SaltByteSize+HashByteSize)+1;
if(hashedPassword==null)
{
返回false;
}
如果(密码==null)
{
抛出新的ArgumentNullException(“密码”);
}
字节[]src=Convert.FromBase64String(hashedPassword);
if((src.Length!=_arrayLen)| |(src[0]!=0))
{
返回false;
}
字节[]_currentSaltBytes=新字节[SaltByteSize];
块复制(src,1,_currentSaltBytes,0,SaltByteSize);
字节[]_currentHashBytes=新字节[HashByteSize];
BlockCopy(src,SaltByteSize+1,_currentHashBytes,0,HashByteSize);
使用(Rfc2898DeriveBytes=新的Rfc2898DeriveBytes(密码,_currentSaltBytes,HasingIterationCount))
{
_passwordHashBytes=bytes.GetBytes(SaltByteSize);
}
返回arehasequal(\u currentHashBytes,\u passwordHashBytes);
}
私有静态bool arehasesequal(字节[]firstHash,字节[]secondHash)
{

int\u minHashLength=firstHash.Length我理解这个被接受的答案,并且已经投了赞成票,但我想我会把我的外行的答案扔在这里

创建散列

  • 盐是使用函数随机生成的 Rfc2898DeriveBytes生成哈希和salt。Rfc2898DeriveBytes的输入是密码、要生成的salt的大小和要执行的哈希迭代次数。
  • 然后将盐和土豆泥捣碎(先撒盐,然后撒土豆泥) 通过散列)并编码为字符串(因此盐在 这个编码的哈希(包含salt和hash)然后 存储(通常)在针对用户的数据库中
  • 根据哈希值检查密码

    检查用户输入的密码

  • 盐是从存储的散列密码中提取的
  • salt用于使用Rfc2898DeriveBytes的重载对用户输入的密码进行散列,该重载接受salt而不是生成salt
  • 然后比较存储的哈希和测试哈希
  • 散列

    在封面下,使用SHA1哈希函数()生成哈希。 此函数迭代调用1000次(在默认标识实现中)

    为什么这是安全的

    • 随机盐意味着攻击者不能使用预先生成的表 尝试破解密码的哈希数。它们需要生成一个 每种盐的哈希表。(假设黑客也破坏了你的盐)
    • 如果两个密码相同,它们将 有不同的散列。(这意味着攻击者无法推断“公共” 密码)
    • 反复调用SHA1 1000次意味着 攻击者也需要这样做,除非他们 在超级计算机上的时间他们将没有足够的资源来进行暴力 强制从哈希表中删除密码。这将大大降低为给定的salt生成哈希表的时间

    我认为这并不能直接回答您的问题,但是Brock Allen在这里写了一些您关心的问题=>并且还编写了一个开源用户身份管理和身份验证库,它具有各种锅炉板功能,如密码重置、哈希等。@Shiva谢谢,我将查看该库和p上的视频年龄。但我宁愿不必处理exte