我想知道是否在 MVC 5 和 ASP.NET Identity Framework 附带的UserManager中默认实现的 Password Hasher 足够安全?如果是这样,如果你能向我解释它是如何工作的?
IPasswordHasher 接口看起来像这样:
public intece IPasswordHasher
{
string HashPassword(string password);
PasswordVerificationResult VerifyHashedPassword(string hashedPassword,
string providedPassword);
}
正如你所看到的,它不需要盐,但在这个线程中提到:“Asp.net Identity password hashing”它确实在幕后加盐。所以我想知道它是如何做到这一点的?
我担心的是盐是静态的,使它非常不安全。
以下是默认实现 (ASP.NET Framework或ASP.NET Core) 的工作方式。它使用带有随机 salt 的Key Derivation Function来生成哈希。salt 作为 KDF 输出的一部分。因此,每次“散列”相同的密码时,您将获得不同的散列。为了验证散列,输出被分割回 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 上找到它:AspNet.Identity 3.0和AspNet.Identity 2.0。
从评论:
/* =======================
* 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.)
*/
我理解接受的答案,并已投票,但认为我会在这里抛弃我的外行的答案...
创建哈希
salt 是使用函数Rfc2898DeriveBytes随机生成的,该函数生成哈希和 salt。Rfc2898DeriveBytes的输入是密码、要生成的 salt 的大小和要执行的哈希迭代次数。s://msdn.microsoft/en-us/library/h83s4e12(v=vs.110).aspx
然后将 salt 和 hash 混合在一起(首先是 salt,然后是 hash),并将其编码为字符串(因此 salt 被编码在 hash 中)。然后(通常)将此编码的哈希(包含 salt 和 hash)存储在针对用户的数据库中。
根据哈希值检查密码
检查用户输入的密码。
盐从存储的散列密码中提取。
salt 用于使用Rfc2898DeriveBytes的重载来哈希用户输入密码,该重载采用 salt 而不是生成 salt。s://msdn.microsoft/en-us/library/yx129kfs(v=vs.110).aspx
然后比较所存储的散列和测试散列。
The Hash使用 SHA1 哈希函数 (s://en..org/wiki/SHA-1) 生成哈希。此函数被迭代调用 1000 次(在默认的 Identity 实现中)
为什么这是安全的
随机盐意味着攻击者不能使用预先生成的哈希表来尝试破解密码。他们需要为每个盐生成一个哈希表。(假设这里黑客也了你的盐)
如果 2 个密码相同,它们将具有不同的哈希值(这意味着攻击者无法推断“常见”密码)
迭代调用 SHA1 1000 次意味着攻击者也需要这样做。这个想法是,除非他们有时间在超级计算机上,否则他们将没有足够的资源从哈希中强行输入密码。这将大大减慢为给定盐生成哈希表的时间。
对于像我这样的人来说,这是全新的,这里是 const 的代码和比较 byte [] 的实际方法。我从 stackoverflow 中获得了所有这些代码,但定义了 consts,因此可以更改值,也可以
// 24 = 192 bits
private const int SaltByteSize = 24;
private const int HashByteSize = 24;
private const int HasingIterationsCount = 10101;
public static string HashPassword(string password)
{
// ://stackoverflow/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;
}
在自定义 ApplicationUserManager 中,将 PasswordHasher 属性设置为包含上述代码的类的名称。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(69条)