【问题标题】:Hashing legacy salted passwords with ASP.NET Identity使用 ASP.NET 标识散列旧式加盐密码
【发布时间】:2014-03-25 22:06:35
【问题描述】:

我有一个现有的会员数据库,其中密码是用户名和唯一 ID 的哈希值。据我了解,ASP.NET Identity 会为您处理密码。

但是,在更新之前,我需要使用旧的散列密码(即它们需要在第一次登录时使用,然后我会更新它)。

IPasswordHasher 有方法:VerifyHashedPassword(string hashedPassword, string providedPassword)。这种方法不允许我传递任何盐。我意识到我不需要为任何 散列密码提供值,但对于我现有的密码,我需要进行遗留检查。

public class CoolGuyPasswordHasher : PasswordHasher {
    public IdentityContext DbContext { get; set; }

    // Custom hashing used before migrating to Identity
    public static string GetSHA1Hash(string password, string guid) {
        string passWithSalt = String.Concat(password, guid);
        return FormsAuthentication.HashPasswordForStoringInConfigFile(passWithSalt, "SHA1");
    }

    // Verify if the password is hashed using SHA1. If yes, rehash using ASP.NET Identity Crypto which is more secure
    public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) {
        //I can't pass in my salt!
        if (String.Equals(hashedPassword, GetSHA1Hash(providedPassword, wheresTheSalt), StringComparison.InvariantCultureIgnoreCase)) {
            ReHashPassword(hashedPassword, providedPassword);
            return PasswordVerificationResult.Success;
        }

        return base.VerifyHashedPassword(hashedPassword, providedPassword);
    }
}

在上面的代码 sn-p 中,请注意在对 GetSHA1Hash 的调用中我没有第二个参数要传入。

我该如何进行旧式加盐密码检查?在新系统中,我想我可以让散列密码保留为用户名 + id 的结果。但是,由于 ASP.NET Identity 的实现似乎并不能满足这一点,我最好的选择是什么?

【问题讨论】:

    标签: asp.net asp.net-identity


    【解决方案1】:

    现代盐的存储方式是将它们添加到散列密码值中(参见:http://brockallen.com/2012/10/19/password-management-made-easy-in-asp-net-with-the-crypto-api/)。因此,您可能必须修改(ahem 修改)UserManager 以从单独的列中加载盐,并将其作为散列密码的一部分传递给密码散列器。

    【讨论】:

    • 感谢您的回复。由于我需要知道何时连接(对于旧密码)以及何时不连接,使用现有散列的长度作为它之前是否使用 SHA1 散列的指标是否完全安全?根据文档 (msdn.microsoft.com/en-us/library/…),SHA1 哈希将是 40 个十六进制字符。我了解 ASP.NET Identity 在后台使用 Crypto.HashPassword (msdn.microsoft.com/en-us/library/…)。该哈希值可以是 40 个字符吗?
    • 是的,听起来是一种合理的方法。哈希大小是恒定的。
    【解决方案2】:

    我找到了this的文章;它具有扩展 UserManager 并使用精心设计的 PasswordHasher 的代码示例,它完全符合您的要求。

    【讨论】: